I have been using 2 threads in python and I have a global queue of size 1000.
For Grabbing the frames from camera - FrameGrabber Thread
In this thread, I use OpenCV to grab the frame from camera and push it into the global queue. The camera works at the rate of 300 fps which means the thread capture frames for every 3.3 milli seconds.
#Global queue frames = Queue.Queue(1000) class FrameGrabber(threading.Thread): def __init__(self, source): threading.Thread.__init__(self) self.ID = source self.cam = cv2.VideoCapture( source ) def run(self): global frames ret, frame = self.cam.read() frames.put(frame)
For processing the frames grabbed - Processor Thread
In this thread, I get the frames from the queue and do some process and this continues. The processing of one frame takes close to 4 milli seconds
class Processor(threading.Thread): def __init__(self, source): threading.Thread.__init__(self) def run(self): global frames self.current_frame = frames.get() # Do some Processing
I am facing problems with this implementation
- I can see that some of the frames missing in the processor thread. We suspect that when the processor thread accessing the queue, the queue gets locked which is delaying the FrameGrabber thread to put frames into the queue.
Question: What is the data structure that can be the best fit for this scenario? I don't want to lose any frames from the camera ideally.