going somewhere
This commit is contained in:
1 parent
a7cb0e7fa7
commit
1da186a759
8 files changed
+3655
-6878
No files matched your search
+155
@@ -1,6 +1,161 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
env
|
||||
*.mp4
|
||||
.idea
|
||||
.DS_Store
|
||||
*.pt
|
||||
*.png
|
||||
*.ipynb
|
||||
Binary file not shown.
@@ -1,6 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
from dodgeballtracker.logic import DetectedObjectBox, DodgeballTracker
|
||||
from dodgeballtracker.logic import (DetectedObjectBox,
|
||||
DodgeballTracker,
|
||||
get_court_perspective_transform)
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -13,8 +15,10 @@ if __name__ == "__main__":
|
||||
DetectedObjectBox.from_viridis(threshold) for threshold in np.arange(0.5, 1, 0.05)
|
||||
]
|
||||
input_video = input_dir / "micro.mp4"
|
||||
output_video = output_dir / "output.mp4"
|
||||
output_video = output_dir / "output-lmbn_n_cuhk03_d.mp4"
|
||||
|
||||
dodgeball_tracker = DodgeballTracker(input_video, output_video, detected_box_objects)
|
||||
transformation_matrix = get_court_perspective_transform()
|
||||
|
||||
dodgeball_tracker = DodgeballTracker(input_video, output_video, detected_box_objects, transformation_matrix)
|
||||
|
||||
dodgeball_tracker.main()
|
||||
+56
-45
@@ -29,27 +29,7 @@ class DetectedObjectBox:
|
||||
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):
|
||||
def get_court_perspective_transform():
|
||||
"""
|
||||
"""
|
||||
empirical_top_left = np.float32([416, 175])
|
||||
@@ -73,6 +53,28 @@ class DodgeballTracker:
|
||||
court_bottom_right]) * pixel_per_meter
|
||||
return cv2.getPerspectiveTransform(empirical_source, empirical_target)
|
||||
|
||||
|
||||
class DodgeballTracker:
|
||||
|
||||
def __init__(self,
|
||||
input_file: Path,
|
||||
output_file: Path,
|
||||
detected_box_objects: list[DetectedObjectBox],
|
||||
transformation_matrix):
|
||||
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 = transformation_matrix
|
||||
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 print_box(self, data, frame_idx, frame_ts):
|
||||
xmin, ymin, xmax, ymax = self.extract_box(data)
|
||||
xavg = (xmax + xmin) / 2
|
||||
@@ -80,16 +82,26 @@ class DodgeballTracker:
|
||||
ret = self.transformation_matrix @ v
|
||||
x, y = ret[0] / ret[2], ret[1] / ret[2]
|
||||
object_id = data[-1]
|
||||
self.points.append({"x": x, "y": y, "frameIndex": frame_idx, "frameTimestamp": frame_ts, "objectId": object_id})
|
||||
self.points.append({"x": x,
|
||||
"y": y,
|
||||
"frameIndex": frame_idx,
|
||||
"frameTimestamp": frame_ts,
|
||||
"objectId": int(object_id)
|
||||
})
|
||||
|
||||
def plot_boxe_to_frame(self, data, frame, frame_idx, frame_ts):
|
||||
def print_tracker_data(self, tracker_data, frame_idx, frame_ts):
|
||||
for d in tracker_data:
|
||||
self.print_box(d, frame_idx, frame_ts)
|
||||
|
||||
def plot_boxe_to_frame(self, data, frame):
|
||||
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)
|
||||
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), detected_box_object.color, 2)
|
||||
return data # [[xmin, ymin, xmax - xmin, ymax - ymin], confidence, int(data[5])]
|
||||
return True
|
||||
return False
|
||||
|
||||
def run_model_on_frame(self, model, frame):
|
||||
return model(frame)[0]
|
||||
@@ -108,43 +120,45 @@ class DodgeballTracker:
|
||||
cv2.polylines(frame, pts, True, (0, 255, 255), 8)
|
||||
|
||||
def persist_points(self):
|
||||
nbr_id = max((p["objectId"] for p in self.points))
|
||||
cm = matplotlib.colormaps['viridis']
|
||||
for p in self.points:
|
||||
value = p["objectId"] / nbr_id
|
||||
color: Color = tuple((int(255 * cm(value)[i]) for i in range(3)))
|
||||
p["color"] = f"rgb({color[0]}, {color[2]}, {color[1]})"
|
||||
|
||||
with open(self.output_json_path, "wt") as file_:
|
||||
json.dump({"points": self.points}, file_, indent=4)
|
||||
|
||||
def _get_tracker(self):
|
||||
|
||||
return DeepOCSORT(
|
||||
model_weights=Path('lmbn_n_cuhk03_d.pt'), # which ReID model to use
|
||||
device=torch.device("mps"),
|
||||
fp16=True,
|
||||
)
|
||||
|
||||
def _get_detection_model(self):
|
||||
"""
|
||||
load the pre-trained YOLOv8n model
|
||||
"""
|
||||
return YOLO("yolov8n.pt")
|
||||
|
||||
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,
|
||||
)
|
||||
tracker = self._get_tracker()
|
||||
|
||||
# load the pre-trained YOLOv8n model
|
||||
model = YOLO("yolov8n.pt")
|
||||
model = self._get_detection_model()
|
||||
|
||||
for i, (frame, ts) in enumerate(iter_frames(video_cap)):
|
||||
# run the YOLO model on the frame
|
||||
res = []
|
||||
detections = self.run_model_on_frame(model, frame)
|
||||
for data in detections.boxes.data.tolist():
|
||||
selected_data = self.plot_boxe_to_frame(data, frame, i, ts)
|
||||
if selected_data:
|
||||
res.append(selected_data)
|
||||
is_good_box = self.plot_boxe_to_frame(data, frame)
|
||||
if is_good_box:
|
||||
res.append(data)
|
||||
|
||||
# show the frame to our screen
|
||||
data_ = tracker.update(np.array(res), frame)
|
||||
for d in data_:
|
||||
self.print_box(d, i, ts)
|
||||
self.print_tracker_data(data_, i, ts)
|
||||
|
||||
self.write_frame(writer, frame)
|
||||
|
||||
@@ -155,6 +169,3 @@ class DodgeballTracker:
|
||||
writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
self.persist_points()
|
||||
|
||||
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
<h3 id="first-ts"></h3>
|
||||
<div>
|
||||
<select>
|
||||
<option value="lmbn_n_cuhk03_d">lmbn_n_cuhk03_d</option>
|
||||
</select>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<svg width="900" height="450" id="first" style="background-color: lightgray">
|
||||
</svg>
|
||||
|
||||
|
||||
+14
-6
@@ -1,5 +1,7 @@
|
||||
import anime from "https://esm.sh/animejs@3.2.2";
|
||||
import m from "https://esm.sh/mithril@2.2.2"
|
||||
import colormap from "https://esm.sh/colormap@2.3.2"
|
||||
|
||||
|
||||
const second = 1000;
|
||||
|
||||
@@ -68,23 +70,29 @@ document.querySelector('#seek').onclick = function(){
|
||||
|
||||
function generatePoints(points, frames){
|
||||
const lst = [];
|
||||
for(const point of points.points){
|
||||
const colors = colormap({
|
||||
colormap: 'viridis',
|
||||
nshades: new Set(points.map(p => p.objectId)).size,
|
||||
format: 'hex',
|
||||
alpha: 1
|
||||
});
|
||||
for(const point of points){
|
||||
if(frames.indexOf(point.frameIndex) !== -1){
|
||||
lst.push(m("circle", {cx: point.y, cy: point.x, r: 3, fill: point.color}));
|
||||
lst.push(m("text", {x: point.y, y: point.x, fill: colors[point.objectId]}, point.objectId));
|
||||
}
|
||||
}
|
||||
return m("g", lst);
|
||||
}
|
||||
|
||||
function getTimeStamp(points, frameId){
|
||||
for(const point of points.points){
|
||||
for(const point of points){
|
||||
if(point.frameIndex === frameId){
|
||||
return `${point.frameTimestamp / 1000} seconds`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const url = "/dodgeball-tracker/outputs/output.json";
|
||||
const url = "/dodgeball-tracker/outputs/output-lmbn_n_cuhk03_d.json";
|
||||
const resp = await fetch(url);
|
||||
const points = await resp.json();
|
||||
|
||||
@@ -97,5 +105,5 @@ function range(min, max){
|
||||
return ret
|
||||
}
|
||||
|
||||
m.render(document.getElementById("first"), generatePoints(points, range(-10, 400)));
|
||||
m.render(document.getElementById("first-ts"), m("span", getTimeStamp(points, 80)));
|
||||
m.render(document.getElementById("first"), generatePoints(points.points, range(-10, 400)));
|
||||
m.render(document.getElementById("first-ts"), m("span", getTimeStamp(points.points, 80)));
|
||||
@@ -0,0 +1,2 @@
|
||||
*.json
|
||||
*.mp4
|
||||
+3412
-6824
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user