Delete view_transformer.py
Browse files- view_transformer.py +0 -61
view_transformer.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
from sports import MeasurementUnit
|
| 2 |
-
from sports.basketball import CourtConfiguration, League, draw_court, draw_points_on_court
|
| 3 |
-
import numpy as np
|
| 4 |
-
import supervision as sv
|
| 5 |
-
import cv2
|
| 6 |
-
|
| 7 |
-
CONFIG = CourtConfiguration(league=League.NBA, measurement_unit=MeasurementUnit.FEET).vertices
|
| 8 |
-
|
| 9 |
-
def frame_xy_to_court_xy(frame_xy: np.ndarray, H: np.ndarray):
|
| 10 |
-
|
| 11 |
-
assert frame_xy.shape[1] == 2
|
| 12 |
-
n_points = frame_xy.shape[0]
|
| 13 |
-
|
| 14 |
-
court_xy = np.hstack((frame_xy, np.ones(shape=(n_points, 1)))) @ H.T
|
| 15 |
-
court_xy_norm = court_xy[:, :2] / court_xy[:, [-1]]
|
| 16 |
-
return court_xy_norm
|
| 17 |
-
|
| 18 |
-
def get_players_court_xy(frame, detections, model, use_bottom_center=True, normalize=False):
|
| 19 |
-
KEYPOINT_DETECTION_MODEL_CONFIDENCE = 0.3
|
| 20 |
-
KEYPOINT_DETECTION_MODEL_ANCHOR_CONFIDENCE = 0.5
|
| 21 |
-
|
| 22 |
-
# Locate court keypoints (or reference points)
|
| 23 |
-
result = model.infer(frame, confidence=KEYPOINT_DETECTION_MODEL_CONFIDENCE)[0]
|
| 24 |
-
key_points = sv.KeyPoints.from_inference(result)
|
| 25 |
-
filter_mask = key_points.confidence[0] > KEYPOINT_DETECTION_MODEL_ANCHOR_CONFIDENCE
|
| 26 |
-
|
| 27 |
-
# Compute homography matrix H
|
| 28 |
-
court_landmarks = np.array(CONFIG)[filter_mask]
|
| 29 |
-
frame_landmarks = key_points[:, filter_mask].xy[0]
|
| 30 |
-
H, _ = cv2.findHomography(frame_landmarks, court_landmarks)
|
| 31 |
-
|
| 32 |
-
# From the player detections, retrieve their position on the court
|
| 33 |
-
x1 = detections.xyxy[:, 0]
|
| 34 |
-
x2 = detections.xyxy[:, 2]
|
| 35 |
-
y1 = detections.xyxy[:, 1]
|
| 36 |
-
y2 = detections.xyxy[:, 3]
|
| 37 |
-
if use_bottom_center:
|
| 38 |
-
# Take the bottom center of the bounding box as the (x,y) coordinate
|
| 39 |
-
frame_xy = np.vstack(
|
| 40 |
-
(x1 + (x2 - x1) / 2, y2)
|
| 41 |
-
).T
|
| 42 |
-
else:
|
| 43 |
-
frame_xy = np.vstack(
|
| 44 |
-
(x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2)
|
| 45 |
-
).T
|
| 46 |
-
# apply homographic transformation
|
| 47 |
-
court_xy = frame_xy_to_court_xy(frame_xy, H)
|
| 48 |
-
|
| 49 |
-
if normalize:
|
| 50 |
-
court_xy = court_xy / np.array([94.0, 50.0])
|
| 51 |
-
|
| 52 |
-
return court_xy
|
| 53 |
-
|
| 54 |
-
def show_positions_on_court(court_xy):
|
| 55 |
-
court = draw_court(config=CONFIG)
|
| 56 |
-
court = draw_points_on_court(
|
| 57 |
-
config=CONFIG,
|
| 58 |
-
xy=court_xy,
|
| 59 |
-
court=court
|
| 60 |
-
)
|
| 61 |
-
sv.plot_image(court)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|