Devshades
Devshades

Reputation: 3

How to fix OpenCV Numpy Error, object is not iterable

I'm following a guide to develop a motion detection security feed and have run into an issue. The author of the tutorial had no issues, but I do.

'''The full code can be found here: https://github.com/ncorbuk/Python-Motion-Detection-system/blob/master/motion_detection.py '''

I have already tried the option of adding,

" for c in cnt or []: " - This got me further into running the application where I could see the camera screens, however as soon as motion is detected the application crashes and provides the following numpy error:

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

The expected output, if you have a webcam; When motion is detected a green rectangle will follow the moving pixels and change the mode from Unoccupied to Occupied, in real time; with no errors.

Upvotes: 0

Views: 1345

Answers (1)

Ha Bom
Ha Bom

Reputation: 2927

In Opencv4.0, findContour() returns only 2 values, contours and hierachy. So in the line 57 in motion_detection.py you have to change

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

to

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

Upvotes: 1

Related Questions