Click here to Skip to main content
15,309,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a motion detector but at last when i tying to counter the boject it gives me a Type Error, I don,t know much about open cv, I tried to find the solution on in ternet but there was no solution to this

The code is as follows:

import cv2

vedio = cv2.VideoCapture(0)

first_frame = None

while True:
check , frame = vedio.read()

gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

gray = cv2.GaussianBlur(gray,(21,21),0)


if first_frame is None:
first_frame=gray
continue


new_frame = cv2.absdiff(first_frame,gray)

thresh_frame = cv2.threshold(new_frame, 30 ,255, cv2.THRESH_BINARY)[1]

thresh_frame = cv2.dilate(thresh_frame,None,iterations = 2)


(__,cnts__,) = cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

for contour in (__,cnts__,):
if cv2.contourArea(contour) < 1000:
continue


(x,y,w,h) = cv2.boundingRect(contour)
cv2.rectangle(frame, (x,y), (x + w+ y + h), (0,255,0),3)





cv2.imshow("thresh",thresh_frame)

cv2.imshow("Capture",gray)



key = cv2.waitKey(1)


if key == ord('g'):
break



print(a)
vedio.release()
cv2.destroyAllWindows()


The error i get is :
Traceback (most recent call last):
File "E:\Python\Python\open cv\vedio.py", line 30, in <module>
if cv2.contourArea(contour) < 1000:
TypeError: Expected cv::UMat for argument 'contour'
[ WARN:0] terminating async callback

What I have tried:

I tried stackoverflow but there was no proper answer for this..
Posted
Updated 14-Jan-19 0:42am

[update]
Quote:
(__,cnts__,) = cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

for contour in (__,cnts__,):
if cv2.contourArea(contour) < 1000:

I would have written, instead
__,cnts,__ = cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

for contour in cnts:
if cv2.contourArea(contour) < 1000:

[/update]
   
v2
Comments
Member 14068174 14-Jan-19 6:28am
   
It gives me the error that cnts is not defined
CPallini 14-Jan-19 6:47am
   
See, my updated solution.
It seems the return values from findContours may be 2 or 3 variables; see cv2.findContours Python Example[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900