python读取网络摄像头的帧,使用OpenCV Python绑定将帧从网络摄像头保存到磁盘

我正在尝试使用opencv将网络摄像头中的帧另存为jpgs或png或其他格式.尽管这里有这样的例子,但事实证明,这比我想象的要困难得多:Capturing a single image from my webcam in Java or Python

我正在尝试这样做:

if __name__ == "__main__":

print "Press ESC to exit ..."

# create windows

cv.NamedWindow('Raw', cv.CV_WINDOW_AUTOSIZE)

cv.NamedWindow('Processed', cv.CV_WINDOW_AUTOSIZE)

# create capture device

device = 0 # assume we want first device

capture = cv.CaptureFromCAM(0)

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)

# check if capture device is OK

if not capture:

print "Error opening capture device"

sys.exit(1)

while 1:

# do forever

# capture the current frame

frame = cv.QueryFrame(capture)

if frame is None:

break

# mirror

cv.Flip(frame, None, 1)

# face detection

detect(frame)

# display webcam image

cv.ShowImage('Raw', frame)

# handle events

k = cv.WaitKey(10)

if k == 0x1b: # ESC

print 'ESC pressed. Exiting ...'

break

if k == 0x63 or k == 0x43:

print 'capturing!'

s, img = capture.read()

if s:

cv.SaveImage("r'C:\test.jpg", img)

如您所见,当我按下字母c时,我尝试使用Froyo在另一个问题中建议的代码修改来使其捕获图像.它不起作用,我找不到使它起作用的文档.

请帮忙!

非常感谢,

亚历克斯

解决方法:

更改您的保存部分,如下所示:

if k == 0x63 or k == 0x43:

print 'capturing!'

cv.SaveImage("test.jpg",frame)

对我有用.由于已经捕获了要检测的帧,因此需要再次捕获以保存它.

cv.CaptureFromCam()和cv.VideoCapture()也不同.他们不要混在一起.

标签:opencv,webcam,python

来源: https://codeday.me/bug/20191201/2078226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值