1

I have been using 2 threads in python and I have a global queue of size 1000.

  1. 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)
    
  2. 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.

Karthik
  • 390
  • 1
  • 4
  • 18
  • 2
    If processing takes longer than new frames come in, how would this system work anyway? – Vroomfondel Aug 18 '17 at 07:25
  • did you monitor whether filling the whole queue without continuous preprocessing will miss no frames (just to make sure there is no bandwidth problem)? Is your queue not running full, if preprocessing takes longer than fps interval? For a lock-free queue see https://stackoverflow.com/q/1448276/2393191 – Micka Aug 18 '17 at 07:30
  • 2
    Just to add a little bit to the comments above, I noticed you are using `multithreading`, have you thought about the [GIL](https://wiki.python.org/moin/GlobalInterpreterLock)? Multiprocessing might be more adapted for your case – Adonis Aug 18 '17 at 08:49

0 Answers0