gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:31:35 25 4
gpt4 key购买 nike

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

完整的代码可以在这里找到: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com