1
+ document . getElementById ( "ai" ) . addEventListener ( "change" , toggleAi )
2
+ document . getElementById ( "fps" ) . addEventListener ( "input" , changeFps )
3
+
4
+ const video = document . getElementById ( "video" ) ;
5
+ const c1 = document . getElementById ( 'c1' ) ;
6
+ const ctx1 = c1 . getContext ( '2d' ) ;
7
+ var cameraAvailable = false ;
8
+ var aiEnabled = false ;
9
+ var fps = 16 ;
10
+
11
+ /* Setting up the constraint */
12
+ var facingMode = "environment" ; // Can be 'user' or 'environment' to access back or front camera (NEAT!)
13
+ var constraints = {
14
+ audio : false ,
15
+ video : {
16
+ facingMode : facingMode
17
+ }
18
+ } ;
19
+
20
+ /* Stream it to video element */
21
+ camera ( ) ;
22
+ function camera ( ) {
23
+ if ( ! cameraAvailable ) {
24
+ console . log ( "camera" )
25
+ navigator . mediaDevices . getUserMedia ( constraints ) . then ( function ( stream ) {
26
+ cameraAvailable = true ;
27
+ video . srcObject = stream ;
28
+ } ) . catch ( function ( err ) {
29
+ cameraAvailable = false ;
30
+ if ( modelIsLoaded ) {
31
+ if ( err . name === "NotAllowedError" ) {
32
+ document . getElementById ( "loadingText" ) . innerText = "Waiting for camera permission" ;
33
+ }
34
+ }
35
+ setTimeout ( camera , 1000 ) ;
36
+ } ) ;
37
+ }
38
+ }
39
+
40
+ window . onload = function ( ) {
41
+ timerCallback ( ) ;
42
+ }
43
+
44
+ function timerCallback ( ) {
45
+ if ( isReady ( ) ) {
46
+ setResolution ( ) ;
47
+ ctx1 . drawImage ( video , 0 , 0 , c1 . width , c1 . height ) ;
48
+ if ( aiEnabled ) {
49
+ ai ( ) ;
50
+ }
51
+ }
52
+ setTimeout ( timerCallback , fps ) ;
53
+ }
54
+
55
+ function isReady ( ) {
56
+ if ( modelIsLoaded && cameraAvailable ) {
57
+ document . getElementById ( "loadingText" ) . style . display = "none" ;
58
+ document . getElementById ( "ai" ) . disabled = false ;
59
+ return true ;
60
+ } else {
61
+ return false ;
62
+ }
63
+ }
64
+
65
+ function setResolution ( ) {
66
+ if ( window . screen . width < video . videoWidth ) {
67
+ c1 . width = window . screen . width * 0.9 ;
68
+ let factor = c1 . width / video . videoWidth ;
69
+ c1 . height = video . videoHeight * factor ;
70
+ } else if ( window . screen . height < video . videoHeight ) {
71
+ c1 . height = window . screen . height * 0.50 ;
72
+ let factor = c1 . height / video . videoHeight ;
73
+ c1 . width = video . videoWidth * factor ;
74
+ }
75
+ else {
76
+ c1 . width = video . videoWidth ;
77
+ c1 . height = video . videoHeight ;
78
+ }
79
+ } ;
80
+
81
+ function toggleAi ( ) {
82
+ aiEnabled = document . getElementById ( "ai" ) . checked ;
83
+ }
84
+
85
+ function changeFps ( ) {
86
+ fps = 1000 / document . getElementById ( "fps" ) . value ;
87
+ }
88
+
89
+ function ai ( ) {
90
+ // Detect objects in the image element
91
+ objectDetector . detect ( c1 , ( err , results ) => {
92
+ console . log ( results ) ; // Will output bounding boxes of detected objects
93
+ for ( let index = 0 ; index < results . length ; index ++ ) {
94
+ const element = results [ index ] ;
95
+ ctx1 . font = "15px Arial" ;
96
+ ctx1 . fillStyle = "red" ;
97
+ ctx1 . fillText ( element . label + " - " + ( element . confidence * 100 ) . toFixed ( 2 ) + "%" , element . x + 10 , element . y + 15 ) ;
98
+ ctx1 . beginPath ( ) ;
99
+ ctx1 . strokeStyle = "red" ;
100
+ ctx1 . rect ( element . x , element . y , element . width , element . height ) ;
101
+ ctx1 . stroke ( ) ;
102
+ console . log ( element . label ) ;
103
+ }
104
+ } ) ;
105
+ }
0 commit comments