首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Raspberry PI的视频流- Python与raspivid + netcat

Raspberry PI的视频流- Python与raspivid + netcat
EN

Stack Overflow用户
提问于 2019-03-01 17:51:56
回答 1查看 1.5K关注 0票数 1

我正在开发一个使用Raspberry PI 3 B+的网络视频流解决方案,其中低延迟是关键。

我使用的第一种方法是通过管道将标准输出从raspivid传输到netcat TCP流中:

代码语言:javascript
复制
# On the Raspberry:
raspivid -w 640 -h 480 --nopreview -t 0 -o - | nc 192.168.64.104 5000

# On the client:
nc -l -p 5000 | mplayer -nolirc -fps 60 -cache 1024 -

这种方法具有相当低的延迟,我对结果总体上感到满意。

但是,我需要在客户端进行一些图像处理。我所做的就是尝试使用python复制上面的方法。我在documentation of the 'picamera' Python module中找到了类似的解决方案

在覆盆子上:

代码语言:javascript
复制
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(('my_server', 8000))

# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
    camera = picamera.PiCamera()
    camera.resolution = (640, 480)
    # 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'):
        # 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())
        # If we've been capturing for more than 30 seconds, quit
        if time.time() - start > 30:
            break
        # 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()

在客户端:

代码语言:javascript
复制
import io
import socket
import struct
import cv2
import numpy as np

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
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)

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

    cv2.imshow("Frame",imagedisp)
finally:
    connection.close()
    server_socket.close()

这种方法的延迟要糟糕得多,我正在试图找出原因。与第一种方法一样,它使用TCP流从内存缓冲区发送帧。

我们的目标只是尽可能快地准备好帧,以便在客户端使用OpenCV进行处理。因此,如果有人有比上面的更好的方法来实现这一点,如果你能分享它,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2020-04-01 15:35:04

这主要是来自另一个帖子,我现在找不到它。但我对给定的代码做了一些修改。在这一点上,你看到的传输每帧的平均时间为0.35秒,这与netcat相比仍然非常糟糕,但比你提到的顺序捕获代码略好一些。这里也使用了socket,但是你要处理的是视频帧,而不是图片:

server.py

代码语言:javascript
复制
import socket
import sys
import cv2
import pickle
import numpy as np
import struct ## new
import time

HOST='ip address'
PORT=8089

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print ('Socket created')

s.bind((HOST,PORT))
print ('Socket bind complete')
s.listen(10)
print ('Socket now listening')

conn,addr=s.accept()

### new
counter=0
data = b''
payload_size = struct.calcsize("<L") 
while True:
    start=time.time()
    while len(data) < payload_size:
        data += conn.recv(8192)
    packed_msg_size = data[:payload_size]
    data = data[payload_size:]
    msg_size = struct.unpack("<L", packed_msg_size)[0]
    while len(data) < msg_size:
        data += conn.recv(8192)
    frame_data = data[:msg_size]
    data = data[msg_size:]
    ###

    frame=pickle.loads(frame_data)

    name='path/to/your/directory'+str(counter)+'.jpg'
    cv2.imwrite(name,frame)
    counter+=1
    end=time.time()
    print("rate is: " ,end-start)

=

client.py

代码语言:javascript
复制
import cv2
import numpy as np
import socket
import sys
import pickle
import struct ### new code
#cap=cv2.VideoCapture(0)
cap=cv2.VideoWriter()
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('server ip address',8089))
while True:
    ret,frame=cap.read()

    data = pickle.dumps(frame) ### new code
    clientsocket.sendall(struct.pack("<L", len(data))+data) ### new code

=

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54942027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档