执行下面的代码我得到这个'TypeError:img不是一个数字元组' [英] While executing the below code i'm getting this 'TypeError: img is not a numerical tuple'
问题描述
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
我做了一切正确我没有改变任何代码在执行时单独的程序它没有显示任何错误,但在我的代码中运行它显示错误。我复制的代码来自此链接
I have done everything right i didn't change anything the code which when executed in a seperate program it's not showing any error but when run inside my code it's showing error. The code i copied is from this link
推荐答案
您在复制时更改了代码。显然, cam.read()
返回一个元组。来自文档:
You have changed the code while copying. Obviously, cam.read()
returns a tuple. From the documentation:
Python: cv2.VideoCapture.read([image]) → retval, image
您将返回 retval
和的整个元组> image
,而该示例仅返回它的第二部分(图像)。因此,第9行中的 image
变量包含 read()
返回的完整元组,而示例仅返回第二部分。 imwrite
然后失败,因为它不希望元组作为参数。
You are returning the whole tuple of retval
and image
, while the example only returns the second part of it (the image). So your image
variable in line 9 contains the complete tuple that is returned by read()
while the example only returns the second part of it. imwrite
then fails because it does not expect a tuple as argument.
尝试更改你的代码:
def get_image():
_, cap = cam.read()
return cap
或者更好,
def get_image():
return cam.read()[1]
此外,您在第2行中将变量 ramp_frames
拼错为 ram_frames
。
Additionally, you have misspelled the variable ramp_frames
as ram_frames
in line 2.
这篇关于执行下面的代码我得到这个'TypeError:img不是一个数字元组'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!