-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroulette.py
236 lines (204 loc) · 7.2 KB
/
roulette.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import cv2
import numpy as np
from ultralytics import YOLO
class VideoProcessor:
def __init__(self, video_path, output_path, trackers):
self.video_path = video_path
self.output_path = output_path
self.trackers = trackers
self.cap = cv2.VideoCapture(self.video_path)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
self.out = cv2.VideoWriter(
self.output_path, fourcc, 30.0, (int(self.cap.get(3)), int(self.cap.get(4)))
)
def process_video(self):
if not self.cap.isOpened():
print("Error: Could not open video.")
return
while self.cap.isOpened():
ret, frame = self.cap.read()
if ret:
for tracker in self.trackers:
tracker.process_frame(frame)
self.out.write(frame)
cv2.imshow("Processed Frame", frame)
if cv2.waitKey(25) & 0xFF == ord("q"):
break
else:
break
self.cap.release()
self.out.release()
cv2.destroyAllWindows()
class RouletteWheelTracker:
def __init__(
self,
center_adjust_x=20, # adjust automatically detected center if needed
center_adjust_y=-20,
distance_adjustment=30, # distance between numbers drawn and the roulette's center
fraction=1 / 100, # number spacing
):
self.center_adjust_x = center_adjust_x
self.center_adjust_y = center_adjust_y
self.distance_adjustment = distance_adjustment
self.fraction = fraction
self.roulette_numbers = [
0,
32,
15,
19,
4,
21,
2,
25,
17,
34,
6,
27,
13,
36,
11,
30,
8,
23,
10,
5,
24,
16,
33,
1,
20,
14,
31,
9,
22,
18,
29,
7,
28,
12,
35,
3,
26,
]
self.lower_green = np.array([40, 40, 40])
self.upper_green = np.array([80, 255, 255])
self.fixed_distance_to_center = None
self.default_angle_per_slot = (2 * np.pi) / 37
self.spacing_adjustment = self.default_angle_per_slot * self.fraction
def process_frame(self, frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, self.lower_green, self.upper_green)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 100]
if contours:
self.process_contours(contours, frame)
def process_contours(self, contours, frame):
max_contour = max(contours, key=cv2.contourArea)
M = cv2.moments(max_contour)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
self.draw_wheel_center(frame, cx, cy)
self.draw_numbers(frame, cx, cy)
def draw_wheel_center(self, frame, cx, cy):
wheel_center_x = frame.shape[1] // 2 + self.center_adjust_x
wheel_center_y = frame.shape[0] // 2 + self.center_adjust_y
cv2.circle(frame, (wheel_center_x, wheel_center_y), 5, (0, 255, 0), -1)
if self.fixed_distance_to_center is None:
self.fixed_distance_to_center = (
np.sqrt((cx - wheel_center_x) ** 2 + (cy - wheel_center_y) ** 2)
- self.distance_adjustment
)
def draw_numbers(self, frame, cx, cy):
wheel_center_x = frame.shape[1] // 2 + self.center_adjust_x
wheel_center_y = frame.shape[0] // 2 + self.center_adjust_y
starting_angle = np.arctan2(cy - wheel_center_y, cx - wheel_center_x)
angle_per_slot = self.default_angle_per_slot + self.spacing_adjustment
for i, number in enumerate(self.roulette_numbers):
angle = starting_angle + i * angle_per_slot
num_x = int(wheel_center_x + np.cos(angle) * self.fixed_distance_to_center)
num_y = int(wheel_center_y + np.sin(angle) * self.fixed_distance_to_center)
cv2.putText(
frame,
str(number),
(num_x, num_y),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 0, 0),
2,
)
class RouletteBallTracker:
def __init__(
self,
model_path,
threshold=0.8,
show_bb=False,
display_score=False,
track_trajectory=True,
max_trajectory_length=50,
):
self.model = YOLO(model_path)
self.threshold = threshold
self.show_bb = show_bb
self.display_score = display_score
self.track_trajectory = track_trajectory
self.max_trajectory_length = max_trajectory_length
self.trajectory = []
def process_frame(self, frame):
results = self.model(frame)[0]
# Find the detection with the highest score above the threshold
best_score = self.threshold
best_result = None
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if score > best_score:
best_score = score
best_result = result
# Process only the best result
if best_result:
x1, y1, x2, y2, score, class_id = best_result
ball_position = ((x1 + x2) // 2, (y1 + y2) // 2)
self.update_trajectory(ball_position)
label = results.names[int(class_id)].upper()
if self.display_score:
label = f"{label}: {score:.2f}"
if self.show_bb:
cv2.rectangle(
frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4
)
cv2.putText(
frame,
label,
(int(x1), int(y1 - 10)),
cv2.FONT_HERSHEY_SIMPLEX,
1.3,
(0, 255, 0),
3,
cv2.LINE_AA,
)
if self.track_trajectory:
self.draw_trajectory(frame)
def update_trajectory(self, position):
int_position = (int(position[0]), int(position[1]))
self.trajectory.append(int_position)
if len(self.trajectory) > self.max_trajectory_length:
self.trajectory.pop(0)
def draw_trajectory(self, frame):
for i in range(1, len(self.trajectory)):
cv2.line(
frame, self.trajectory[i - 1], self.trajectory[i], (0, 255, 255), 2
)
if __name__ == "__main__":
VIDEO_PATH = "videos/roulette_test.mp4"
OUTPUT_PATH = "output_video.mp4"
roulette_tracker = RouletteWheelTracker()
yolo_tracker = RouletteBallTracker(
model_path="runs/detect/train2/weights/last.pt",
threshold=0.5,
display_score=True,
track_trajectory=False,
)
video_processor = VideoProcessor(
VIDEO_PATH, OUTPUT_PATH, [roulette_tracker, yolo_tracker]
)
video_processor.process_video()