[英]Launching webcam and capturing an image using python
I am trying to launch the webcam and capture an image using python I used the following code 我正在尝试启动网络摄像头并使用python捕获图像,我使用了以下代码
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. 这会启动相机并在按下Esc键时关闭,但不会捕获图像。 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. 该代码将起作用,我仅添加了cv2.imwrite并保存了您已经在使用的框架。 The command writes the image if you press escape:
如果按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. [编辑:]确保您使用的是cv2,而不是cv,我已更正了您的import语句。 What version of OpenCV are you using?
您正在使用哪个版本的OpenCV?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.