@@ -153,6 +153,15 @@ def stop(self):
153
153
input_mean = 127.5
154
154
input_std = 127.5
155
155
156
+ # Check output layer name to determine if this model was created with TF2 or TF1,
157
+ # because outputs are ordered differently for TF2 and TF1 models
158
+ outname = output_details [0 ]['name' ]
159
+
160
+ if ('StatefulPartitionedCall' in outname ): # This is a TF2 model
161
+ boxes_idx , classes_idx , scores_idx = 1 , 3 , 0
162
+ else : # This is a TF1 model
163
+ boxes_idx , classes_idx , scores_idx = 0 , 1 , 2
164
+
156
165
# Initialize frame rate calculation
157
166
frame_rate_calc = 1
158
167
freq = cv2 .getTickFrequency ()
@@ -185,10 +194,9 @@ def stop(self):
185
194
interpreter .invoke ()
186
195
187
196
# Retrieve detection results
188
- boxes = interpreter .get_tensor (output_details [0 ]['index' ])[0 ] # Bounding box coordinates of detected objects
189
- classes = interpreter .get_tensor (output_details [1 ]['index' ])[0 ] # Class index of detected objects
190
- scores = interpreter .get_tensor (output_details [2 ]['index' ])[0 ] # Confidence of detected objects
191
- #num = interpreter.get_tensor(output_details[3]['index'])[0] # Total number of detected objects (inaccurate and not needed)
197
+ boxes = interpreter .get_tensor (output_details [boxes_idx ]['index' ])[0 ] # Bounding box coordinates of detected objects
198
+ classes = interpreter .get_tensor (output_details [classes_idx ]['index' ])[0 ] # Class index of detected objects
199
+ scores = interpreter .get_tensor (output_details [scores_idx ]['index' ])[0 ] # Confidence of detected objects
192
200
193
201
# Loop over all detections and draw detection box if confidence is above minimum threshold
194
202
for i in range (len (scores )):
0 commit comments