Skip to content

Commit 4a90fda

Browse files
committed
Merge branch 'dev_models'
2 parents 0f2ab63 + 1250a86 commit 4a90fda

File tree

9 files changed

+295
-47
lines changed

9 files changed

+295
-47
lines changed

DeepLabStream.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import pandas as pd
1818
import click
1919

20-
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL, MULTI_CAM, STACK_FRAMES, \
20+
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL_NAME, MULTI_CAM, STACK_FRAMES, \
2121
ANIMALS_NUMBER, STREAMS, STREAMING_SOURCE
22-
from utils.poser import load_deeplabcut, get_pose, find_local_peaks_new, calculate_skeletons
22+
from utils.poser import load_deeplabcut, get_pose, find_local_peaks_new, calculate_skeletons,\
23+
get_ma_pose, calculate_ma_skeletons, calculate_skeletons_dlc_live, transform_2skeleton
2324
from utils.plotter import plot_bodyparts, plot_metadata_frame
2425

2526

@@ -266,19 +267,61 @@ def write_video(self, frames: dict, index: int):
266267
######################
267268
@staticmethod
268269
def get_pose_mp(input_q, output_q):
270+
from utils.configloader import MODEL_ORIGIN
271+
from utils.poser import get_ma_pose
272+
269273
"""
270274
Process to be used for each camera/DLC stream of analysis
271275
Designed to be run in an infinite loop
272276
:param input_q: index and corresponding frame
273277
:param output_q: index and corresponding analysis
274278
"""
275-
config, sess, inputs, outputs = load_deeplabcut()
276-
while True:
277-
if input_q.full():
278-
index, frame = input_q.get()
279-
scmap, locref, pose = get_pose(frame, config, sess, inputs, outputs)
280-
peaks = find_local_peaks_new(scmap, locref, ANIMALS_NUMBER, config)
281-
output_q.put((index, peaks))
279+
280+
if MODEL_ORIGIN == 'DLC' or MODEL_ORIGIN == 'MADLC':
281+
config, sess, inputs, outputs = load_deeplabcut()
282+
while True:
283+
if input_q.full():
284+
index, frame = input_q.get()
285+
if MODEL_ORIGIN == 'DLC':
286+
scmap, locref, pose = get_pose(frame, config, sess, inputs, outputs)
287+
# TODO: REmove alterations to original
288+
#peaks = find_local_peaks_new(scmap, locref, ANIMALS_NUMBER, config)
289+
peaks = pose
290+
if MODEL_ORIGIN == 'MADLC':
291+
peaks = get_ma_pose(frame, config, sess, inputs, outputs)
292+
293+
output_q.put((index, peaks))
294+
295+
elif MODEL_ORIGIN == 'DLC-LIVE':
296+
from dlclive import DLCLive
297+
from utils.configloader import MODEL_PATH
298+
dlc_live = DLCLive(MODEL_PATH)
299+
while True:
300+
if input_q.full():
301+
index, frame = input_q.get()
302+
if not dlc_live.is_initialized:
303+
peaks = dlc_live.init_inference(frame)
304+
else:
305+
peaks = dlc_live.get_pose(frame)
306+
307+
output_q.put((index, peaks))
308+
elif MODEL_ORIGIN == 'DEEPPOSEKIT':
309+
from deepposekit.models import load_model
310+
from utils.configloader import MODEL_PATH
311+
model = load_model(MODEL_PATH)
312+
predict_model = model.predict_model
313+
while True:
314+
if input_q.full():
315+
index, frame = input_q.get()
316+
frame = frame[..., 1][..., None]
317+
st_frame = np.stack([frame])
318+
prediction = predict_model.predict(st_frame, batch_size=1, verbose=True)
319+
peaks= prediction[0,:,:2]
320+
output_q.put((index, peaks))
321+
322+
323+
else:
324+
raise ValueError(f'Model origin {MODEL_ORIGIN} not available.')
282325

283326
@staticmethod
284327
def create_mp_tools(devices):
@@ -366,7 +409,9 @@ def get_analysed_frames(self) -> tuple:
366409

367410
# Getting the analysed data
368411
analysed_index, peaks = self._multiprocessing[camera]['output'].get()
369-
skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER)
412+
#TODO: REMOVE IF USELESS
413+
skeletons = [transform_2skeleton(peaks)]
414+
#skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER)
370415
print('', end='\r', flush=True) # this is the line you should not remove
371416
analysed_frame, depth_map, input_time = self.get_stored_frames(camera)
372417
analysis_time = time.time() - input_time
@@ -722,7 +767,7 @@ def show_benchmark_statistics():
722767

723768
if benchmark_enabled:
724769
import re
725-
short_model = re.split('[-_]', MODEL)
770+
short_model = re.split('[-_]', MODEL_NAME)
726771
short_model = short_model[0] + '_' + short_model[2]
727772
np.savetxt(f'{OUT_DIR}/{short_model}_framerate_{FRAMERATE}_resolution_{RESOLUTION[0]}_{RESOLUTION[1]}.txt', np.transpose([fps_data, whole_loop_time_data]))
728773

experiments/custom/triggers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from utils.configloader import RESOLUTION
1212
from collections import deque
1313
import numpy as np
14+
15+
import time
16+
1417
"""Single posture triggers"""
1518

1619
class HeaddirectionROITrigger:
@@ -440,4 +443,4 @@ def check_skeleton(self, skeleton: dict):
440443
color=color)}}
441444
response = (result, response_body)
442445

443-
return response
446+
return response

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
numba
12
gpiozero
23
pigpio
34
pyserial

