-
Notifications
You must be signed in to change notification settings - Fork 1
/
hit_detection.py
45 lines (34 loc) · 1.11 KB
/
hit_detection.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
38
39
40
41
42
43
44
45
import cv2
import numpy as np
import board
import math
def hit_detect():
lower_bound = np.array([0,0,100])
upper_bound = np.array([80,80,255])
cap = cv2.VideoCapture(1)
while True:
ret, frame = cap.read()
if not ret:
print("Error reading Image")
break
# Perform color thresholding to isolate the circle
mask = cv2.inRange(frame, lower_bound, upper_bound)
contour_img, bounding_boxs, centers, radius = board.Contours(frame)
if len(centers)<=2:
continue
elif len(centers)==4:
centers.sort(reverse=True)
radius.sort(reverse=True)
x1 = centers[0][0]
y1 = centers[0][1]
x2 = centers[2][0]
y2 = centers[2][1]
distance = math.sqrt((x2-x1)**2 + (y2-y1)**2)
if distance<radius[0]:
return "Hit"
else:
return "Miss"
else:
print("There are more than 2 contours found....Check kar")
if cv2.waitKey(1) & 0xFF == ord('q'):
break