@@ -53,19 +53,25 @@ async function createDetector() {
53
53
return posedetection . createDetector ( STATE . model , {
54
54
runtime,
55
55
modelType : STATE . modelConfig . type ,
56
- solutionPath :
'https://cdn.jsdelivr.net/npm/@mediapipe/[email protected] '
56
+ solutionPath :
57
+ 'https://cdn.jsdelivr.net/npm/@mediapipe/[email protected] '
57
58
} ) ;
58
59
} else if ( runtime === 'tfjs' ) {
59
60
return posedetection . createDetector (
60
61
STATE . model , { runtime, modelType : STATE . modelConfig . type } ) ;
61
62
}
62
63
case posedetection . SupportedModels . MoveNet :
63
- const modelType = STATE . modelConfig . type == 'lightning' ?
64
- posedetection . movenet . modelType . SINGLEPOSE_LIGHTNING :
65
- posedetection . movenet . modelType . SINGLEPOSE_THUNDER ;
64
+ let modelType ;
65
+ if ( STATE . modelConfig . type == 'lightning' ) {
66
+ modelType = posedetection . movenet . modelType . SINGLEPOSE_LIGHTNING ;
67
+ } else if ( STATE . modelConfig . type == 'thunder' ) {
68
+ modelType = posedetection . movenet . modelType . SINGLEPOSE_THUNDER ;
69
+ } else if ( STATE . modelConfig . type == 'multipose' ) {
70
+ modelType = posedetection . movenet . modelType . MULTIPOSE ;
71
+ }
66
72
if ( STATE . modelConfig . customModel !== '' ) {
67
- return posedetection . createDetector ( STATE . model , { modelType ,
68
- modelUrl : STATE . modelConfig . customModel } ) ;
73
+ return posedetection . createDetector (
74
+ STATE . model , { modelType , modelUrl : STATE . modelConfig . customModel } ) ;
69
75
}
70
76
return posedetection . createDetector ( STATE . model , { modelType} ) ;
71
77
}
@@ -83,13 +89,21 @@ async function checkGuiUpdate() {
83
89
84
90
window . cancelAnimationFrame ( rafId ) ;
85
91
86
- detector . dispose ( ) ;
92
+ if ( detector != null ) {
93
+ detector . dispose ( ) ;
94
+ }
87
95
88
96
if ( STATE . isFlagChanged || STATE . isBackendChanged ) {
89
97
await setBackendAndEnvFlags ( STATE . flags , STATE . backend ) ;
90
98
}
91
99
92
- detector = await createDetector ( STATE . model ) ;
100
+ try {
101
+ detector = await createDetector ( STATE . model ) ;
102
+ } catch ( error ) {
103
+ detector = null ;
104
+ alert ( error ) ;
105
+ }
106
+
93
107
STATE . isFlagChanged = false ;
94
108
STATE . isBackendChanged = false ;
95
109
STATE . isModelChanged = false ;
@@ -125,21 +139,35 @@ async function renderResult() {
125
139
} ) ;
126
140
}
127
141
128
- // FPS only counts the time it takes to finish estimatePoses.
129
- beginEstimatePosesStats ( ) ;
130
-
131
- const poses = await detector . estimatePoses (
132
- camera . video ,
133
- { maxPoses : STATE . modelConfig . maxPoses , flipHorizontal : false } ) ;
142
+ let poses = null ;
143
+
144
+ // Detector can be null if initialization failed (for example when loading
145
+ // from a URL that does not exist).
146
+ if ( detector != null ) {
147
+ // FPS only counts the time it takes to finish estimatePoses.
148
+ beginEstimatePosesStats ( ) ;
149
+
150
+ // Detectors can throw errors, for example when using custom URLs that
151
+ // contain a model that doesn't provide the expected output.
152
+ try {
153
+ poses = await detector . estimatePoses (
154
+ camera . video ,
155
+ { maxPoses : STATE . modelConfig . maxPoses , flipHorizontal : false } ) ;
156
+ } catch ( error ) {
157
+ detector . dispose ( ) ;
158
+ detector = null ;
159
+ alert ( error ) ;
160
+ }
134
161
135
- endEstimatePosesStats ( ) ;
162
+ endEstimatePosesStats ( ) ;
163
+ }
136
164
137
165
camera . drawCtx ( ) ;
138
166
139
167
// The null check makes sure the UI is not in the middle of changing to a
140
168
// different model. If during model change, the result is from an old model,
141
169
// which shouldn't be rendered.
142
- if ( poses . length > 0 && ! STATE . isModelChanged ) {
170
+ if ( poses && poses . length > 0 && ! STATE . isModelChanged ) {
143
171
camera . drawResults ( poses ) ;
144
172
}
145
173
}
0 commit comments