python - 在 python 中从 opencv 写入 Gstreamer 管道

标签 python opencv raspberry-pi gstreamer

我正在尝试使用 gstreamer 从 opencv 流式传输一些图像,但我在管道方面遇到了一些问题。一般来说,我是 gstreamer 和 opencv 的新手。我在树莓派 3 上用 gstreamer 为 python3 编译了 opencv 3.2。我有一个与 raspivid 一起使用的小 bash 脚本

raspivid -fps 25 -h 720 -w 1080 -vf -n -t 0 -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.1.27 port=5000

我想翻译这条管道,以便从 opencv 使用它,并将我的算法处理的图像输入其中。我做了一些研究,发现我可以将 videoWriter 与 appsrc 而不是 fdsrc 一起使用,但我收到以下错误

GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error.

顺便说一句,我想出的python脚本如下 导入cv2

cap = cv2.VideoCapture(0)


# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('appsrc  ! h264parse ! '
                      'rtph264pay config-interval=1 pt=96 ! '
                      'gdppay ! tcpserversink host=192.168.1.27 port=5000 ',
                      fourcc, 20.0, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        frame = cv2.flip(frame, 0)

        # write the flipped frame
        out.write(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

流水线有没有错误?我不明白这个错误。我已经有一个可以从 bash 管道读取的 Python 客户端,从延迟和消耗资源的角度来看,结果非常好。

最佳答案

我找到了解决方案,希望这能帮助遇到同样问题的其他人。 管线布置错误,需要videoconvert。 另一方面,延迟非常重要,但将 speed.preset 设置为 ultrafast 可以解决问题,即使那里没有进行太多压缩,这是一个很好的折衷方案。这是我的解决方案。

import cv2

cap = cv2.VideoCapture(0)

framerate = 25.0

out = cv2.VideoWriter('appsrc ! videoconvert ! '
                      'x264enc noise-reduction=10000 speed-preset=ultrafast tune=zerolatency ! '
                      'rtph264pay config-interval=1 pt=96 !'
                      'tcpserversink host=192.168.1.27 port=5000 sync=false',
                      0, framerate, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:

        out.write(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()

关于python - 在 python 中从 opencv 写入 Gstreamer 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544877/

相关文章:

python - 如何使用 Pillow 模块将列表中的图像组粘贴到基础图像之上?

python - OpenCV - 在 python 中创建椭圆形掩模

python - 如何在 Python 中通过 ZeroMQ PUB/SUB 从 Raspberry Pi 接收图像?

python - 能否提高海量时序数据之间相关性分析的计算速度?

python - 搜索链接号码的循环

python - python socket.io客户端无法接收广播消息

针对传送带上的盒子的 OpenCV 特征匹配算法建议

opencv - 计算cvInRangeS中的最小值和最大值

c++ - 如何将timestamp_t转换为实际时间?

Python "in memory DB style"数据类型