python - 如何修复OpenCV Numpy错误,对象不可迭代

标签 python opencv typeerror nonetype

我正在遵循开发运动检测安全提要的指南,但遇到了问题。教程的作者没有问题,但是我有。

完整的代码可以在这里找到:https://github.com/ncorbuk/Python-Motion-Detection-system/blob/master/motion_detection.py
'''

我已经尝试过添加

“对于cnt或[]:”-“这使我进一步运行了可以看到摄像机屏幕的应用程序,但是,一旦检测到运动,应用程序就会崩溃并提供以下numpy错误:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


for c in cnt:
        if (cv2.contourArea(c) > 800): 
            (x, y, w, h) = cv2.boundingRect(c)

            cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2) 

            text = 'Occupied'

        else:
            pass

预期的输出(如果有网络摄像头);
当检测到运动时,绿色矩形将跟随运动的像素并实时将模式从“未占用”更改为“已占用”;没有错误。

最佳答案

在Opencv4.0中,findContour()仅返回2个值contourshierachy。因此,在motion_detection.py的第57行中,您必须进行更改
cnt = cv2.findContours(dilate_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]

cnt = cv2.findContours(dilate_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

关于python - 如何修复OpenCV Numpy错误,对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55348047/

相关文章:

Python3 : Write csv directly to zipfile gives TypeError

ruby - 具有真值的案例陈述

python - 在 OpenCV 中从带有孔的掩模侧面腐 eclipse x 像素

python - fatal error : 'QTKit/QTKit.h' file not found when I build OpenCV on mac

python - 类型错误:不支持解码 str

python - Python 中的 C _IOR 函数等效于什么?

python - 在 Python 中,如何将 3D 等值面导出到 Blender

python - 使用 Oauth 对 Gmail 进行身份验证后,有没有办法获取您的电子邮件地址?

python - 加载索引具有冗余列的 parquet 文件时出现 pyarrow 问题

c++ - 在 VS2012 项目上编译 Boost C++ 库