How to Batch Multiple Videoframes before run Tensorflow Inference Session

有些话、适合烂在心里 提交于 2019-12-04 11:54:28

Well, I'd just collect batch_size frames and feed them:

batch_size = 5
while video_stream.isActive():
  image_np_list = []
  for _ in range(batch_size):
      image_np_list.append(video_stream.read())
      fps.update()
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.asarray(image_np_list)
  # Actual detection.
  (boxes, scores, classes, num) = sess.run(
      [detection_boxes, detection_scores, detection_classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})

  # Visualization of the results of a detection.
  for i in range(batch_size):
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np_expanded[i],
          boxes[i],
          classes[i].astype(np.int32),
          scores[i],
          category_index,
          use_normalized_coordinates=True,
          line_thickness=bbox_thickness)
          if visualize:
              cv2.imshow('object_detection', image_np_expanded[i])
              # Exit Option
              if cv2.waitKey(1) & 0xFF == ord('q'):
                  break

Of course you'll have to make the relevant changes after that if you're reading the results from the detection, since they will now have batch_size rows.

Be careful though: before tensorflow 1.4 (I think), the object detection API only supports batch size of 1 in image_tensor, so this will not work unless you upgrade your tensorflow.

Also note that your resulting FPS will be an average, but that the frames in a same batch will actually be closer in time than between different batches (since you'll still need to wait for the sess.run() to finish). The average should still be significantly better than your current FPS, although the max time between two consecutive frames should increase.

If you want your frames to all have roughly the same interval between them, I guess you'll need more sophisticated tools like multithreading and queueing: one thread would read the images from the stream and store them in a queue, the other one would take them from the queue and call sess.run() on them asynchronously; it could also tell the 1st thread to hurry up or slow down depending on its own computing capacity. This is trickier to implement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!