Skip to content

Instantly share code, notes, and snippets.

@aravindpai
Last active December 6, 2022 10:50
Show Gist options
  • Save aravindpai/bd4a8396e4b2a946e3cfe9ec82eeb935 to your computer and use it in GitHub Desktop.
Save aravindpai/bd4a8396e4b2a946e3cfe9ec82eeb935 to your computer and use it in GitHub Desktop.
!rm -r ball/*
ball_df = pd.DataFrame(columns=['frame','x','y','w','h'])
for idx in range(len(frames)):
img= cv2.imread('frames/' + 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)
!rm -r patch/*
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, ymin= x-num, y-num
xmax, ymax= x+w+num, y+h+num
else:
xmin, ymin= x,y
xmax, ymax= x+w, 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
files=os.listdir('patch/')
if(len(files)>0):
files.sort(key=lambda f: int(re.sub('\D', '', f)))
test=[]
for file in files:
img=cv2.imread('patch/'+file,0)
img=cv2.resize(img,(25,25))
test.append(img)
test = np.array(test)
test = test.reshape(len(test),-1)
y_pred = rfc.predict(test)
prob=rfc.predict_proba(test)
if 0 in y_pred:
ind = np.where(y_pred==0)[0]
proba = prob[:,0]
confidence = proba[ind]
confidence = [i for i in confidence if i>0.7]
if(len(confidence)>0):
maximum = max(confidence)
ball_file=files[list(proba).index(maximum)]
img= cv2.imread('patch/'+ball_file)
cv2.imwrite('ball/'+str(frames[idx]),img)
no = int(ball_file.split(".")[0])
ball_df.loc[idx]= df.loc[no]
else:
ball_df.loc[idx,'frame']=frames[idx]
else:
ball_df.loc[idx,'frame']=frames[idx]
@Shorya-99
Copy link

!rm -r patch/* what is the meaning of this expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment