简体   繁体   中英

Python QueryFrame returns None, but C++ bindings work

In OpenCV 2.3.1 (built from source) on Ubuntu 10.04, the C++ fragment

cvNamedWindow("Camera", 1);
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
while (1) {
  IplImage* frame = cvQueryFrame(capture);
  cvShowImage("Camera", frame);
  key = cvWaitKey(10);
  ...

will open up a window and show video from my ThinkPad camera, but

import cv2.cv as cv
# or import cv
cv.NamedWindow("Camera", 1)
capture = cv.CaptureFromCAM(-1)
while True:
  frame = cv.QueryFrame(capture)
  cv.ShowImage("Camera", frame)
  key = cv.WaitKey(10)
  ...

fails (the window is gray), because cv.QueryFrame returns None (and the light on the laptop camera doesn't come on.)

Any idea what may be going on here (and how I might remedy it)? cv.QueryFrame works when displaying .jpg , so this seems to be a camera issue.

Found a workaround, via opencv+python+linux+webcam = cannot capture frames , which I'll leave here for posterity.

Install lib4vl ( apt-get install libv4l-dev ) and in the cmake step of building OpenCV , pass -D WITH_4VL=ON . (I'd been building with that OFF.)

Why C++ works without lib4vl but the Python bindings require it to work with a webcam is a puzzle, which perhaps some OpenCV-knowledgable person can explain. I'd love to hear an explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM