-
Notifications
You must be signed in to change notification settings - Fork 53
/
run.py
165 lines (135 loc) · 4.29 KB
/
run.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
import argparse
import cv2
import numpy as np
import PIL
from norfair import Tracker, Video
from norfair.camera_motion import MotionEstimator
from norfair.distances import mean_euclidean
from inference import Converter, HSVClassifier, InertiaClassifier, YoloV5
from inference.filters import filters
from run_utils import (
get_ball_detections,
get_main_ball,
get_player_detections,
update_motion_estimator,
)
from soccer import Match, Player, Team
from soccer.draw import AbsolutePath
from soccer.pass_event import Pass
parser = argparse.ArgumentParser()
parser.add_argument(
"--video",
default="videos/soccer_possession.mp4",
type=str,
help="Path to the input video",
)
parser.add_argument(
"--model", default="models/ball.pt", type=str, help="Path to the model"
)
parser.add_argument(
"--passes",
action="store_true",
help="Enable pass detection",
)
parser.add_argument(
"--possession",
action="store_true",
help="Enable possession counter",
)
args = parser.parse_args()
video = Video(input_path=args.video)
fps = video.video_capture.get(cv2.CAP_PROP_FPS)
# Object Detectors
player_detector = YoloV5()
ball_detector = YoloV5(model_path=args.model)
# HSV Classifier
hsv_classifier = HSVClassifier(filters=filters)
# Add inertia to classifier
classifier = InertiaClassifier(classifier=hsv_classifier, inertia=20)
# Teams and Match
chelsea = Team(
name="Chelsea",
abbreviation="CHE",
color=(255, 0, 0),
board_color=(244, 86, 64),
text_color=(255, 255, 255),
)
man_city = Team(name="Man City", abbreviation="MNC", color=(240, 230, 188))
teams = [chelsea, man_city]
match = Match(home=chelsea, away=man_city, fps=fps)
match.team_possession = man_city
# Tracking
player_tracker = Tracker(
distance_function=mean_euclidean,
distance_threshold=250,
initialization_delay=3,
hit_counter_max=90,
)
ball_tracker = Tracker(
distance_function=mean_euclidean,
distance_threshold=150,
initialization_delay=20,
hit_counter_max=2000,
)
motion_estimator = MotionEstimator()
coord_transformations = None
# Paths
path = AbsolutePath()
# Get Counter img
possession_background = match.get_possession_background()
passes_background = match.get_passes_background()
for i, frame in enumerate(video):
# Get Detections
players_detections = get_player_detections(player_detector, frame)
ball_detections = get_ball_detections(ball_detector, frame)
detections = ball_detections + players_detections
# Update trackers
coord_transformations = update_motion_estimator(
motion_estimator=motion_estimator,
detections=detections,
frame=frame,
)
player_track_objects = player_tracker.update(
detections=players_detections, coord_transformations=coord_transformations
)
ball_track_objects = ball_tracker.update(
detections=ball_detections, coord_transformations=coord_transformations
)
player_detections = Converter.TrackedObjects_to_Detections(player_track_objects)
ball_detections = Converter.TrackedObjects_to_Detections(ball_track_objects)
player_detections = classifier.predict_from_detections(
detections=player_detections,
img=frame,
)
# Match update
ball = get_main_ball(ball_detections)
players = Player.from_detections(detections=players_detections, teams=teams)
match.update(players, ball)
# Draw
frame = PIL.Image.fromarray(frame)
if args.possession:
frame = Player.draw_players(
players=players, frame=frame, confidence=False, id=True
)
frame = path.draw(
img=frame,
detection=ball.detection,
coord_transformations=coord_transformations,
color=match.team_possession.color,
)
frame = match.draw_possession_counter(
frame, counter_background=possession_background, debug=False
)
if ball:
frame = ball.draw(frame)
if args.passes:
pass_list = match.passes
frame = Pass.draw_pass_list(
img=frame, passes=pass_list, coord_transformations=coord_transformations
)
frame = match.draw_passes_counter(
frame, counter_background=passes_background, debug=False
)
frame = np.array(frame)
# Write video
video.write(frame)