Ask Your Question
0

cv2.imshow() to show sequence of live images

asked 2014-09-15 15:41:24 -0600

roboscan gravatar image

updated 2014-09-15 15:50:08 -0600

berak gravatar image

Hi, I am trying out a program in python using OpenCV to see feeds from multiple webcams.

I have no trouble viewing the individual webcam feeds in separate windows and no problem viewing or recording feeds from individual cameras.

I am playing around with an idea that I have to use 2 cameras and show each one in every-other frame - sort of the way 3-d video works in some instances.

I am attempting to show the cameras by using a for loop, but it is not working.

 cams = [cv2.videoCapture(0),
 cv2.videoCapture(1)]

 for i in range (0,len(cams)):
     read, frame = cams[i].read()
     cv2.imshow("Cameras",frame)

     cv2.waitKey(1) #I added this to play with timings, thought it might help - didn't.

In theory, it should be simply playing frames from source 1 and then source 2 and repeat.. but what seems to actually happen is it reads each camera once and then just sequences the images... repeatedly - the "feed" that is being displayed is not live after the first frame.

Would anyone know why this is happening?

Thank you, J

edit retag flag offensive close merge delete

Comments

Hi berak, thank you...

Thing is, I don't want the images to appear in 2 different windows... Doing that, BTW,works just fine...

What I'm attempting is to make a single window where every other frame is Cam1 and the others are Cam 2...

Single window: cam1, cam2, cam1, cam2, cam1, cam2, etc.

What is actually happening is that the window is showing every other frame from cams one and two respectively as I hoped, but it is only capturing frame from each camera once, and then repeatedly showing the same frame(s) over and over...

So, I want: Cam1(frame1), Cam2(frame1), Cam1(frame2),Cam2(frame2),Cam1(frame3), Cam2(frame3), etc.

What I'm getting: Cam1(frame1), Cam2(frame1),Cam1(frame1), Cam2(frame1),Cam1(frame1), Cam2(frame1), etc.

Puzzling.

roboscan gravatar imageroboscan ( 2014-09-15 22:28:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-09-18 23:37:31 -0600

roboscan gravatar image

I sorta got it working by adding a cv2.waitKey() after my read() and after the imshow() commands. The strange thing now is that it is not consistent. When I play with timings sometimes it does and doesn't work...

This Works: for i in range (0,len(cams)): read, frame = cams[i].read() cv2.waitKey(500) cv2.imshow("Cameras",frame) cv2.waitKey(500)

But This Does NOT work for i in range (0,len(cams)): read, frame = cams[i].read() cv2.waitKey(5000) cv2.imshow("Cameras",frame) cv2.waitKey(500)

This works for i in range (0,len(cams)): read, frame = cams[i].read() cv2.waitKey(1) cv2.imshow("Cameras",frame) cv2.waitKey(1)

But This Does NOT work for i in range (0,len(cams)): read, frame = cams[i].read() cv2.waitKey(1) cv2.imshow("Cameras",frame) k = cv2.waitKey(4) if k == ord('q'): #CODE TO CLOSE MY WINDOWS HERE

By "not working" I mean (this test anyway) that the program runs with no errors, but it does not open a window and display anything. I've tried throwing in some Print statements for debugging and depending on where I place them, it may or may not work. I can't seem to find any consistency so I can't really figure out "what works (and why)" and "what doesn't."

Any thoughts?

Which brings me to another question about waitKey() that I will ask separately...

Thanks!

J

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-09-15 15:41:24 -0600

Seen: 15,688 times

Last updated: Sep 18 '14