forked from tryolabs/soccer-video-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
116 lines (90 loc) · 2.49 KB
/
ball.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
import cv2
import norfair
import numpy as np
from draw import Draw
class Ball:
def __init__(self, detection: norfair.Detection):
"""
Initialize Ball
Parameters
----------
detection : norfair.Detection
norfair.Detection containing the ball
"""
self.detection = detection
self.color = None
def set_color(self, match: "Match"):
"""
Sets the color of the ball to the team color with the ball possession in the match.
Parameters
----------
match : Match
Match object
"""
if match.team_possession is None:
return
self.color = match.team_possession.color
if self.detection:
self.detection.data["color"] = match.team_possession.color
def get_center(self, points: np.array) -> tuple:
"""
Returns the center of the points
Parameters
----------
points : np.array
2D points
Returns
-------
tuple
(x, y) coordinates of the center
"""
x1, y1 = points[0]
x2, y2 = points[1]
center_x = (x1 + x2) / 2
center_y = (y1 + y2) / 2
return (center_x, center_y)
@property
def center(self) -> tuple:
"""
Returns the center of the ball
Returns
-------
tuple
Center of the ball (x, y)
"""
if self.detection is None:
return None
center = self.get_center(self.detection.points)
round_center = np.round_(center)
return round_center
@property
def center_abs(self) -> tuple:
"""
Returns the center of the ball in absolute coordinates
Returns
-------
tuple
Center of the ball (x, y)
"""
if self.detection is None:
return None
center = self.get_center(self.detection.absolute_points)
round_center = np.round_(center)
return round_center
def draw(self, frame: np.ndarray) -> np.ndarray:
"""
Draw the ball on the frame
Parameters
----------
frame : np.ndarray
Frame to draw on
Returns
-------
np.ndarray
Frame with ball drawn
"""
if self.detection is None:
return frame
return Draw.draw_detection(self.detection, frame)
def __str__(self):
return f"Ball: {self.center}"