tensorRT优化模型的实测FPS值约为70fps或约13ms处理时间。但是,这些帧似乎比实际视频要慢一些,因为我可以清楚地观察到输出帧中的人的运动比原始视频要慢。
因此,我认为我测量fps的方式可能有问题。
这里是代码的重要部分:
times = []
# Detection
with tf.device('/gpu:0'):
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while True:
# Read frame from camera
ret, image_np = cap.read()
if not ret:
print('cap.read() did not return image\n')
break
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Extract image tensor
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Extract detection boxes
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Extract detection scores
scores = detection_graph.get_tensor_by_name('detection_scores:0')
# Extract detection classes
classes = detection_graph.get_tensor_by_name('detection_classes:0')
# Extract number of detectionsd
num_detections = detection_graph.get_tensor_by_name(
'num_detections:0')
# Actual detection.
t1 = time()
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
t2 = time()
times.append(t2-t1)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
# Display output
image_np = cv2.putText(image_np, "Time: {:,.2f}ms | FPS: {:.2f}fps".format(times[-1]*1000, 1/times[-1]),
(0, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 255), 2)
cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# cap.release()
cv2.destroyAllWindows()
if len(times) > 0:
print('Average FPS: {:,.2f}fps'.format(1/(sum(times)/len(times))))
我看不到这些代码有任何反面。