I started learning Python and Deep learning a week back as I wanted to help my daughter analyze her cricket playing skills. I am using the following code from the web as my starting point as that's the closest code which I can refer.. Here is the link of the code.. https://www.analyticsvidhya.com/blog/2020/03/ball-tracking-cricket-computer-vision/
While I was able to understand and execute the code till the last step, however I got stuck with the following part of the code and I am unable to proceed since last 2 days, this is after doing all possible research at my end.
Following is the part of the code, The code line x,y,w,h = cv2.boundingRect(countours[i])
is giving an error.
!rm -r ball/*
ball_df = pd.DataFrame(columns=['frame','x','y','w','h'])
for idx in range(len(frames)):
img= cv2.imread('FFrames/' + frames[idx])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(25, 25),0)
_ , mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
image, contours = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
num=20
cnt=0
df = pd.DataFrame(columns=['frame','x','y','w','h'])
for i in range(len(contours)):
x,y,w,h = cv2.boundingRect(contours[i])
numer=min([w,h])
denom=max([w,h])
ratio=numer/denom
if(x>=num and y>=num):
xmin=x-num
ymin=y-num
xmax=x+w+num
ymax=y+h+num
else:
xmin=x
ymin=y
xmax=x+w
ymax=y+h
if(ratio>=0.5):
#print(cnt,x,y,w,h,ratio)
df.loc[cnt,'frame'] = frames[idx]
df.loc[cnt,'x']=x
df.loc[cnt,'y']=y
df.loc[cnt,'w']=w
df.loc[cnt,'h']=h
cv2.imwrite("patch/"+str(cnt)+".png",img[ymin:ymax,xmin:xmax])
cnt=cnt+1
Error:
cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:784: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'