-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathshot_detector.py
153 lines (117 loc) · 5.66 KB
/
shot_detector.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Avi Shah - Basketball Shot Detector/Tracker - July 2023
from ultralytics import YOLO
import cv2
import cvzone
import math
import numpy as np
from utils import score, detect_down, detect_up, in_hoop_region, clean_hoop_pos, clean_ball_pos
class ShotDetector:
def __init__(self):
# Load the YOLO model created from main.py - change text to your relative path
self.model = YOLO("best.pt")
self.class_names = ['Basketball', 'Basketball Hoop']
# Uncomment line below to use webcam (I streamed to my iPhone using Iriun Webcam)
# self.cap = cv2.VideoCapture(0)
# Use video - replace text with your video path
self.cap = cv2.VideoCapture("video_test_5.mp4")
self.ball_pos = [] # array of tuples ((x_pos, y_pos), frame count, width, height, conf)
self.hoop_pos = [] # array of tuples ((x_pos, y_pos), frame count, width, height, conf)
self.frame_count = 0
self.frame = None
self.makes = 0
self.attempts = 0
# Used to detect shots (upper and lower region)
self.up = False
self.down = False
self.up_frame = 0
self.down_frame = 0
# Used for green and red colors after make/miss
self.fade_frames = 20
self.fade_counter = 0
self.overlay_color = (0, 0, 0)
self.run()
def run(self):
while True:
ret, self.frame = self.cap.read()
if not ret:
# End of the video or an error occurred
break
results = self.model(self.frame, stream=True)
for r in results:
boxes = r.boxes
for box in boxes:
# Bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
w, h = x2 - x1, y2 - y1
# Confidence
conf = math.ceil((box.conf[0] * 100)) / 100
# Class Name
cls = int(box.cls[0])
current_class = self.class_names[cls]
center = (int(x1 + w / 2), int(y1 + h / 2))
# Only create ball points if high confidence or near hoop
if (conf > .3 or (in_hoop_region(center, self.hoop_pos) and conf > 0.15)) and current_class == "Basketball":
self.ball_pos.append((center, self.frame_count, w, h, conf))
cvzone.cornerRect(self.frame, (x1, y1, w, h))
# Create hoop points if high confidence
if conf > .5 and current_class == "Basketball Hoop":
self.hoop_pos.append((center, self.frame_count, w, h, conf))
cvzone.cornerRect(self.frame, (x1, y1, w, h))
self.clean_motion()
self.shot_detection()
self.display_score()
self.frame_count += 1
cv2.imshow('Frame', self.frame)
# Close if 'q' is clicked
if cv2.waitKey(1) & 0xFF == ord('q'): # higher waitKey slows video down, use 1 for webcam
break
self.cap.release()
cv2.destroyAllWindows()
def clean_motion(self):
# Clean and display ball motion
self.ball_pos = clean_ball_pos(self.ball_pos, self.frame_count)
for i in range(0, len(self.ball_pos)):
cv2.circle(self.frame, self.ball_pos[i][0], 2, (0, 0, 255), 2)
# Clean hoop motion and display current hoop center
if len(self.hoop_pos) > 1:
self.hoop_pos = clean_hoop_pos(self.hoop_pos)
cv2.circle(self.frame, self.hoop_pos[-1][0], 2, (128, 128, 0), 2)
def shot_detection(self):
if len(self.hoop_pos) > 0 and len(self.ball_pos) > 0:
# Detecting when ball is in 'up' and 'down' area - ball can only be in 'down' area after it is in 'up'
if not self.up:
self.up = detect_up(self.ball_pos, self.hoop_pos)
if self.up:
self.up_frame = self.ball_pos[-1][1]
if self.up and not self.down:
self.down = detect_down(self.ball_pos, self.hoop_pos)
if self.down:
self.down_frame = self.ball_pos[-1][1]
# If ball goes from 'up' area to 'down' area in that order, increase attempt and reset
if self.frame_count % 10 == 0:
if self.up and self.down and self.up_frame < self.down_frame:
self.attempts += 1
self.up = False
self.down = False
# If it is a make, put a green overlay
if score(self.ball_pos, self.hoop_pos):
self.makes += 1
self.overlay_color = (0, 255, 0)
self.fade_counter = self.fade_frames
# If it is a miss, put a red overlay
else:
self.overlay_color = (0, 0, 255)
self.fade_counter = self.fade_frames
def display_score(self):
# Add text
text = str(self.makes) + " / " + str(self.attempts)
cv2.putText(self.frame, text, (50, 125), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 255, 255), 6)
cv2.putText(self.frame, text, (50, 125), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
# Gradually fade out color after shot
if self.fade_counter > 0:
alpha = 0.2 * (self.fade_counter / self.fade_frames)
self.frame = cv2.addWeighted(self.frame, 1 - alpha, np.full_like(self.frame, self.overlay_color), alpha, 0)
self.fade_counter -= 1
if __name__ == "__main__":
ShotDetector()