Files
dodgeball-tracker/dodgeballtracker/logic.py
T
2024-01-24 07:40:50 -05:00

150 lines
5.2 KiB
Python

import json
from pathlib import Path
import torch
from ultralytics import YOLO
import cv2
from dodgeballtracker.helper import create_video_writer, iter_frames
import matplotlib
import numpy as np
from boxmot import DeepOCSORT
Color = tuple[int, int, int]
class DetectedObjectBox:
def __init__(self, min_threshold: float, color: Color):
self.min_threshold = min_threshold
self.color = color
def __lt__(self, other):
return self.min_threshold < other.min_threshold
@classmethod
def from_viridis(cls, min_threshold):
cm = matplotlib.colormaps['viridis']
color: Color = tuple((int(255 * cm(min_threshold)[i]) for i in range(3)))
return cls(min_threshold, color)
class DodgeballTracker:
def __init__(self,
input_file: Path,
output_file: Path,
detected_box_objects: list[DetectedObjectBox]):
self.input_path = input_file
self.output_path = output_file
self.output_json_path = output_file.with_suffix(".json")
self.detected_box_objects = sorted(detected_box_objects, reverse=True)
self.transformation_matrix = self.get_court_perspective_transform()
self.points = []
def write_frame(self, writer, frame) -> None:
cv2.imshow("Frame", frame)
writer.write(frame)
def extract_box(self, data) -> tuple[int, int, int, int]:
return int(data[0]), int(data[1]), int(data[2]), int(data[3])
def get_court_perspective_transform(self):
"""
"""
empirical_top_left = np.float32([416, 175])
empirical_top_right = np.float32([905, 173])
empirical_bottom_left = np.float32([51, 702])
empirical_bottom_right = np.float32([1188, 695])
court_top_left = np.float32([0, 0])
court_top_right = np.float32([9, 0])
court_bottom_left = np.float32([0, 18])
court_bottom_right = np.float32([9, 18])
empirical_source = np.float32([empirical_top_left,
empirical_top_right,
empirical_bottom_left,
empirical_bottom_right])
pixel_per_meter = 50
empirical_target = np.float32([court_top_left,
court_top_right,
court_bottom_left,
court_bottom_right]) * pixel_per_meter
return cv2.getPerspectiveTransform(empirical_source, empirical_target)
def print_box(self, data, frame_idx, frame_ts):
xmin, ymin, xmax, ymax = self.extract_box(data)
xavg = (xmax + xmin) / 2
v = np.array([xavg, ymax, 1])
ret = self.transformation_matrix @ v
x, y = ret[0] / ret[2], ret[1] / ret[2]
self.points.append({"x": x, "y": y, "frameIndex": frame_idx, "frameTimestamp": frame_ts})
def plot_boxe_to_frame(self, data, frame, frame_idx, frame_ts):
confidence = float(data[4])
for detected_box_object in self.detected_box_objects:
if detected_box_object.min_threshold < confidence:
xmin, ymin, xmax, ymax = self.extract_box(data)
self.print_box(data, frame_idx, frame_ts)
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), detected_box_object.color, 2)
return data # [[xmin, ymin, xmax - xmin, ymax - ymin], confidence, int(data[5])]
def run_model_on_frame(self, model, frame):
return model(frame)[0]
def crazy_experiment(self, frame):
cv2.putText(frame,
"Hello world",
(50, 50),
cv2.FONT_HERSHEY_SIMPLEX,
2,
(0, 255, 0), 8)
cv2.line(frame, (0, 0), (511, 511), (255, 0, 0), 5)
pts = np.array([[50, 50], [500, 50], [500, 500], [50, 500], [50, 50]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(frame, pts, True, (0, 255, 255), 8)
def persist_points(self):
with open(self.output_json_path, "wt") as file_:
json.dump({"points": self.points}, file_, indent=4)
def main(self):
# define some constants
video_cap = cv2.VideoCapture(str(self.input_path))
writer = create_video_writer(video_cap, str(self.output_path))
tracker = DeepOCSORT(
model_weights=Path('osnet_x0_25_msmt17.pt'), # which ReID model to use
device=torch.device("mps"),
fp16=True,
)
# load the pre-trained YOLOv8n model
model = YOLO("yolov8n.pt")
for i, (frame, ts) in enumerate(iter_frames(video_cap)):
# run the YOLO model on the frame
detections = self.run_model_on_frame(model, frame)
results = []
for data in detections.boxes.data.tolist():
results.append(self.plot_boxe_to_frame(data, frame, i, ts))
tracks = tracker.update(np.array([r for r in results if r is not None]), img=frame)
# show the frame to our screen
self.write_frame(writer, frame)
if cv2.waitKey(1) == ord("q"):
break
video_cap.release()
writer.release()
cv2.destroyAllWindows()
self.persist_points()