I am trying to grab a single frame from the webcam using OpenCV. But the image returned by the QueryFrame() is much older than the current frame. It takes multiple QueryFrame() calls to get the most recent image but even that lags by 2 to 3 seconds from the expected current image. I tried using different webcams but the outcomes are same. I tried the read() method from cv2 and had the same issues. Is there anyway to fix this and get the current frame from the webcam using OpenCV?
Webcam has 30fps with 640/480 resolution. OS : Ubuntu 12.04, OpenCV 2.4.9
# CV code
import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test2.JPG", img)
# CV2 code
import cv2
cam = cv2.VideoCapture()
cam.open(-1)
img=cam.read()
cv2.imwrite("test3.jpg",img[1])
A work around to fix the issue for me was to acquire the webcam access just when I needed to take a snap and releasing it immediately.
def getframe(name):
cam.open(0)
img=cam.read()
cv2.imwrite(str(name)+".jpg",img[1])
cam.release()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With