Skip to content

Commit cdaa7c7

Browse files
committed
update video example - play video on loop and detect
1 parent bcd1788 commit cdaa7c7

File tree

4 files changed

+26
-54
lines changed

4 files changed

+26
-54
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/objectDetection-video/sketch.js

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,53 @@
99
let video;
1010
let detector;
1111
let detections = [];
12-
let canvasElement;
1312

1413
function preload(){
1514
detector = ml5.objectDetector("cocossd");
1615
}
1716

17+
// Callback function is called each time the object detector finishes processing a frame.
18+
function gotDetections(results) {
19+
// Update detections array with the new results
20+
detections = results;
21+
}
22+
1823
function setup() {
1924
// Create canvas with initial size - will be resized later
20-
canvasElement = createCanvas(640, 480);
21-
canvasElement.position(0, 0);
25+
createCanvas(640, 480);
2226

23-
// Create video element (paused by default)
27+
// Load and loop the video for object detection
2428
video = createVideo('test.mov');
25-
video.position(0, 0);
26-
video.volume(0);
27-
video.showControls();
28-
29-
// Make canvas transparent and on top
30-
canvasElement.style('z-index', '1');
31-
canvasElement.style('pointer-events', 'none'); // Allow clicks to pass through to video
32-
video.style('z-index', '-1');
33-
34-
// Set up video event listeners
35-
video.elt.addEventListener('loadedmetadata', () => {
36-
console.log('Video metadata loaded');
29+
video.size(width, height);
30+
video.hide();
31+
video.loop();
3732

38-
// Resize canvas to match video size
39-
resizeCanvas(video.elt.videoWidth, video.elt.videoHeight);
40-
});
41-
42-
video.elt.addEventListener('play', () => {
43-
console.log('Video started playing');
44-
45-
// Start detection
46-
detector.detectStart(video, gotDetections);
47-
});
48-
49-
video.elt.addEventListener('pause', () => {
50-
console.log('Video paused');
51-
52-
// Stop detection when video is paused
53-
detector.detectStop();
54-
});
55-
56-
video.elt.addEventListener('ended', () => {
57-
console.log('Video ended');
58-
59-
// Stop detection when video ends
60-
detector.detectStop();
61-
});
33+
detector.detectStart(video, gotDetections);
6234
}
6335

6436
function draw(){
65-
// Clear the canvas (this acts as the transparent overlay)
66-
clear();
37+
image(video, 0, 0, width, height); // draw video frame
38+
39+
// Scale factors from original video to canvas size
40+
let scaleX = width / video.elt.videoWidth;
41+
let scaleY = height / video.elt.videoHeight;
6742

68-
for (let i = 0; i < detections.length; i += 1) {
43+
for (let i = 0; i < detections.length; i++) {
6944
let detection = detections[i];
7045

71-
// Draw bounding box
46+
let x = detection.x * scaleX;
47+
let y = detection.y * scaleY;
48+
let w = detection.width * scaleX;
49+
let h = detection.height * scaleY;
50+
7251
stroke(0, 255, 0);
7352
strokeWeight(4);
7453
noFill();
75-
rect(detection.x, detection.y, detection.width, detection.height);
54+
rect(x, y, w, h);
7655

77-
// Draw label
7856
noStroke();
7957
fill(255);
80-
textSize(24);
81-
text(detection.label, detection.x + 10, detection.y + 24);
58+
textSize(18);
59+
text(detection.label, x + 5, y + 20);
8260
}
83-
}
84-
85-
// Callback function is called each time the object detector finishes processing a frame.
86-
function gotDetections(results) {
87-
// Update detections array with the new results
88-
detections = results;
8961
}

0 commit comments

Comments
 (0)