Skip to content
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
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft

Conversation

lwhite1
Copy link
Contributor

@lwhite1 lwhite1 commented Jan 23, 2025

Short PR that swaps multi-threading for multiprocesing to see if there's any improvement in write performance.

This change should be considered temporary, as it's not clear that this is an appropriate approach for image processing.

@lwhite1 lwhite1 requested a review from divyadk January 23, 2025 21:29
Copy link

@divyadk divyadk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes in the stop process to clear the process and to avoid dangling reference just in case.

def stop(self): if self.open and self.recording: self.logger.debug('FLIR: Setting Record Stop Flag') self.recording = False if self.save_process is not None: #wait for child process to finish self.save_process.join(timeout=5) if self.save_process.is_alive(): self.logger.error('FLIR: Save process did not terminate in time.') #terminating the child process self.save_process.terminate() #clear the save_process reference just in case self.save_process = None self.streaming = False

self.save_thread.start()

try:
self.save_process = multiprocessing.Process(target = self.camCaptureVid())
Copy link

Choose a reason for hiding this comment

The 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)

@@ -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):
Copy link

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

def camCaptureVid(self):
    try:
        # Initializing VideoWriter in the child process instead
        fourcc = cv2.VideoWriter_fourcc(*"MJPG")
        video_out = cv2.VideoWriter(
            self.video_filename, fourcc,
            self.FRAME_RATE_OUT, self.frameSize
        )
        self.logger.debug('FLIR: Save Process Started')
 
       #write the frames from the queue as before
       while self.recording or not self.image_queue.empty():
            try:
                # Retrieve frame from the queue
                dequeuedImage = self.image_queue.get(block=True, timeout=1)
                # Write frame to the video file
                video_out.write(dequeuedImage)
            except queue.Empty:
                continue
    except Exception as e:
        self.logger.error(f'FLIR: Error in save process: {e}')
    finally:
        # Safely release video writer
        video_out.release()
        self.logger.debug('FLIR: Exiting Save Process')

@lwhite1 lwhite1 marked this pull request as draft February 6, 2025 19:27
@lwhite1 lwhite1 requested review from neurobooth-bot and removed request for neurobooth-bot February 6, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants