Skip to content

Commit e2934da

Browse files
committed
merge performance changes from dev branch including NaN handling update and frame crash handling and streaming fix
1 parent b3a47cb commit e2934da

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

DeepLabStream.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -281,38 +281,42 @@ def get_pose_mp(input_q, output_q):
281281
while True:
282282
if input_q.full():
283283
index, frame = input_q.get()
284+
start_time = time.time()
284285
if MODEL_ORIGIN == 'DLC':
285286
scmap, locref, pose = get_pose(frame, config, sess, inputs, outputs)
286287
# TODO: Remove alterations to original
287288
peaks = find_local_peaks_new(scmap, locref, ANIMALS_NUMBER, config)
288289
# peaks = pose
289290
if MODEL_ORIGIN == 'MADLC':
290291
peaks = get_ma_pose(frame, config, sess, inputs, outputs)
291-
292-
output_q.put((index, peaks))
292+
analysis_time = time.time() - start_time
293+
output_q.put((index, peaks, analysis_time))
293294

294295
elif MODEL_ORIGIN == 'DLC-LIVE':
295296
dlc_live = load_dlc_live()
296297
while True:
297298
if input_q.full():
298299
index, frame = input_q.get()
300+
start_time = time.time()
299301
if not dlc_live.is_initialized:
300302
peaks = dlc_live.init_inference(frame)
301303
else:
302304
peaks = dlc_live.get_pose(frame)
303-
304-
output_q.put((index, peaks))
305+
analysis_time = time.time() - start_time
306+
output_q.put((index, peaks, analysis_time))
305307

306308
elif MODEL_ORIGIN == 'DEEPPOSEKIT':
307309
predict_model = load_dpk()
308310
while True:
309311
if input_q.full():
310312
index, frame = input_q.get()
313+
start_time = time.time()
311314
frame = frame[..., 1][..., None]
312315
st_frame = np.stack([frame])
313316
prediction = predict_model.predict(st_frame, batch_size=1, verbose=True)
314317
peaks = prediction[0, :, :2]
315-
output_q.put((index, peaks))
318+
analysis_time = time.time() - start_time
319+
output_q.put((index,peaks,analysis_time))
316320
else:
317321
raise ValueError(f'Model origin {MODEL_ORIGIN} not available.')
318322

@@ -383,9 +387,9 @@ def input_frames_for_analysis(self, frames: tuple, index: int):
383387
frame_time = time.time()
384388
self._multiprocessing[camera]['input'].put((index, frame))
385389
if d_maps:
386-
self.store_frames(camera, frame, d_maps[camera], frame_time)
390+
self.store_frames(camera, frame, d_maps[camera], frame_time, index)
387391
else:
388-
self.store_frames(camera, frame, None, frame_time)
392+
self.store_frames(camera, frame, None, frame_time, index)
389393

390394
def get_analysed_frames(self) -> tuple:
391395
"""
@@ -405,12 +409,11 @@ def get_analysed_frames(self) -> tuple:
405409
self._start_time = time.time() # getting the first frame here
406410

407411
# Getting the analysed data
408-
analysed_index, peaks = self._multiprocessing[camera]['output'].get()
412+
analysed_index, peaks, analysis_time = self._multiprocessing[camera]['output'].get()
409413
skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER)
410414
print('', end='\r', flush=True) # this is the line you should not remove
411-
analysed_frame, depth_map, input_time = self.get_stored_frames(camera)
412-
analysis_time = time.time() - input_time
413-
415+
analysed_frame , depth_map, input_time = self.get_stored_frames(camera, analysed_index)
416+
delay_time = time.time() - input_time
414417
# Calculating FPS and plotting the data on frame
415418
self.calculate_fps(analysis_time if analysis_time != 0 else 0.01)
416419
frame_time = time.time() - self._start_time
@@ -434,23 +437,30 @@ def get_analysed_frames(self) -> tuple:
434437
analysed_frames[camera] = analysed_image
435438
return analysed_frames, analysis_time
436439

437-
def store_frames(self, camera: str, c_frame, d_map, frame_time: float):
440+
def store_frames(self, camera: str, c_frame, d_map, frame_time: float, index: int):
438441
"""
439-
Store frames currently sent for analysis
442+
Store frames currently sent for analysis in index based dictionary
440443
:param camera: camera name
441444
:param c_frame: color frame
442445
:param d_map: depth map
443446
:param frame_time: inputting time of frameset
447+
:param index: index of frame that is currently analysed
444448
"""
445-
self._stored_frames[camera] = c_frame, d_map, frame_time
449+
if camera in self._stored_frames.keys():
450+
self._stored_frames[camera][index] = c_frame, d_map, frame_time
451+
452+
else:
453+
self._stored_frames[camera] = {}
454+
self._stored_frames[camera][index] = c_frame, d_map, frame_time
446455

447-
def get_stored_frames(self, camera: str):
456+
def get_stored_frames(self, camera: str, index: int):
448457
"""
449-
Retrieve frames currently sent for analysis
458+
Retrieve frames currently sent for analysis, retrieved frames will be removed (popped) from the dictionary
450459
:param camera: camera name
460+
:param index: index of analysed frame
451461
:return:
452462
"""
453-
c_frame, d_map, frame_time = self._stored_frames.get(camera)
463+
c_frame, d_map, frame_time = self._stored_frames[camera].pop(index, None)
454464
return c_frame, d_map, frame_time
455465

456466
def convert_depth_map_to_image(self, d_map):

app.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
from utils.configloader import MULTI_CAM, STREAMS, RECORD_EXP
1616
from utils.gui_image import QFrame, ImageWindow, emit_qframes
1717

18-
from PyQt5.QtCore import QThread
19-
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget, QGridLayout
20-
from PyQt5.QtGui import QIcon
21-
18+
from PySide2.QtCore import QThread
19+
from PySide2.QtWidgets import QPushButton, QApplication, QWidget, QGridLayout
20+
from PySide2.QtGui import QIcon
2221

2322
# creating a complete thread process to work in the background
2423
class AThread(QThread):

utils/poser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,14 @@ def handle_missing_bp(animal_skeletons: list):
335335
:return animal_skeleton with handled missing values"""
336336
for skeleton in animal_skeletons:
337337
for bodypart, coordinates in skeleton.items():
338-
if any(np.isnan(coordinates)):
338+
np_coords = np.array((coordinates))
339+
if any(np.isnan(np_coords)):
339340
if HANDLE_MISSING == 'skip':
340341
animal_skeletons.remove(skeleton)
341342
break
342343
elif HANDLE_MISSING == 'null':
343-
new_coordinates = np.nan_to_num(coordinates, copy = True)
344-
skeleton[bodypart] = new_coordinates
344+
new_coordinates = np.nan_to_num(np_coords, copy = True)
345+
skeleton[bodypart] = tuple(new_coordinates)
345346
else:
346347
animal_skeletons.remove(skeleton)
347348
break

0 commit comments

Comments
 (0)