File size: 11,669 Bytes
8b35e63
 
 
 
 
 
 
 
741db91
8b35e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
741db91
 
8b35e63
741db91
 
 
 
8b35e63
741db91
 
 
 
 
 
 
 
 
8b35e63
741db91
8b35e63
741db91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b35e63
 
 
 
 
741db91
8b35e63
 
 
 
 
 
 
 
741db91
 
 
 
 
 
 
8b35e63
741db91
 
 
 
 
 
 
 
8b35e63
 
 
741db91
 
 
 
 
 
 
 
8b35e63
 
 
 
741db91
 
 
 
 
8b35e63
741db91
 
 
8b35e63
741db91
8b35e63
741db91
8b35e63
741db91
8b35e63
741db91
8b35e63
741db91
 
 
 
 
 
8b35e63
 
 
 
 
 
741db91
8b35e63
741db91
8b35e63
 
 
 
741db91
8b35e63
741db91
 
 
8b35e63
 
741db91
 
 
8b35e63
 
741db91
 
 
 
8b35e63
741db91
 
 
 
8b35e63
 
 
741db91
8b35e63
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from pathlib import Path
from ultralytics import YOLO
from numpy import ndarray
from pydantic import BaseModel
from typing import List, Tuple, Optional
import numpy as np
import cv2
from sklearn.cluster import KMeans



########################################
# Helper utilities for grass & color clustering
########################################

def get_grass_color(img: np.ndarray) -> Tuple[int, int, int]:
    """Estimate dominant green (grass) color from the image in BGR."""
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    lower_green = np.array([30, 40, 40])
    upper_green = np.array([80, 255, 255])
    mask = cv2.inRange(hsv, lower_green, upper_green)
    grass_color = cv2.mean(img, mask=mask)
    return grass_color[:3]


def get_players_boxes(result):
    """Extract player crops and boxes from YOLO result.
    
    Model class mapping:
    0: 'Player', 1: 'GoalKeeper', 2: 'Ball', 3: 'Main Referee', 
    4: 'Side Referee', 5: 'Staff Member', 6: 'left team', 7: 'right team'
    """
    players_imgs, players_boxes = [], []
    for box in result.boxes:
        label = int(box.cls.cpu().numpy()[0])
        if label == 0:  # Player class (cls_id=0 is Player)
            x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
            crop = result.orig_img[y1:y2, x1:x2]
            if crop.size > 0:
                players_imgs.append(crop)
                players_boxes.append((x1, y1, x2, y2))
    return players_imgs, players_boxes


