9
9
let video ;
10
10
let detector ;
11
11
let detections = [ ] ;
12
- let canvasElement ;
13
12
14
13
function preload ( ) {
15
14
detector = ml5 . objectDetector ( "cocossd" ) ;
16
15
}
17
16
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
+
18
23
function setup ( ) {
19
24
// Create canvas with initial size - will be resized later
20
- canvasElement = createCanvas ( 640 , 480 ) ;
21
- canvasElement . position ( 0 , 0 ) ;
25
+ createCanvas ( 640 , 480 ) ;
22
26
23
- // Create video element (paused by default)
27
+ // Load and loop the video for object detection
24
28
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 ( ) ;
37
32
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 ) ;
62
34
}
63
35
64
36
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 ;
67
42
68
- for ( let i = 0 ; i < detections . length ; i += 1 ) {
43
+ for ( let i = 0 ; i < detections . length ; i ++ ) {
69
44
let detection = detections [ i ] ;
70
45
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
+
72
51
stroke ( 0 , 255 , 0 ) ;
73
52
strokeWeight ( 4 ) ;
74
53
noFill ( ) ;
75
- rect ( detection . x , detection . y , detection . width , detection . height ) ;
54
+ rect ( x , y , w , h ) ;
76
55
77
- // Draw label
78
56
noStroke ( ) ;
79
57
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 ) ;
82
60
}
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 ;
89
61
}
0 commit comments