python - 使用rtsp流时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
我是 python 和 tensorflow 的新手。是否应该以任何方式修改此代码以应对 rtsp 流?我的电脑没有 GPU 卡。
解决方案
没有 GPU,Tensorflow 无法以高 fps 处理高质量帧。在我的机器上处理一个 640*480 帧花了将近 0.2 秒。所以它每秒可以处理大约 5 帧。
有两种方法可以使代码实时运行。
- 降低帧的分辨率
- 降低帧率
代码
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 倍。
推荐阅读
- c# - WPF:KO 无法同步 StoryBoards 动画 + 无法更改应用程序主题
- c# - 使所有方法的 Moq 输出调用到控制台
- xcode - 为什么我第一次在 Xcode 10 中打开 Playground 时会显示“无编辑器”的空白屏幕?
- python - 根据第三个变量给线图上色
- php - Laravel equivalent if statement
- python - 从 csv 读取数据并将其存储到 python 数据结构中
- excel - excel嵌套if和语句 - 看不出为什么它不起作用
- reactjs - 进行更改后将 Power BI 嵌入式报表保存在数据库中
- sql - ORDER BY 子句中区分大小写的默认选项
- reactjs - 无法拖动 React 引导选项卡 - React Beautiful dnd