opencv+python+linux+webcam = cannot capture frames - python

I am trying to code up simple face detection in python using opencv. But unfortunately my opencv is refusing to detect my webcam. I am not sure how it works internally, as documentation is very limited, but CaptureFromCAM(-1) returns some object, but QueryFrame returns nones. When I try to use one of my two cameras for example in cheese, I get video without a problem.
capture = cv.CaptureFromCAM(-1)
faceCascade = cv.Load("haarcascade_frontalface_alt.xml")
while (cv.WaitKey(15)==-1):
img = cv.QueryFrame(capture)
if img != None:
image = DetectFace(img, faceCascade)
cv.ShowImage("face detection test", image)
cv.ReleaseCapture(capture)
Any ideas?

Ok, I have figured it out. Basically my openvc was compiled with v4l (video for linux) support.
When solving this problem you first need to make sure your camera is working with some other application using v4l. If that is the case then you can try to recompile openvc with v4l support. For gentoo (which uses portage) it is very simple:
sudo su
USE="v4l v4l2" emerge -av opencv
for other package managers either figure something out or compile from source with USE_V4L=ON.

Related

Python OpenCV with Imaging device USB3 Vision compliant camera - VideoCapture() index of camera not found

I have a problem with using external camera in Python OpenCV.
I am using Windows.
In device manager, camera is not under "Cameras", but under "Imaging devices" and is called USB3 Vision compliant camera.
The camera is working fine on app given by company (UcamViewer).
I want to connect to this camera using cv2.VideoCapture().
I have of course tried:
cv2.VideoCapture(k)
for k = [0..100]
Only k = 0 is occupied and works - that is my webcam.
I have also tried adding multiple possible values to the second parameter of the VideoCapture(), which did not work.
how to get video from a "USB3 Vision" device?
This question suggested to use cv.CAP_XIMEA as the second parameter of VideoCapture(), but CAP_XIMEA does not seem to be a possible value.
If I can not fix this issue and use VideoCapture(), what would you suggest to use to capture the video and convert it to format that can be used by openCV methods?
Any ideas how could I solve this issue will be appreciated.
Thank you for your help.

How to select a different webcam using cv2 in python

I am trying to stream live video from an external camera using cv2. I was able to write the simple code to stitch the frames and stream it. But am struggling to find how to change the camera.
I tried to run it after disabling the main webcam from the task manager, but it still did not work.
So, if anyone can help me with some clue regarding the same, that would be a great help.
Cameras are numbered for Windows. You can try a few indeces and check which camera index belongs to the camera you want.
capture = cv2.VideoCapture(index)

Code editor not displaying suggestions for functions when working with opencv

I just installed opencv and was following a tutorial and realized that when I type down capture. vscode is not giving me the suggestions for the functions available for the 'capture' pointer. It is, however, showing me sugestions from the cv2 library.
This is what I want to see
but
This is what I am getting
So my question basically is, How can I fix this? I havnet been able to find any solution online. I want to be able to see what functions are available so I can learn and explore the library better.
Here's the complete code, just in case:
import cv2 as cv
capture = cv.VideoCapture('video.mp4')
while True:
isTrue, frame = capture.read() #grab video frame by frame
cv.imshow('Camera', frame)
if cv.waitKey(20) & 0xFF==ord('d'):
break
capture.release()
cv.destroyAllWindows()
Thanks in advance!
This problem has been answered here.
Python Language Server does not support Intesllisense from the .pyd file for now.

How to load video with opencv-python on Windows machine? (Difference btw Windows / Linux ?)

I am new to the opencv(3.0.0)-python(2.7).
There is an error with loading the video on a windows machine with cv2.Videocapture. I actually tried to use haar classifier using the video, but since the video file wasn't playing I tried the following simple code.
import cv2
cap = cv2.VideoCapture('‪C:\\Users\\jimmy\\Desktop\\bbd.mp4')
if cap.isOpened():
print 'Yes'
else:
print 'No'
And it keeps returning 'No'.
I don't think it's the problem of directory because it worked perfectly with images and imshow, imread functions. So, these kinds of errors always happen when I try to load a video.
I actually used raspberrypi to load a video before and it worked perfectly, so I am wondering is there any difference between rpi and windows machine that I don't know. Also please tell me how to fix it.
Thanks in advance !
Change your path from this:
cap = cv2.VideoCapture('‪C:\\Users\\jimmy\\Desktop\\bbd.mp4')
to this
cap = cv2.VideoCapture('‪C:/Users/jimmy/Desktop/bbd.mp4')
OpenCV doesn't like the forward slashes for some reason

Getting Python input from Webcam on raspberry pi?

i'm using a sony ps3 eye as webcam input on my pi for an OpenCL python program I'm writing, but for some reason no matter what i do the pi python compiler isn't accessing the webcam. Yet the same code when run on my laptop using the same webcam runs perfectly. Then i checked the usb devices on my pi and it state that "sony playstation 3 eye cam" was on usb port 6. also when using the "motion" package on the pi i was able to use the camera perfectly as input. So my problem is again that the python compiler isn't communicating with my webcam on the pi. Please, think about this and tell me how to fix it; i simply can't think of what's wrong. Here's the sample code I used, and please help me out. Thank you very much.
import cv2.cv as cv
#cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
while not capture:
print "error opening capture device, correction attempt"
while True:
frame = cv.QueryFrame(capture)
if frame is None:
print "fail with putting in frame"
else:
c = cv.WaitKey(100)
print 'capturing!'
cv.SaveImage("pictest.png", frame)
I never updated this but my solution was: I used the Motion Package for Raspberry Pi to receive images (since the Sony Ps3 EyeToy Camera did not have drivers written that were compatible with the Pi) and then Used OpenCV to analyze each Image. Doing this as each image came in, many times a second, is the same as analyzing video through OpenCV.This implementation worked perfectly for my needs, and I used other Bash tools to delete old image files after a certain time period so my memory was not unnecessarily filled.
There are some known problems with isochronous USB. Some camera issues have been addressed with recent fixes, but others remain (and apparently being worked upon). What kernel version are you using (uname -a)?
The fixes have not worked their way into the official distribution yet, so if you don't want to wait can run rpi-update to pick up the latest kernel (assuming you're using Raspbian). You want at least #389.

Categories

Resources