New ui #1
No files matched your search
@@ -1,5 +1,7 @@
|
|||||||
import contextlib
|
import contextlib
|
||||||
import time
|
import time
|
||||||
|
from typing import Iterator
|
||||||
|
from dodgeballtracker.typing import VideoFrame, VideoTimestamp
|
||||||
import cv2
|
import cv2
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@@ -7,7 +9,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def iter_frames(video_cap):
|
def iter_frames(video_cap) -> Iterator[tuple[VideoFrame, VideoTimestamp]]:
|
||||||
while 1:
|
while 1:
|
||||||
ret, frame = video_cap.read()
|
ret, frame = video_cap.read()
|
||||||
if not ret:
|
if not ret:
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from collections import Counter
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import logging
|
import logging
|
||||||
import torch
|
import torch
|
||||||
@@ -9,7 +10,7 @@ from dodgeballtracker.video_writer import create_video_writer, VideoWriter
|
|||||||
import matplotlib
|
import matplotlib
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from boxmot import DeepOcSort as DeepOCSORT
|
from boxmot import DeepOcSort as DeepOCSORT
|
||||||
|
from .typing import VideoFrame
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -111,7 +112,7 @@ class DodgeballTracker:
|
|||||||
for d in tracker_data:
|
for d in tracker_data:
|
||||||
self.print_box(d, frame_idx, frame_ts)
|
self.print_box(d, frame_idx, frame_ts)
|
||||||
|
|
||||||
def plot_box_to_frame(self, data, frame):
|
def plot_box_to_frame(self, data, frame: VideoFrame):
|
||||||
confidence = float(data[4])
|
confidence = float(data[4])
|
||||||
for detected_box_object in self.detected_box_objects:
|
for detected_box_object in self.detected_box_objects:
|
||||||
|
|
||||||
@@ -121,22 +122,9 @@ class DodgeballTracker:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def run_model_on_frame(self, model, frame):
|
def run_model_on_frame(self, model, frame: VideoFrame):
|
||||||
return model(frame)[0]
|
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):
|
def persist_points(self):
|
||||||
with open(self.output_json_path, "wt") as file_:
|
with open(self.output_json_path, "wt") as file_:
|
||||||
json.dump({"points": self.points}, file_, indent=4)
|
json.dump({"points": self.points}, file_, indent=4)
|
||||||
@@ -144,7 +132,7 @@ class DodgeballTracker:
|
|||||||
def _get_tracker(self):
|
def _get_tracker(self):
|
||||||
return DeepOCSORT(
|
return DeepOCSORT(
|
||||||
Path('lmbn_n_cuhk03_d.pt'), # which ReID model to use
|
Path('lmbn_n_cuhk03_d.pt'), # which ReID model to use
|
||||||
get_best_available_torch_device(), # torch.cuda.get_device_name(0), #torch.device(get_best_available_torch_device()),
|
get_best_available_torch_device(),
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -157,11 +145,9 @@ class DodgeballTracker:
|
|||||||
def main(self):
|
def main(self):
|
||||||
# define some constants
|
# define some constants
|
||||||
video_cap = cv2.VideoCapture(str(self.input_path))
|
video_cap = cv2.VideoCapture(str(self.input_path))
|
||||||
# writer = create_video_writer(video_cap, self.output_path)
|
|
||||||
tracker = self._get_tracker()
|
tracker = self._get_tracker()
|
||||||
|
|
||||||
# load the pre-trained YOLOv8n model
|
|
||||||
model = self._get_detection_model()
|
model = self._get_detection_model()
|
||||||
|
counter = Counter()
|
||||||
|
|
||||||
for i, (frame, ts) in enumerate(iter_frames(video_cap)):
|
for i, (frame, ts) in enumerate(iter_frames(video_cap)):
|
||||||
# run the YOLO model on the frame
|
# run the YOLO model on the frame
|
||||||
@@ -177,8 +163,9 @@ class DodgeballTracker:
|
|||||||
|
|
||||||
self.video_writer.write_frame(frame)
|
self.video_writer.write_frame(frame)
|
||||||
|
|
||||||
if cv2.waitKey(1) == ord("q"):
|
if self.video_writer.wait():
|
||||||
break
|
break
|
||||||
|
|
||||||
|
print(f"counter: {counter}")
|
||||||
video_cap.release()
|
video_cap.release()
|
||||||
self.persist_points()
|
self.persist_points()
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
from typing import NewType
|
||||||
|
|
||||||
|
type XCoord = float
|
||||||
|
type YCoord = NewType('YCoord', float)
|
||||||
|
type Point = tuple[XCoord, YCoord]
|
||||||
|
type VideoFrame = NewType('Frame', float)
|
||||||
|
type VideoTimestamp = float
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import abc
|
import abc
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from dodgeballtracker.typing import VideoFrame
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
def create_video_writer(video_cap: cv2.VideoCapture, output_filename: Path) -> cv2.VideoWriter:
|
def create_video_writer(video_cap: cv2.VideoCapture, output_filename: Path) -> cv2.VideoWriter:
|
||||||
@@ -23,35 +23,45 @@ def create_video_writer(video_cap: cv2.VideoCapture, output_filename: Path) -> c
|
|||||||
class VideoWriter(abc.ABC):
|
class VideoWriter(abc.ABC):
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def write_frame(self, frame) -> None:
|
def write_frame(self, frame: VideoFrame) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def wait(self) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NoopVideoWriter(VideoWriter):
|
class NoopVideoWriter(VideoWriter):
|
||||||
|
|
||||||
def write_frame(self, frame) -> None:
|
def write_frame(self, frame: VideoFrame) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def wait(self) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class MP4VVideoWriter(VideoWriter):
|
class MP4VVideoWriter(VideoWriter):
|
||||||
|
|
||||||
def __init__(self, video_writer: cv2.VideoWriter) -> None:
|
def __init__(self, video_writer: cv2.VideoWriter) -> None:
|
||||||
self._video_writer = video_writer
|
self._video_writer = video_writer
|
||||||
|
|
||||||
def write_frame(self, frame) -> None:
|
def write_frame(self, frame: VideoFrame) -> None:
|
||||||
self._video_writer.write(frame)
|
self._video_writer.write(frame)
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
self._video_writer.release()
|
self._video_writer.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
def wait(self) -> bool:
|
||||||
|
return False #cv2.waitKey(1) == ord("q")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_frame(cls, input_: Path, output: Path) -> VideoWriter:
|
def from_frame(cls, input_: Path, output: Path) -> VideoWriter:
|
||||||
video_cap = cv2.VideoCapture(str(input_))
|
video_cap = cv2.VideoCapture(str(input_))
|
||||||
@@ -62,6 +72,9 @@ class MP4VVideoWriter(VideoWriter):
|
|||||||
|
|
||||||
|
|
||||||
class MP4VVideoWriterWithFrame(MP4VVideoWriter):
|
class MP4VVideoWriterWithFrame(MP4VVideoWriter):
|
||||||
def write_frame(self, frame) -> None:
|
def write_frame(self, frame: VideoFrame) -> None:
|
||||||
super().write_frame(frame)
|
super().write_frame(frame)
|
||||||
cv2.imshow("Frame", frame)
|
cv2.imshow("Frame", frame)
|
||||||
|
|
||||||
|
def wait(self) -> bool:
|
||||||
|
return cv2.waitKey(1) == ord("q")
|
||||||
Reference in new issue
Block a user