Python &引用;类型错误:';非类型';对象不可下标
我正试图为这段代码添加一些功能,我可以捕获我需要的任何palm并将其保存在文件夹中,第一次尝试成功,但第二次失败说:Python &引用;类型错误:';非类型';对象不可下标,python,opencv,numpy,raspberry-pi,palm,Python,Opencv,Numpy,Raspberry Pi,Palm,我正试图为这段代码添加一些功能,我可以捕获我需要的任何palm并将其保存在文件夹中,第一次尝试成功,但第二次失败说: Traceback (most recent call last): File "home/pi/Downloads/palmdetect.py", line 97, in<module> camera_capture = get_image() File "home/pi/Downloads/palmdetect.py", line 11, in get_i
Traceback (most recent call last):
File "home/pi/Downloads/palmdetect.py", line 97, in<module>
camera_capture = get_image()
File "home/pi/Downloads/palmdetect.py", line 11, in get_image
crop_image = img[100:450, 100:450]
TypeError: 'NoneType' object is not subscriptable
任何人都知道如何修复它,而且阈值窗口不会出现
在一个调用中组合VideoCapture::grab()和VideoCapture::retrieve()
和VideoCapture::retrieve()
解码并返回刚刚抓取的帧。如果未安装任何帧
抓取(相机已断开连接,或图像中没有更多帧)
方法返回false,函数返回NULL
指针
在python中翻译时,您必须期望retvar
和frame
,其中frame
不是None
,只有当retval
为True
时
在代码行中:
# read image
ret, img = cap.read()
可能会将img设置为“无”,因此当您尝试在此处进行裁剪时:
crop_img = img[100:300, 100:300]
您将得到您的错误:
TypeError:“非类型”对象不可下标
您需要首先测试retval:
retval, img = cap.read()
if retval:
# get hand data from the rectangle sub window on the screen
cv2.rectangle(img, (300, 300), (100, 100), (0, 255, 0), 0)
crop_img = img[100:300, 100:300]
...
或对img进行测试,而不是一开始就没有:
# Since we don't use the return value let's just forget it
_, img = cap.read()
if img is not None:
# get hand data from the rectangle sub window on the screen
cv2.rectangle(img, (300, 300), (100, 100), (0, 255, 0), 0)
crop_img = img[100:300, 100:300]
...
然后,您必须处理get_image函数get no image(返回None或引发异常)这一事实
旁注:您的代码仍然有两个问题,在函数末尾有一行
all_img=np.hstack((绘图,裁剪img))
,但您从未使用所有的img。在模块末尾,您执行del(摄像头)
但摄像头从未定义。最后,如果您必须公开共享您的代码,请遵循(安装或仅在您的编辑器或选项中安装)。堆栈跟踪是什么?您调试过吗?请给出完整的错误消息。请共享完整的回溯。您是否搜索了错误消息?您没有包含回溯,但有一个None
somewhere请提供。另外,请参阅
# Since we don't use the return value let's just forget it
_, img = cap.read()
if img is not None:
# get hand data from the rectangle sub window on the screen
cv2.rectangle(img, (300, 300), (100, 100), (0, 255, 0), 0)
crop_img = img[100:300, 100:300]
...