Ask Your Question
0

VideoCapture read non blocking?

asked 2015-01-19 04:04:47 -0500

Jaman42 gravatar image

I am programming in Python, Pygame from what I understand uses opencv for its webcam functions. There is a query_image() available that polls the camera so you can start fetching a frame only when there is a frame to fetch, for example:

pygame.init()
pygame.camera.init()
webcam = pygame.camera.Camera("/dev/video0",(320,240))
webcam.start()
if(webcam.query_image()):
    image = webcam.get_image()

When I use the following code with opencv:

cap = cv2.VideoCapture(0)
rval, frame = cap.read()

It waits until the frame is fetched which is for me undesirable, is there a way to poll the camera before trying to fetch to see if there is a frame available?

edit retag flag offensive close merge delete

Comments

not know at all python .. but i think if cap.open() first is needed ... any how normally the detection works in a separate thread ....

gfx gravatar imagegfx ( 2017-07-18 16:06:21 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-09-29 07:14:46 -0500

supra56 gravatar image

updated 2017-09-29 07:15:25 -0500

Try this:

import pygame

import pygame.camera

from pygame.locals import *

pygame.init()
pygame.camera.init()

cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()

image = cam.get_image()

pygame.image.save(image, 'image.jpg')

cam.stop()
edit flag offensive delete link more

Comments

Btw, I'm using raspberry pi 3, picamera version 2, Debian Strecth version 9, python 3.5 and OpenCV 3.3.0.

supra56 gravatar imagesupra56 ( 2017-09-29 07:48:06 -0500 )edit
0

answered 2017-09-29 05:43:56 -0500

punnerud gravatar image

updated 2017-09-29 05:44:15 -0500

I had to place this after "webcam.start()" for it to work:
import time
time.sleep(2)

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-01-19 04:04:47 -0500

Seen: 1,217 times

Last updated: Sep 29 '17