-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
37 lines (29 loc) · 986 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
import cv2
# import PIL
# import PIL.Image
cap = cv2.VideoCapture(0)
CAPTURE = r"resources\large_image.png"
BALL = r"resources\small_image.png"
def find(large_image, small_image, method = cv2.TM_SQDIFF_NORMED):
small_image = cv2.imread(BALL)
large_image = cv2.imread(CAPTURE)
result = cv2.matchTemplate(small_image, large_image, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = small_image.shape[:2]
return [MPx, MPy, MPx+tcols, MPy+trows]
font = cv2.FONT_HERSHEY_SIMPLEX
while(True):
ret, frame = cap.read()
cv2.imwrite(CAPTURE, frame)
coords = find(CAPTURE, BALL)
x = coords[0]
y = coords[1]
cv2.rectangle(frame, (x,y),(coords[2],coords[3]),(0,0,255),2)
cv2.putText(frame, f"x: {x}, y: {y}",(10,30), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('Tennis Ball Tracker', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()