2

I have two cameras and this is important to read the frames with OpenCV exactly at the same time, I thought something like Lock but I cannot figure out the way I can implement this.

I need some trigger to push and enable the threads to read frames, and then wait for another trigger hit, something like below :

def get_frame(queue, cap):
    while running:
        if(read_frame):
            queue.put(cap.read());
        else:
            # without this sleep this function just consumes unnecessary CPU time
            time.sleep(some_time);

q = Queue.Queue()
# for every camera
for u in xrange(2):
    t = threading.Thread(target=get_frame, args = (q, caps[u]))
    t.daemon = True
    t.start()

The problems with the above implementation are :

  1. I need the sleep time to be defined since I don't know the delay in between every frame read (i.e. it might be long or short, depending on the calculation)
  2. This does not enable me to read once for every trigger hit.

So this approach won't work, Any suggestions?

dariush
  • 3,191
  • 3
  • 24
  • 43
  • Since, I dont have 2 cameras right now, please can you comment on how well the synchronization worked. – saurabheights Oct 15 '16 at 15:03
  • @saurabheights sure, I am working on it, I have a problem mentioned [here](http://stackoverflow.com/questions/40060515/how-to-capture-video-from-webcam-in-mjpg-opencv); If I can solve it then the next phase is the synchronization. but the basic and naive implementation of your suggestion seems to be working fine so far. – dariush Oct 15 '16 at 15:49

1 Answers1

0

Consider getting FPS from VideoCapture. Also, note the difference between VideoCapture.grab and VideoCapture.retrieve frame. This is used for camera synchronization.

First call VideoCapture#grab for both cameras and then retrieve the frames. See docs.

saurabheights
  • 3,967
  • 2
  • 31
  • 50