Python 使用rtsp流时Tensorflow对象检测速度慢
我在这里遵循了这个示例:让对象检测与网络摄像头一起工作 但我已经将我的网络摄像头切换为使用IP摄像头的rtsp流,我认为这是流式H264,现在我注意到视频中有大约30秒的延迟,加上视频有时非常停止-启动 以下是执行主要处理的python代码:Python 使用rtsp流时Tensorflow对象检测速度慢,python,opencv,tensorflow,Python,Opencv,Tensorflow,我在这里遵循了这个示例:让对象检测与网络摄像头一起工作 但我已经将我的网络摄像头切换为使用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
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帧 有两种方法可以使代码实时运行
- 降低帧的分辨率
- 减少fps
提供免费GPU服务(有限时间)。您可以上传代码并在floydhub中运行,并测量性能。我发现GPU的速度大约是CPU的35倍。如果1080p@30fps可以通过网络摄像头正常工作,但不能通过RTSP,那么解码RTSP流的额外开销可能会占用太多CPU。它很难同时完成你要求它完成的两项任务。也有可能内存是瓶颈,尽管这似乎不太可能 许多Intel CPU都集成了GPU,能够对视频进行本机解码。然而,我注意到,在某些条件下,在某些软件中,本机解码选择CPU往往会出现相当大的延迟(高达30秒)。这也可能是你在这里遇到的问题。在朋友的电脑上试用这个软件可能是值得的,它的质量相似,但硬件不同。您还可以在相同价格范围的较新硬件上进行测试,因为我在最新一代Intel CPU中没有看到此问题。Opencv的read()函数对usb网络摄像头和ipcameras的工作方式不同 在ipcameras上运行时,它不会读取最新的帧,而是最旧的(下一个)帧 由于循环中的对象检测推断占用了时间,read()很快就会落后,并且正在读取opencv缓冲区中最旧的可用帧
解决方案是为相机启动一个线程,读取帧并填充队列。然后在另一个线程中,从该队列读取帧,并对其运行对象检测推断 在没有检测的情况下,你能从IP摄像头中获得多少fps?1080p的fps是30fps,这表明它可以与网络摄像头配合使用,不过,我敢打赌,在30fps的情况下至少是480p。
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