settings.ini

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
[Streaming]
2-
RESOLUTION = 848, 480
2+
RESOLUTION = 1920, 1080
33
FRAMERATE = 30
44
OUTPUT_DIRECTORY = /Output
55
#if you have connected multiple cameras (USB), you will need to select the number OpenCV has given them.
66
#Default is "0", which takes the first available camera.
77
CAMERA_SOURCE = 0
8-
#you can use camera, ipwebcam or video to select your input source
9-
STREAMING_SOURCE = camera
8+
#you can use "camera", "ipwebcam" or "video" to select your input source
9+
STREAMING_SOURCE = video
1010

11-
[DeepLabCut]
12-
DLC_PATH = DLC_PATH
13-
MODEL = MODEL_NAME
11+
[Pose Estimation]
12+
#possible origins are: DLC, DLC-LIVE, MADLC, DEEPPOSEKIT
13+
MODEL_ORIGIN = ORIGIN
14+
MODEL_PATH = PATH_TO_MODEL
15+
MODEL_NAME = MODEL_NAME
16+
; only used in DLC-LIVE and DeepPoseKit for now; if left empty or to short, auto-naming will be enabled in style bp1, bp2 ...
17+
ALL_BODYPARTS = bp1, bp2, bp3, bp4
1418

1519
[Experiment]
1620
#Available parameters are "CUSTOM" and "BASE"
17-
EXP_ORIGIN = BASE
21+
EXP_ORIGIN = CUSTOM
1822
#Name of the experiment config in /experiments/configs or name of the custom experiment in /experiments/custom/experiments.py
19-
EXP_NAME = CONFIG_NAME
23+
EXP_NAME = ExampleExperiment
2024
#if you want the experiment to be recorded as a raw video set this to "True".
2125
RECORD_EXP = True
2226

2327
[Video]
2428
#Full path to video that you want to use as input. Needs "STREAMING_SOURCE" set to "video"!
25-
VIDEO_SOURCE = PATH_TO_PRERECORDED_VIDEO
29+
VIDEO_SOURCE = PATH_TO_VIDEO
2630

2731
[IPWEBCAM]
2832
#Standard Port is 5555 if you followed the SmoothStream setup

utils/VideoAnalyzer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from utils.configloader import VIDEO_SOURCE, OUT_DIR, ANIMALS_NUMBER
1919
from experiments.custom.experiments import ExampleExperiment
2020

21+
from deepposekit.models import load_model
22+
2123

2224
def create_dataframes(data_output):
2325
"""
@@ -43,7 +45,7 @@ def create_dataframes(data_output):
4345

4446
def start_videoanalyser():
4547
print("Starting DeepLabCut")
46-
config, sess, inputs, outputs = load_deeplabcut()
48+
model = load_model(r"D:\DeepPoseKit-Data-master\datasets\fly\best_model_densenet.h5")
4749

4850
experiment_enabled = False
4951
video_output = True

utils/configloader.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ def get_script_path():
2727
adv_cfg_path = os.path.join(os.path.dirname(__file__), 'advanced_settings.ini')
2828
with open(adv_cfg_path) as adv_cfg_file:
2929
adv_dsc_config.read_file(adv_cfg_file)
30-
# DeepLabCut
31-
deeplabcut_config = dict(dsc_config.items('DeepLabCut'))
30+
31+
#poseestimation
32+
MODEL_ORIGIN = dsc_config['Pose Estimation'].get('MODEL_ORIGIN')
33+
MODEL_PATH = dsc_config['Pose Estimation'].get('MODEL_PATH')
34+
MODEL_NAME = dsc_config['Pose Estimation'].get('MODEL_NAME')
35+
ALL_BODYPARTS = tuple(part for part in dsc_config['Pose Estimation'].get('ALL_BODYPARTS').split(','))
3236

3337
# Streaming items
3438
try:
@@ -37,7 +41,7 @@ def get_script_path():
3741
print('Incorrect resolution in config!\n'
3842
'Using default value "RESOLUTION = 848, 480"')
3943
RESOLUTION = (848, 480)
40-
MODEL = dsc_config['Streaming'].get('MODEL')
44+
4145
FRAMERATE = dsc_config['Streaming'].getint('FRAMERATE')
4246
OUT_DIR = dsc_config['Streaming'].get('OUTPUT_DIRECTORY')
4347
CAMERA_SOURCE = dsc_config['Streaming'].get('CAMERA_SOURCE')
@@ -56,7 +60,6 @@ def get_script_path():
5660

5761
START_TIME = time.time()
5862

59-
6063
"""advanced settings"""
6164
STREAMS = [str(part).strip() for part in adv_dsc_config['Streaming'].get('STREAMS').split(',')]
6265
MULTI_CAM = adv_dsc_config['Streaming'].getboolean('MULTIPLE_DEVICES')

utils/plotter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
import cv2
10-
import random
1110

1211

1312
def plot_dots(image, coordinates, color, cond=False):
@@ -55,6 +54,16 @@ def plot_metadata_frame(image, frame_width, frame_height, current_fps, current_e
5554
(int(frame_width * 0.8), int(frame_height * 0.95)), font, 1, (255, 255, 0))
5655
return res_image
5756

57+
def plot_dlc_bodyparts(image, bodyparts):
58+
"""
59+
Plots dlc bodyparts on given image
60+
adapted from plotter
61+
"""
62+
63+
for bp in bodyparts:
64+
center = tuple(bp.astype(int))
65+
cv2.circle(image, center=center, radius=3, color=(255, 0, 0), thickness=2)
66+
return image
5867

5968
def plot_triggers_response(image, response):
6069
"""
@@ -74,6 +83,4 @@ def plot_triggers_response(image, response):
7483
cv2.rectangle(image, **plot['square'], thickness=2)
7584

7685

77-
def plot_special_peak():
78-
if random.randint(0, 100000) == 1:
79-
print(EGG)
86+

0 commit comments

Comments
 (0)