I am trying to launch the webcam and capture an image using python I used the following code
import cv as cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.VideoCapture.open(0)
cv2.destroyWindow("preview")
This launches the camera and closes when Esc is pressed but does not capture an image. Is there a command I'm missing to capture the image?
This code would work, I have simply added cv2.imwrite and save the frame you are already using. The command writes the image if you press escape:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
cv2.imwrite("image.png", frame)
break
cv2.destroyWindow("preview")
[Edit:] Make sure you are using cv2 and not cv, I have corrected your import statement. What version of OpenCV are you using?