【问题标题】:Netcat H.264 Video from Raspivid into OpenCV从 Raspivid 到 OpenCV 的 Netcat H.264 视频
【发布时间】:2017-08-28 17:14:21
【问题描述】:

目标是通过网络将来自 Raspberry Pi (Raspivid/H.264) 的视频流式传输到笔记本电脑上运行的 OpenCV 应用程序中。

打开的CV截图如下(C++):

cv::VideoCapture cap;
cap.open("cam_1"); // cam_1 is a FIFO 

cv::Mat frame;

while(1){
    cap >> frame;
    cv::imshow("", frame);
    cv::waitKey(10);
}

FIFO Stream 创建如下:

mkfifo cam_1

一旦 OpenCV 程序运行,netcat 监听器就会启动:

ncat --recv-only --keep-open --verbose --listen 5001 > cam_1

一旦 netcat 侦听器在笔记本电脑上运行,就会从 Raspberry Pi 启动流

raspivid --verbose --nopreview -b 2000000 --timeout 0 -o - | ncat 192.168.LAPTOP.IP 5001

或者,出于调试目的,笔记本电脑上的本地文件可以流式传输到 netcat:

cat video.h264 | nc 192.168.LAPTOP.IP 5001 

两者都给出以下错误:

无法停止流:设备的 ioctl 不合适 (ERROR)icvOpenAVI_XINE(): 无法初始化视频驱动程序。

有趣的是,如果我在笔记本电脑上启动 Netcat 监听器,然后使用 CTRL+C 将其终止,然后在启动视频流之前再次启动它,使用任一方法... 然后视频播放正确

我无法弄清楚为什么启动 netcat 侦听器然后杀死它,然后重新启动会产生影响或影响是什么。我考虑过可能需要在视频之前将 EOF 或 BOF 回显到 FIFO 中,但我不确定该语法是什么。

我已经尝试了所有口味的 Netcat。

【问题讨论】:

标签: opencv raspberry-pi h.264 netcat


【解决方案1】:

我刚刚使用以下https://stackoverflow.com/a/48675107/2355051解决了这个问题

我最终改编了这个picamera python recipe

在树莓派上:(createStream.py)

import io
import socket
import struct
import time
import picamera

# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('10.0.0.3', 777))

# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
    with picamera.PiCamera() as camera:
        camera.resolution = (1024, 768)
        # Start a preview and let the camera warm up for 2 seconds
        camera.start_preview()
        time.sleep(2)

        # Note the start time and construct a stream to hold image data
        # temporarily (we could write it directly to connection but in this
        # case we want to find out the size of each capture first to keep
        # our protocol simple)
        start = time.time()
        stream = io.BytesIO()
        for foo in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
            # Write the length of the capture to the stream and flush to
            # ensure it actually gets sent
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()

            # Rewind the stream and send the image data over the wire
            stream.seek(0)
            connection.write(stream.read())

            # Reset the stream for the next capture
            stream.seek(0)
            stream.truncate()
    # Write a length of zero to the stream to signal we're done
    connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()

在处理流的机器上:(processStream.py)

import io
import socket
import struct
import cv2
import numpy as np

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 777))
server_socket.listen(0)

# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')
try:
    while True:
        # Read the length of the image as a 32-bit unsigned int. If the
        # length is zero, quit the loop
        image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
        if not image_len:
            break
        # Construct a stream to hold the image data and read the image
        # data from the connection
        image_stream = io.BytesIO()
        image_stream.write(connection.read(image_len))
        # Rewind the stream, open it as an image with opencv and do some
        # processing on it
        image_stream.seek(0)
        image = Image.open(image_stream)

        data = np.fromstring(image_stream.getvalue(), dtype=np.uint8)
        imagedisp = cv2.imdecode(data, 1)

        cv2.imshow("Frame",imagedisp)
        cv2.waitKey(1)  #imshow will not output an image if you do not use waitKey
        cv2.destroyAllWindows() #cleanup windows 
finally:
    connection.close()
    server_socket.close()

此解决方案与我在原始问题中引用的视频具有相似的结果。较大的分辨率帧会增加提要的延迟,但这对于我的应用程序而言是可以容忍的。

首先你需要运行processStream.py,然后在树莓派上执行createStream.py。如果这不起作用,请使用 sudo 执行 python 脚本

【讨论】:

    【解决方案2】:

    如果在 OpenCV 尝试读取 FIFO 之后但在开始流式传输之前触摸 FIFO,那么它将起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2019-08-03
      • 1970-01-01
      相关资源
      最近更新 更多