Skip to content Skip to sidebar Skip to footer

Problem With Opencv And Python

I'm new to python and Opencv and I tried to put in the following code to save an image to my computer from my webcam: import cv if __name__=='__main__': pCapturedImage = cv.Cap

Solution 1:

Have you tried the demo programs? They show how to use the webcam among many other things.

For the first problem, I am not familiar with using cameras in opencv, but I got it to work by opening the capture (capture.open(device_id) in the code below)

Here is a working python sample (I use the newer c++ interface: imread, imwrite, VideoCapture, etc... which you can find in the OpenCV docs listed as "cv2" when it is available for python.):

import cv2

capture = cv2.VideoCapture()  # thisis the newer c++ interface
capture.open(0)  # Use your device id; I think thisis what you are missing. 
image = capture.read()[1]
cv2.imwrite("test.jpg", image)

I got your second sample also working just by using open on the capture object:

import cv2

cv2.namedWindow("camera", 1)  # this is where you will put the video images
capture = cv2.VideoCapture()
capture.open(0)  # again, use your own device idwhileTrue:
    img = capture.read()[1]
    cv2.imshow("camera", img)
    if cv2.waitKey(10) == 27:  # waiting for the esc keybreak
cv2.destroyWindow("camera")

Post a Comment for "Problem With Opencv And Python"