def get_kits_colors(players, grass_hsv=None, frame=None):
    """Extract average kit colors from player crops."""
    kits_colors = []
    if grass_hsv is None:
        grass_color = get_grass_color(frame)
        grass_hsv = cv2.cvtColor(np.uint8([[list(grass_color)]]), cv2.COLOR_BGR2HSV)
    for player_img in players:
        hsv = cv2.cvtColor(player_img, cv2.COLOR_BGR2HSV)
        lower_green = np.array([grass_hsv[0, 0, 0] - 10, 40, 40])
        upper_green = np.array([grass_hsv[0, 0, 0] + 10, 255, 255])
        mask = cv2.inRange(hsv, lower_green, upper_green)
        mask = cv2.bitwise_not(mask)
        upper_mask = np.zeros(player_img.shape[:2], np.uint8)
        upper_mask[0:player_img.shape[0] // 2, :] = 255
        mask = cv2.bitwise_and(mask, upper_mask)
        kit_color = np.array(cv2.mean(player_img, mask=mask)[:3])
        kits_colors.append(kit_color)
    return kits_colors


########################################
# Data models
########################################

class BoundingBox(BaseModel):
    x1: int
    y1: int
    x2: int
    y2: int
    cls_id: int
    conf: float


class TVFrameResult(BaseModel):
    frame_id: int
    boxes: list[BoundingBox]
    keypoints: list[Tuple[int, int]]


########################################
# Main Miner class
########################################

class Miner:
    """
    Main class for sn44-compatible inference pipeline.
    Integrates YOLO + team color classification (HSV-based).
    """
    CORNER_INDICES = {0, 5, 24, 29}

    def __init__(
        self, 
        path_hf_repo: Path,
    ) -> None:
        """Load models from the repository.
        
        Model class mapping:
        0: 'Player', 1: 'GoalKeeper', 2: 'Ball', 3: 'Main Referee', 
        4: 'Side Referee', 5: 'Staff Member', 6: 'left team', 7: 'right team'
        
        Args:
            path_hf_repo: Path to HuggingFace repo with models
        """
        self.bbox_model = YOLO(path_hf_repo / "251110-football-detection.pt")
        print("✅ BBox Model Loaded")
        self.keypoints_model = YOLO(path_hf_repo / "17112025_keypoint.pt")
        print("✅ Keypoints Model (Pose) Loaded")

        self.team_kmeans = None
        self.left_team_label = 0
        self.grass_hsv = None
        self.team_classifier_fitted = False

    def __repr__(self) -> str:
        return (
            f"BBox Model: {type(self.bbox_model).__name__}\n"
            f"Keypoints Model: {type(self.keypoints_model).__name__}\n"
            f"Team Clustering: HSV + KMeans"
        )

    def fit_team_classifier(self, frame: np.ndarray) -> None:
        """Fit KMeans team classifier on the first frame."""
        result = self.bbox_model(frame, conf=0.2, verbose=False)[0]
        players_imgs, players_boxes = get_players_boxes(result)
        if len(players_imgs) == 0:
            print("⚠️ No players found for team fitting.")
            return

        kits_colors = get_kits_colors(players_imgs, frame=frame)
        
        # Check if we have enough samples before fitting KMeans
        if len(kits_colors) < 2:
            print(f"⚠️ Chỉ tìm thấy {len(kits_colors)} cầu thủ, không đủ để phân thành 2 đội. Bỏ qua việc fit.")
            return
        
        self.team_kmeans = KMeans(n_clusters=2, random_state=42)
        self.team_kmeans.fit(kits_colors)
        self.team_classifier_fitted = True
        print(f"✅ Team KMeans fitted on {len(kits_colors)} players")

        # Determine which team is on the left
        team_assignments = self.team_kmeans.predict(kits_colors)
        team_0_x = [players_boxes[i][0] for i, t in enumerate(team_assignments) if t == 0]
        team_1_x = [players_boxes[i][0] for i, t in enumerate(team_assignments) if t == 1]
        if len(team_0_x) and len(team_1_x):
            avg0, avg1 = np.mean(team_0_x), np.mean(team_1_x)
            self.left_team_label = 0 if avg0 < avg1 else 1
        print(f"🏳️ Left team label: {self.left_team_label}")

        grass_color = get_grass_color(frame)
        self.grass_hsv = cv2.cvtColor(np.uint8([[list(grass_color)]]), cv2.COLOR_BGR2HSV)

    def predict_batch(
        self,
        batch_images: list[ndarray],
        offset: int,
        n_keypoints: int,
    ) -> list[TVFrameResult]:
        """
        Run predictions and return structured results.
        
        Args:
            batch_images: List of image arrays (numpy)
            offset: Starting frame ID
            n_keypoints: Number of keypoints expected
        
        Returns:
            List of TVFrameResult
        """
        results: list[TVFrameResult] = []

        for i, frame in enumerate(batch_images):
            frame_id = offset + i

            # Fit KMeans on first frame if not done
            if not self.team_classifier_fitted:
                self.fit_team_classifier(frame)

            bbox_result = self.bbox_model(frame, conf=0.2, verbose=False)[0]
            boxes = []

            if bbox_result and bbox_result.boxes is not None:
                players_imgs, players_boxes = get_players_boxes(bbox_result)
                kits_colors = get_kits_colors(players_imgs, self.grass_hsv, frame)
                
                # Only predict team if team_kmeans is fitted and we have enough data
                if len(kits_colors) > 0 and self.team_kmeans is not None:
                    teams = self.team_kmeans.predict(kits_colors)
                else:
                    teams = []

                # Map player indices to team predictions
                player_indices = []  # Track which boxes are players
                for idx, box in enumerate(bbox_result.boxes):
                    cls_id = int(box.cls.cpu().numpy()[0])
                    if cls_id == 0:  # Player class (cls_id=0 is Player)
                        player_indices.append(idx)
                
                # Predict teams for players
                team_predictions = {}
                if len(player_indices) > 0 and len(teams) > 0:
                    for player_idx, team_id in zip(player_indices, teams):
                        # Map team_id (0,1) to cls_id (6,7) based on left_team_label
                        # cls_id 6 = team 1 (left team), cls_id 7 = team 2 (right team)
                        if team_id == self.left_team_label:
                            team_predictions[player_idx] = 6  # team 1
                        else:
                            team_predictions[player_idx] = 7  # team 2
                
                # Create boxes with correct cls_id mapping
                for idx, box in enumerate(bbox_result.boxes):
                    x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
                    conf = float(box.conf.cpu().numpy()[0])
                    cls_id = int(box.cls.cpu().numpy()[0])
                    
                    # Map YOLO model classes to validator's OBJECT_ID_LOOKUP format
                    # YOLO model: 0=Player, 1=GoalKeeper, 2=Ball, 3=Main Referee, 4=Side Referee, 5=Staff
                    # Validator expects: 0=ball, 1=goalkeeper, 2=player, 3=referee, 6=team1, 7=team2
                    
                    if idx in team_predictions:
                        # Player with team assignment
                        cls_id = team_predictions[idx]  # 6 or 7 for teams
                    elif cls_id == 0:  # YOLO Player -> Validator Player (2)
                        cls_id = 2
                    elif cls_id == 1:  # YOLO GoalKeeper -> Validator GoalKeeper (1)
                        cls_id = 1
                    elif cls_id == 2:  # YOLO Ball -> Validator Ball (0)
                        cls_id = 0
                    elif cls_id in [3, 4]:  # YOLO Main/Side Referee -> Validator Referee (3)
                        cls_id = 3
                    else:  # Staff or other -> skip
                        continue
                    
                    boxes.append(
                        BoundingBox(
                            x1=x1, y1=y1, x2=x2, y2=y2, cls_id=cls_id, conf=conf
                        )
                    )

            # -----------------------------------------
            # Keypoint detection using YOLO pose model
            # -----------------------------------------
            keypoints_result = self.keypoints_model(frame, verbose=False)[0]
            frame_keypoints: List[Tuple[int, int]] = [(0, 0)] * n_keypoints

            if keypoints_result and hasattr(keypoints_result, "keypoints") and keypoints_result.keypoints is not None:
                frame_keypoints_with_conf: List[Tuple[int, int, float]] = []
                for i, part_points in enumerate(keypoints_result.keypoints.data):
                    for k_id, (x, y, _) in enumerate(part_points):
                        confidence = float(keypoints_result.keypoints.conf[i][k_id])
                        frame_keypoints_with_conf.append((int(x), int(y), confidence))
                
                if len(frame_keypoints_with_conf) < n_keypoints:
                    frame_keypoints_with_conf.extend(
                        [(0, 0, 0.0)] * (n_keypoints - len(frame_keypoints_with_conf))
                    )
                else:
                    frame_keypoints_with_conf = frame_keypoints_with_conf[:n_keypoints]
                
                # Apply confidence filtering
                filtered_keypoints: List[Tuple[int, int]] = []
                for idx, (x, y, confidence) in enumerate(frame_keypoints_with_conf):
                    if idx in self.CORNER_INDICES:
                        if confidence < 0.3:
                            filtered_keypoints.append((0, 0))
                        else:
                            filtered_keypoints.append((int(x), int(y)))
                    else:
                        if confidence < 0.5:
                            filtered_keypoints.append((0, 0))
                        else:
                            filtered_keypoints.append((int(x), int(y)))
                frame_keypoints = filtered_keypoints

            results.append(TVFrameResult(frame_id=frame_id, boxes=boxes, keypoints=frame_keypoints))
        
        return results