-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use multiprocessing for writing Flir frames #465
Draft
lwhite1
wants to merge
20
commits into
master
Choose a base branch
from
new-branch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
19655d2
try multiprocessing of frame queue
lwhite1 985b7b6
Merge branch 'master' into new-branch-1
lwhite1 0181f59
Update flir_cam.py
lwhite1 9d7100c
Update flir_cam.py
lwhite1 d99d9ff
handle additional variable shared between processes
lwhite1 27a3279
Update flir_cam.py
lwhite1 f533091
Update flir_cam.py
lwhite1 4b8ad3a
Update flir_cam.py
lwhite1 f19619a
remove test code
lwhite1 8c12d46
Update flir_cam.py
lwhite1 f34eebe
Update flir_cam.py
lwhite1 94e8272
Update flir_cam.py
lwhite1 4965a1d
Update flir_cam.py
lwhite1 3419737
Update flir_cam.py
lwhite1 b022ed2
fixing boolean check in multithreading.Manager
lwhite1 757fc45
Remove debugging code and add comments
lwhite1 d5f8c65
added back old thread-based flir logic for comparison
lwhite1 1c22411
remove test code
lwhite1 8bb2796
Insert test code in flir_cam.py.
lwhite1 18d5ad8
comment-out test code
lwhite1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,15 @@ | |
import os | ||
import threading | ||
import uuid | ||
import neurobooth_os.iout.metadator as meta | ||
import logging | ||
import multiprocessing | ||
from typing import Callable, Any | ||
|
||
import cv2 | ||
import PySpin | ||
from pylsl import StreamInfo, StreamOutlet | ||
import skvideo | ||
import skvideo.io | ||
import h5py | ||
|
||
import neurobooth_os.iout.metadator as meta | ||
from neurobooth_os.iout.stim_param_reader import FlirDeviceArgs | ||
from neurobooth_os.iout.stream_utils import DataVersion, set_stream_description | ||
from neurobooth_os.log_manager import APP_LOG_NAME | ||
|
@@ -44,6 +42,8 @@ def __init__( | |
gamma=0.6, | ||
fd=1, | ||
): | ||
self.frame_counter = None | ||
self.save_process = None | ||
self.device_args: FlirDeviceArgs = device_args | ||
# not currently using sizex, sizey --> need to update to use these parameters | ||
# need to read these parameters from database | ||
|
@@ -65,8 +65,7 @@ def __init__( | |
|
||
self.get_cam() | ||
self.setup_cam() | ||
|
||
self.image_queue = queue.Queue(0) | ||
self.image_queue = multiprocessing.Queue(0) | ||
self.outlet = self.createOutlet() | ||
|
||
self.logger.debug(f'FLIR: fps={str(self.device_args.sample_rate())}; ' | ||
|
@@ -151,7 +150,7 @@ def createOutlet(self): | |
# function to capture images, convert to numpy, send to queue, and release | ||
# from buffer in separate process | ||
def camCaptureVid(self): | ||
self.logger.debug('FLIR: Save Thread Started') | ||
self.logger.debug('FLIR: Save Process Started') | ||
while self.recording or self.image_queue.qsize(): | ||
try: | ||
dequeuedImage = self.image_queue.get(block=True, timeout=1) | ||
|
@@ -192,11 +191,16 @@ def prepare(self, name="temp_video"): | |
self.streaming = True | ||
|
||
def record(self): | ||
|
||
self.logger.debug('FLIR: LSL Thread Started') | ||
self.recording = True | ||
self.frame_counter = 0 | ||
self.save_thread = threading.Thread(target=self.camCaptureVid) | ||
self.save_thread.start() | ||
|
||
try: | ||
self.save_process = multiprocessing.Process(target = self.camCaptureVid()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor change here -> passing the function object instead of calling the function. self.save_process = multiprocessing.Process(target = self.camCaptureVid) |
||
self.save_process.start() | ||
except BaseException as e: | ||
self.logger.error(f'Unable to start Flir save process; error={e}') | ||
|
||
self.stamp = [] | ||
while self.recording: | ||
|
@@ -206,7 +210,12 @@ def record(self): | |
except: | ||
continue | ||
|
||
self.image_queue.put(im) | ||
try: | ||
self.image_queue.put(im) | ||
except BaseException as e: | ||
self.logger.critical(f'Unable to enqueue Flir frame; error={e}') | ||
raise e | ||
|
||
self.stamp.append(tsmp) | ||
|
||
try: | ||
|
@@ -216,17 +225,15 @@ def record(self): | |
self.outlet = self.createOutlet(self.video_filename) | ||
self.outlet.push_sample([self.frame_counter, tsmp]) | ||
|
||
# self.video_out.write(im_conv_d) | ||
self.frame_counter += 1 | ||
|
||
if not self.frame_counter % 1000 and self.image_queue.qsize() > 2: | ||
self.logger.debug( | ||
f"Queue length is {self.image_queue.qsize()} frame count: {self.frame_counter}" | ||
) | ||
|
||
self.cam.EndAcquisition() | ||
self.recording = False | ||
self.save_thread.join() | ||
self.save_process.join() | ||
self.video_out.release() | ||
self.logger.debug('FLIR: Video File Released; Exiting LSL Thread') | ||
|
||
|
@@ -243,7 +250,7 @@ def close(self): | |
|
||
def ensure_stopped(self, timeout_seconds: float) -> None: | ||
"""Check to make sure the recording is actually stopped.""" | ||
self.video_thread.join() | ||
self.video_thread.join(timeout_seconds) | ||
if self.video_thread.is_alive(): | ||
self.logger.error('FLIR: Potential Zombie Thread Detected!') | ||
raise FlirException('Potential Zombie Thread Detected!') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can create a video writer here (that gets created in the child process instead). this can be the rewritten function:
#rewritten function