Skip to content

Commit

Permalink
chore: improve validations
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoFernandezC committed Oct 7, 2022
1 parent fca3f1a commit b73f948
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion draw_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

frame = cv2.imread("images/city_gk_vs_referee.png")

counter_background = match.get_possession_backround()
counter_background = match.get_possession_background()

# Players
player_detections = get_player_detections(person_detector, frame)
Expand Down
4 changes: 2 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
path = AbsolutePath()

# Get Counter img
possession_background = match.get_possession_backround()
passes_background = match.get_passes_backround()
possession_background = match.get_possession_background()
passes_background = match.get_passes_background()

for i, frame in enumerate(video):

Expand Down
12 changes: 6 additions & 6 deletions soccer/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,16 @@ def passes_bar(self, frame: PIL.Image.Image, origin: tuple) -> PIL.Image.Image:

return frame

def get_possession_backround(
def get_possession_background(
self,
) -> PIL.Image.Image:
"""
Get possession counter backround
Get possession counter background
Returns
-------
PIL.Image.Image
Counter backround
Counter background
"""

counter = PIL.Image.open("./images/possession_board.png").convert("RGBA")
Expand All @@ -408,14 +408,14 @@ def get_possession_backround(
counter = counter.resize((int(315 * 1.2), int(210 * 1.2)))
return counter

def get_passes_backround(self) -> PIL.Image.Image:
def get_passes_background(self) -> PIL.Image.Image:
"""
Get passes counter backround
Get passes counter background
Returns
-------
PIL.Image.Image
Counter backround
Counter background
"""

counter = PIL.Image.open("./images/passes_board.png").convert("RGBA")
Expand Down
41 changes: 12 additions & 29 deletions soccer/pass_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,54 +117,37 @@ def update(self, closest_player: Player, ball: Ball) -> None:
self.ball = ball
self.closest_player = closest_player

init_player = self.init_player_with_ball

init_player_has_id = init_player and "id" in init_player.detection.data
closest_player_has_id = closest_player and "id" in closest_player.detection.data

same_id = (
init_player_has_id
and closest_player_has_id
and (init_player == closest_player)
)

different_id = (
init_player_has_id
and closest_player_has_id
and not (init_player == closest_player)
)
same_id = Player.have_same_id(self.init_player_with_ball, closest_player)

if same_id:
self.player_with_ball_counter += 1
elif different_id:
elif not same_id:
self.player_with_ball_counter = 0

self.init_player_with_ball = closest_player

def validate_pass(self, posible_start: Player, posible_end: Player) -> bool:
def validate_pass(self, start_player: Player, end_player: Player) -> bool:
"""
Check if there is a pass between two players of the same team
Parameters
----------
posible_start : Player
start_player : Player
Player that originates the pass
posible_end : Player
end_player : Player
Destination player of the pass
Returns
-------
bool
Valid pass occurred
"""
last_player_has_id = posible_start and "id" in posible_start.detection.data
closest_player_has_id = posible_end and "id" in posible_end.detection.data
players_id = last_player_has_id and closest_player_has_id

different_player = players_id and not (posible_start == posible_end)
same_team = posible_start.team == posible_end.team
if Player.have_same_id(start_player, end_player):
return False
if start_player.team != end_player.team:
return False

return different_player and same_team
return True

def generate_pass(
self, team: Team, start_pass: np.ndarray, end_pass: np.ndarray
Expand Down Expand Up @@ -206,8 +189,8 @@ def process_pass(self) -> None:
self.last_player_with_ball = self.init_player_with_ball

valid_pass = self.validate_pass(
posible_start=self.last_player_with_ball,
posible_end=self.closest_player,
start_player=self.last_player_with_ball,
end_player=self.closest_player,
)

if valid_pass:
Expand Down
23 changes: 23 additions & 0 deletions soccer/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ def __eq__(self, other: "Player") -> bool:

return self_id == other_id

@staticmethod
def have_same_id(player1: "Player", player2: "Player") -> bool:
"""
Check if player1 and player2 have the same ids
Parameters
----------
player1 : Player
One player
player2 : Player
Another player
Returns
-------
bool
True if they have the same id
"""
if not player1 or not player2:
return False
if "id" not in player1.detection.data or "id" not in player2.detection.data:
return False
return player1 == player2

@staticmethod
def draw_players(
players: List["Player"],
Expand Down

0 comments on commit b73f948

Please sign in to comment.