使用Python捕获单个帧(使用网络摄像头) [英] Capturing a single frame with Python (using a webcam)

查看:344
本文介绍了使用Python捕获单个帧(使用网络摄像头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了其他解决方案,但他们没有回答我的问题. 我的问题是,每当我尝试从视频中仅捕获一帧(我基本上想用网络摄像头拍摄照片)时,我只会得到一个黑色的窗口.

I checked other solutions, but they weren't answering my question. My issue is that whenever I try to capture just one frame from a video (I want to basically take a picture with my webcam) I just get a black window.

代码-

import cv2


cam = cv2.VideoCapture(0)
frame = cam.read()[1]
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)

屏幕截图- https://imgur.com/kfeXYvQ

我的网络摄像头是USB,720p,30fps.

My webcam is USB, 720p at 30fps.

谢谢.

推荐答案

两件事之一.您可能需要在cv2.imshow()之后添加waitKey().另外,您也不会检查相机的退货是否有任何错误.可能是连接问题.这是两件事.

One of two things. It might be that you need to add a waitKey() after cv2.imshow(). Alternatively, you aren't checking the return from the camera for any errors. It could be a connection problem. Here are the two things to do.

import cv2

cam = cv2.VideoCapture(0)
retval, frame = cam.read()
if retval != True:
    raise ValueError("Can't read frame")

cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
cv2.waitKey()

waitKey()函数将暂停程序,直到用户在窗口中输入键为止.

The waitKey() function halts the program until a user has entered a key in the window.

这篇关于使用Python捕获单个帧(使用网络摄像头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