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

Posted

技术标签:

【中文标题】使用rtsp流时Tensorflow对象检测速度慢【英文标题】:Tensorflow Object Detection Slow when using rtsp stream 【发布时间】:2018-10-27 10:04:15 【问题描述】:

我已按照此处的示例: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

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

【问题讨论】:

在不检测的情况下通过流式传输 IP 摄像机获得多少 fps? 1080p 的 fps 是 30fps 【参考方案1】:

没有 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

注意:即使在低分辨率下,Tensorflow 对象检测也表现得相当好。

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

【讨论】:

OP 表示它可以与网络摄像头配合使用,不过我敢打赌至少 480p @ 30fps。【参考方案2】:

Opencv 的 read() 函数对 usb 网络摄像头和 ipcameras 的工作方式不同。

在 ipcameras 上运行时,它不会读取最新帧,而是读取最旧(下一个)帧。

由于循环中的对象检测推断会占用时间,因此 read() 很快就会落后并正在读取 opencv 缓冲区中最旧的可用帧。

一种解决方案是为相机启动一个线程,该线程读取帧并填充队列。然后在另一个线程中,从该队列中读取帧并对它们运行对象检测推理。

【讨论】:

【参考方案3】:

如果 1080p @ 30fps 在您的网络摄像头上运行良好,但在 RTSP 上无法正常工作,则解码 RTSP 流的额外开销可能对您的 CPU 要求过高。同时执行您要求的两项任务时遇到问题。内存也有可能是瓶颈,尽管这似乎不太可能。

许多英特尔 CPU 都集成了能够原生解码视频的 GPU。但是,我注意到在某些条件下,并且使用某些软件,本机解码选择 CPU 往往会显着滞后(最多约 30 秒)。这也可能是您在这里遇到的问题。可能值得在朋友的计算机上试用该软件,该计算机具有类似质量但不完全相同的硬件。您还可以在相同价格范围的较新硬件上对其进行测试,因为我在最新一代的 Intel CPU 中还没有发现这个问题。

【讨论】:

以上是关于使用rtsp流时Tensorflow对象检测速度慢的主要内容,如果未能解决你的问题,请参考以下文章

当我使用 rtsp 摄像头流时,opencv 视频捕获滞后

MobileVLCKit播放rtsp流时,设置TCP协议的问题

Tensorflow 2.0 Keras 的训练速度比 2.0 Estimator 慢 4 倍

使用 tf.function 的 Tensorflow 2.0 模型非常慢,并且每次列车数量发生变化时都会重新编译。 Eager 的运行速度提高了大约 4 倍

Python:TCP 损坏的路由检测速度非常慢

与 CPU 版本相比,OpenCV GPU 对象检测速度较慢且检测次数较少