So I'm trying to display a preview of human detection done on raspberry pi on a webpage.
I already saw, and tried this proposed solution. My issue with it is that the processing is done only when the page is viewed, for obvious reasons. I want the processing to happen independent on whether the preview is active, and when the page is viewed for it to be simply attached "on top".
Having a separate thread for processing looks like a probable solution, but due to flask's event driven approach I'm struggling to figure out how to safely pass the frames between threads (pre processing takes reasonable time, and if i simply use locks to guard, sometimes times it raises the exceptions), and generally am not sure if that the best way to solve the problem.
Is multithreading the way to go? Or should I maybe choose some other library other then flask for that purpose?
CodePudding user response:
From the example you posted, you can use the VideoCamera
class but split get_frame
into two functions. One that retrieves the frame and processes it (update_frame
), then another that returns the latest frame in the encoding you need for flask (get_frame
). Simply run update_frame
in a separate thread and that should work.
Its probably best practice to store the new frame in a local variable first, then use a lock to read/write to the instance variable you are storing the latest frame in. But I'll spare the example code that implementation.
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
self._last_frame = None
def __del__(self):
self.video.release()
def update_frame(self):
ret, frame = self.video.read()
# DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV
# Perform mutual exclusion here
self._last_frame = frame
def get_frame():
# Perform mutual exclusion here
frame = self._last_frame
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()