Skip to content

Commit 60f3deb

Browse files
Update to work with TF1 or TF2 models
1 parent 40d5751 commit 60f3deb

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

TFLite_detection_video.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@
109109
input_mean = 127.5
110110
input_std = 127.5
111111

112+
# Check output layer name to determine if this model was created with TF2 or TF1,
113+
# because outputs are ordered differently for TF2 and TF1 models
114+
outname = output_details[0]['name']
115+
116+
if ('StatefulPartitionedCall' in outname): # This is a TF2 model
117+
boxes_idx, classes_idx, scores_idx = 1, 3, 0
118+
else: # This is a TF1 model
119+
boxes_idx, classes_idx, scores_idx = 0, 1, 2
120+
112121
# Open video file
113122
video = cv2.VideoCapture(VIDEO_PATH)
114123
imW = video.get(cv2.CAP_PROP_FRAME_WIDTH)
@@ -134,10 +143,9 @@
134143
interpreter.invoke()
135144

136145
# Retrieve detection results
137-
boxes = interpreter.get_tensor(output_details[0]['index'])[0] # Bounding box coordinates of detected objects
138-
classes = interpreter.get_tensor(output_details[1]['index'])[0] # Class index of detected objects
139-
scores = interpreter.get_tensor(output_details[2]['index'])[0] # Confidence of detected objects
140-
#num = interpreter.get_tensor(output_details[3]['index'])[0] # Total number of detected objects (inaccurate and not needed)
146+
boxes = interpreter.get_tensor(output_details[boxes_idx]['index'])[0] # Bounding box coordinates of detected objects
147+
classes = interpreter.get_tensor(output_details[classes_idx]['index'])[0] # Class index of detected objects
148+
scores = interpreter.get_tensor(output_details[scores_idx]['index'])[0] # Confidence of detected objects
141149

142150
# Loop over all detections and draw detection box if confidence is above minimum threshold
143151
for i in range(len(scores)):
@@ -170,4 +178,3 @@
170178
# Clean up
171179
video.release()
172180
cv2.destroyAllWindows()
173-

0 commit comments

Comments
 (0)