@@ -27,7 +27,11 @@ export class CocoSsdBase {
27
27
constructor ( video , options , constructorCallback ) {
28
28
this . video = video || null ;
29
29
this . modelReady = false ;
30
+
30
31
this . isPredicting = false ;
32
+ this . signalStop = false ;
33
+ this . prevCall = "" ;
34
+
31
35
this . config = {
32
36
base : options . base || DEFAULTS . base ,
33
37
modelUrl : options . modelUrl || DEFAULTS . modelUrl ,
@@ -37,9 +41,6 @@ export class CocoSsdBase {
37
41
this . ready = callCallback ( this . loadModel ( ) , this . callback ) ;
38
42
}
39
43
40
- /**
41
- * load model
42
- */
43
44
async loadModel ( ) {
44
45
await tf . setBackend ( "webgl" ) ; // this line resolves warning : performance is poor on webgpu backend
45
46
await tf . ready ( ) ;
@@ -74,8 +75,11 @@ export class CocoSsdBase {
74
75
* @returns {ObjectDetectorPrediction }
75
76
*/
76
77
async detectInternal ( imgToPredict ) {
77
- this . isPredicting = true ;
78
- mediaReady ( imgToPredict , true )
78
+ // this.isPredicting = true;
79
+ await this . ready ;
80
+
81
+ mediaReady ( imgToPredict , true ) ;
82
+
79
83
await tf . nextFrame ( ) ;
80
84
81
85
const predictions = await this . model . detect ( imgToPredict ) ;
@@ -95,7 +99,9 @@ export class CocoSsdBase {
95
99
} ,
96
100
} ;
97
101
} ) ;
102
+
98
103
this . isPredicting = false ;
104
+
99
105
return formattedPredictions ;
100
106
}
101
107
@@ -107,16 +113,48 @@ export class CocoSsdBase {
107
113
* @returns {ObjectDetectorPrediction }
108
114
*/
109
115
async detect ( inputOrCallback , cb ) {
110
- await this . ready ;
111
- await tf . nextFrame ( ) ;
112
-
113
116
const args = handleArguments ( this . video , inputOrCallback , cb ) ;
114
117
args . require ( "image" , "Detection subject not supported" ) ;
115
118
116
- await mediaReady ( args . image , true ) ;
117
-
118
119
return callCallback ( this . detectInternal ( args . image ) , args . callback ) ;
119
120
}
121
+
122
+ async detectStart ( inputNumOrCallback , numOrCallback , cb ) {
123
+ const { image, number, callback } = handleArguments (
124
+ inputNumOrCallback ,
125
+ numOrCallback ,
126
+ cb
127
+ ) . require ( "image" , "No input provided." ) ;
128
+
129
+ const detectFrame = async ( ) => {
130
+ await mediaReady ( image , true ) ;
131
+
132
+ await callCallback ( this . detectInternal ( image ) , callback ) ;
133
+
134
+ if ( ! this . signalStop ) {
135
+ requestAnimationFrame ( detectFrame ) ;
136
+ } else {
137
+ this . isPredicting = false ;
138
+ }
139
+ } ;
140
+
141
+ // start the detection
142
+ this . signalStop = false ;
143
+ if ( ! this . isPredicting ) {
144
+ this . isPredicting = true ;
145
+ detectFrame ( ) ;
146
+ }
147
+
148
+ if ( this . prevCall === "start" ) {
149
+ console . warn ( "warning" ) ;
150
+ }
151
+ this . prevCall = "start" ;
152
+ }
153
+
154
+ detectStop ( ) {
155
+ if ( this . isPredicting ) { this . signalStop = true ; }
156
+ this . prevCall = "stop" ;
157
+ }
120
158
}
121
159
122
160
export const CocoSsd = ( ...inputs ) => {
0 commit comments