import cv2
ram_frames=30
cam = cv2.VideoCapture(0)
def get_image():
cap = cam.read()
return cap
for i in xrange(ramp_frames):
temp = get_image()
image = get_image()
cv2.imwrite('bin/color.jpg',image)
我收到的错误是:
File "C:\modules\imlib.py", line 1035, in __init__
self.imin = self.WinWebCam()
File "C:\modules\imlib.py", line 1125, in WinWebCam
cv2.imwrite('bin/color.jpg',image)
TypeError: img is not a numerical tuple
我已经完成了所有正确的事情,我没有更改任何代码,当在单独的程序中执行时,它没有显示任何错误,但是当在我的代码中运行时,它显示错误。我复制的代码来自这个链接
您在复制时更改了代码。明显地,cam.read()
返回一个元组。从文档中:
Python: cv2.VideoCapture.read([image]) → retval, image
您正在返回整个元组retval
and image
,而该示例仅返回其第二部分(图像)。所以你的image
第 9 行中的变量包含由返回的完整元组read()
而该示例仅返回它的第二部分。imwrite
然后失败,因为它不期望元组作为参数。
尝试像这样更改您的代码:
def get_image():
_, cap = cam.read()
return cap
或者,甚至更好,
def get_image():
return cam.read()[1]
此外,您还拼写错误了变量ramp_frames
as ram_frames
在第 2 行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)