使用rtsp流时,Tensorflow对象检测速度很慢

Har*_*Boy 13 python opencv tensorflow

我按照这里的示例:https://www.youtube.com/watch?v = MoMjIwGSFVQ并使用网络摄像头进行对象检测.

但我已经将我的网络摄像头转换为使用来自IP摄像机的rtsp流,我相信它正在流式传输H264 ,我现在注意到视频中有大约30秒的延迟,而且视频有时会停止启动.

这是执行主要处理的python代码:

import cv2
cap = cv2.VideoCapture("rtsp://192.168.200.1:5544/stream1")

# Running the tensorflow session
with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
   ret = True
   while (ret):
      ret,image_np = cap.read()

      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      image_np_expanded = np.expand_dims(image_np, axis=0)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

      # Each box represents a part of the image where a particular object was detected.
      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

      # Each score represent how level of confidence for each of the objects.
      # Score is shown on the result image, together with the class label.
      scores = detection_graph.get_tensor_by_name('detection_scores:0')
      classes = detection_graph.get_tensor_by_name('detection_classes:0')
      num_detections = detection_graph.get_tensor_by_name('num_detections:0')

      # Actual detection.
      (boxes, scores, classes, num_detections) = sess.run(
      [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})

      # 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)

#      plt.figure(figsize=IMAGE_SIZE)
#      plt.imshow(image_np)
      cv2.imshow('image',cv2.resize(image_np,(1280,960)))
      if cv2.waitKey(25) & 0xFF == ord('q'):
          cv2.destroyAllWindows()
          cap.release()
          break
Run Code Online (Sandbox Code Playgroud)

我是python和tensorflow的新手.是否应以任何方式修改此代码以应对rtsp流?我的电脑没有GPU卡.

Sre*_*A R 6

没有GPU Tensorflow无法以高fps处理高质量的帧.在我的机器中处理640*480帧花了将近0.2秒.因此它可以处理大约每秒5帧.

有两种方法可以使代码实时运行.

  • 降低帧的分辨率
  • 减少fps

cap = cv2.VideoCapture("rtsp://192.168.200.1:5544/stream1")
cap.set(3,640) #set frame width
cap.set(4,480) #set frame height
cap.set(cv2.cv.CV_CAP_PROP_FPS, 5) #adjusting fps to 5
Run Code Online (Sandbox Code Playgroud)

注意:即使在低分辨率下,Tensorflow对象检测也能很好地执行.

要体验GPU性能,floydhub 提供免费的GPU服务(限时).您可以上传代码并在floydhub中运行并测量性能.我发现GPU比CPU快35倍.