0

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.

2
  • What version of OpenCV are you using? Commented Mar 26, 2019 at 0:42
  • @AlexanderReynolds I'm using opencv-python==4.0.0.21 Commented Mar 26, 2019 at 0:52

1 Answer 1

1

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].

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @Ha Bom, your answer has solved my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.