python - 从 python Opencv Videocapture 接收 Gazebo gstreamer UDP 视频

标签 python opencv tensorflow gstreamer gazebo-simu

我正在尝试实现对象检测Gazebo模拟环境。为此,我使用 https://dev.px4.io/v1.9.0/en/simulation/gazebo.html这个网站。我正在从 QGroundControl 接收视频。此外,我还通过 Python 从 USB 网络摄像头接收视频并检测对象。 现在,我想从 python Opencv videocapture 函数接收 gstreamer UDP 视频。但它给出了错误:

  File "gstreamer_try.py", line 120, in <module>
    feed_dict={image_tensor: image_np_expanded})
  File "/home/hanco/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 956, in run
    run_metadata_ptr)
  File "/home/hanco/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1149, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "/home/hanco/anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

首先, 我尝试编写 gstreamer 发送器和接收器一些视频:

gstreamer_sender.py:

import socket
import numpy as np
import cv2 as cv


addr = ("127.0.0.1", 5655)
buf = 512
width = 640
height = 480
cap = cv.VideoCapture("/home/hanco/Desktop/duckduck.mp4")
cap.set(3, width)
cap.set(3, height)
code = 'start'
code = ('start' + (buf - len(code)) * 'a').encode('utf-8')


if __name__ == '__main__':
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            s.sendto(code, addr)
            data = frame.tostring()
            for i in range(0, len(data), buf):
                s.sendto(data[i:i+buf], addr)
            # cv.imshow('send', frame)
            # if cv.waitKey(1) & 0xFF == ord('q'):
                # break
        else:
            break
    # s.close()
    # cap.release()
    # cv.destroyAllWindows()

gstreamer_receiver.py:

import socket
import numpy as np
import cv2 as cv


addr = ("127.0.0.1", 5600)
buf = 512
width = 640
height = 480
code = b'start'
num_of_chunks = width * height * 3 / buf

if __name__ == '__main__':
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(addr)
    chunk, _ = s.recvfrom(buf)
    chunk.startswith(code)
    start = True
    while True:
        chunks = []
        while len(chunks) < num_of_chunks:
            chunk, _ = s.recvfrom(buf)
            if start:
                chunks.append(chunk)

        byte_frame = b''.join(chunks)

        frame = np.frombuffer(byte_frame, dtype=np.uint8).reshape(width, height, 3)

        cv.imshow('recv', frame)
        if cv.waitKey(1) & 0xFF == ord('q'):
            break

    s.close()
    cv.destroyAllWindows()

它运行良好,我意识到我可以通过仅更改端口来接收来自 Gazebo 的视频。 Gazebo 默认端口为 5600。 但没有成功。

我只想使用上面的代码在这里实现:

cap = cv2.VideoCapture(GSTREAMEAR_VIDEO_INPUT)

这一行出现错误:

          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)

最佳答案

以下代码运行时没有任何错误。

# Read video
video = cv2.VideoCapture("udpsrc port=5600 ! application/x-rtp,payload=96,encoding-name=H264 ! rtpjitterbuffer mode=1 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! appsink", cv2.CAP_GSTREAMER);

关于python - 从 python Opencv Videocapture 接收 Gazebo gstreamer UDP 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58639588/

相关文章:

python - Pandas 系列和 Nan 值不匹配的值

c++ - 这里有什么方法可以使用拉普拉斯算子找出图像是否模糊

python - 匹配特定字符串,忽略其他字符串

python - socket.bind 方法有什么作用?

python - 从python检测macos中的暗模式

python - 如何在 OpenCV 中可靠地将颜色分类为红色、黄色或 "other"颜色?

python - 如何在Python中使用OpenCV查找图片上的最大矩形?

java - 在 Java 中将数据传递给 Tensorflow 模型

python - tensorflow /tflearn 输入形状

python - 一起变换图像和蒙版(Keras 示例)