简体   繁体   English

使用python启动网络摄像头并捕获图像

[英]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.

相关问题 在Windows上使用Python从网络摄像头捕获图像 - Capturing image from Webcam Using Python on Windows 使用Python捕获单个帧(使用网络摄像头) - Capturing a single frame with Python (using a webcam) 使用 Java 或 Python 从我的网络摄像头捕获单个图像 - Capturing a single image from my webcam in Java or Python 我想通过使用网络摄像头将捕获图像添加到我的 PyQt gui 窗口,并且网络摄像头图像将出现在 gui 窗口中 - I wanted to add capturing image by using webcam to my PyQt gui window and webcam image will come on gui window 使用OpenCV在Python中捕获网络摄像头流 - 需要帮助 - Capturing webcam stream in Python using OpenCV - Need help 使用 python 静默捕获网络摄像头快照 - Capturing webcam snapshot with python silently Python:输入文本框,与OpenCV Live Webcam图像并行捕获userinput - Python: Input text boxes capturing userinput in parallel to OpenCV Live Webcam image 捕获和操作网络摄像头馈送并将其公开为“虚拟网络摄像头” - 在 Python 中,在 Windows 中 - Capturing and manipulating a webcam feed and exposing it as a “virtual webcam” - in Python, on Windows 使用Python捕获HTML表的图像 - Capturing image of HTML table using Python python 中的网络摄像头图像失真 - webcam image distorted in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM