going in the very right direction!

This commit is contained in:
didier amyot committed 2024-01-23 21:02:45 -05:00
1 parent 3ea54208be
commit e6c609fb08
5 files changed
+14481 -17

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ def iter_frames(video_cap):
ret, frame = video_cap.read() ret, frame = video_cap.read()
if not ret: if not ret:
return return
yield frame yield frame, video_cap.get(cv2.CAP_PROP_POS_MSEC)
@contextlib.contextmanager @contextlib.contextmanager
+36 -15
View File
@@ -1,3 +1,5 @@
import json
import sys
from pathlib import Path from pathlib import Path
from ultralytics import YOLO from ultralytics import YOLO
@@ -34,7 +36,10 @@ class DodgeballTracker:
detected_box_objects: list[DetectedObjectBox]): detected_box_objects: list[DetectedObjectBox]):
self.input_path = input_file self.input_path = input_file
self.output_path = output_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.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: def write_frame(self, writer, frame) -> None:
cv2.imshow("Frame", frame) cv2.imshow("Frame", frame)
@@ -43,18 +48,6 @@ class DodgeballTracker:
def extract_box(self, data) -> tuple[int, int, int, int]: def extract_box(self, data) -> tuple[int, int, int, int]:
return int(data[0]), int(data[1]), int(data[2]), int(data[3]) return int(data[0]), int(data[1]), int(data[2]), int(data[3])
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
def run_model_on_frame(self, model, frame):
return model(frame)[0]
def get_court_perspective_transform(self): def get_court_perspective_transform(self):
""" """
""" """
@@ -79,6 +72,30 @@ class DodgeballTracker:
court_bottom_right]) * pixel_per_meter court_bottom_right]) * pixel_per_meter
return cv2.getPerspectiveTransform(empirical_source, empirical_target) return cv2.getPerspectiveTransform(empirical_source, empirical_target)
def print_box(self, data, frame_idx, frame_ts):
xmin, ymin, xmax, ymax = self.extract_box(data)
v = np.array([
(xmax + xmin) / 2,
ymax, # (ymax + ymin) / 2,
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
def run_model_on_frame(self, model, frame):
return model(frame)[0]
def crazy_experiment(self, frame): def crazy_experiment(self, frame):
cv2.putText(frame, cv2.putText(frame,
"Hello world", "Hello world",
@@ -92,6 +109,10 @@ class DodgeballTracker:
cv2.polylines(frame, pts, True, (0, 255, 255), 8) 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): def main(self):
# define some constants # define some constants
@@ -101,12 +122,11 @@ class DodgeballTracker:
# load the pre-trained YOLOv8n model # load the pre-trained YOLOv8n model
model = YOLO("yolov8n.pt") model = YOLO("yolov8n.pt")
for frame in iter_frames(video_cap): for i, (frame, ts) in enumerate(iter_frames(video_cap)):
with time_tracker():
# run the YOLO model on the frame # run the YOLO model on the frame
detections = self.run_model_on_frame(model, frame) detections = self.run_model_on_frame(model, frame)
for data in detections.boxes.data.tolist(): for data in detections.boxes.data.tolist():
self.plot_boxe_to_frame(data, frame) self.plot_boxe_to_frame(data, frame, i, ts)
# show the frame to our screen # show the frame to our screen
self.write_frame(writer, frame) self.write_frame(writer, frame)
@@ -117,6 +137,7 @@ class DodgeballTracker:
video_cap.release() video_cap.release()
writer.release() writer.release()
cv2.destroyAllWindows() cv2.destroyAllWindows()
self.persist_points()
+8
View File
@@ -12,6 +12,13 @@
</head> </head>
<body> <body>
<h1>Hello, world!</h1> <h1>Hello, world!</h1>
<h3 id="first-ts"></h3>
<svg width="450" height="900" id="first" style="background-color: lightgray">
</svg>
<br>
<svg width="900" height="450" > <svg width="900" height="450" >
<rect x="0" y="0" width="900" height="450" class="court-border"></rect> <rect x="0" y="0" width="900" height="450" class="court-border"></rect>
<line x1="450" y1="0" x2="450" y2="450" class="court-border"></line> <line x1="450" y1="0" x2="450" y2="450" class="court-border"></line>
@@ -57,6 +64,7 @@
</svg> </svg>
<div> <div>
<button id="play">play</button> <br> <button id="play">play</button> <br>
<button id="pause">pause</button> <button id="pause">pause</button>
+36 -1
View File
@@ -1,4 +1,5 @@
import anime from "https://esm.sh/animejs@3.2.2"; import anime from "https://esm.sh/animejs@3.2.2";
import m from "https://esm.sh/mithril@2.2.2"
const second = 1000; const second = 1000;
@@ -13,7 +14,6 @@ function gaussianRandom(mean=0, stdev=1) {
return z * stdev + mean; return z * stdev + mean;
} }
function* brownian(n){ function* brownian(n){
let x = 0; let x = 0;
for(let i = 0; i < n; i ++){ for(let i = 0; i < n; i ++){
@@ -64,3 +64,38 @@ document.querySelector('#seek').onclick = function(){
timeline.pause(); timeline.pause();
timeline.seek(seekVal); timeline.seek(seekVal);
} }
function generatePoints(points, frames){
const lst = [];
for(const point of points.points){
if(frames.indexOf(point.frameIndex) !== -1){
lst.push(m("circle", {cx: point.x, cy: point.y, r: 3}));
}
}
return m("g", lst);
}
function getTimeStamp(points, frameId){
for(const point of points.points){
if(point.frameIndex === frameId){
return `${point.frameTimestamp / 1000} seconds`;
}
}
}
const url = "/dodgeball-tracker/outputs/output.json";
const resp = await fetch(url);
const points = await resp.json();
function range(min, max){
const ret = [];
while(min < max){
ret.push(min);
min ++;
}
return ret
}
m.render(document.getElementById("first"), generatePoints(points, range(-10, 400)));
m.render(document.getElementById("first-ts"), m("span", getTimeStamp(points, 80)));
+14400
View File
File diff suppressed because it is too large. Load diff