gpt4 book ai didi

python - 来自Raspberry PI的视频流-Python与raspivid + netcat

转载 作者:行者123 更新时间:2023-12-02 16:37:20 26 4
gpt4 key购买 nike

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

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

# 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中找到了类似的解决方案:

在树莓上:
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()

在客户端上:
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进行处理。因此,如果有比上述方法更好的方法来实现这一目标,我将不胜感激。

最佳答案

这主要来自我现在找不到的另一篇文章。但是我在那里修改了给定的代码。在这一帧上,平均每帧传输时间为0.35秒,与netcat相比仍然很差,但比您提到的顺序捕获代码稍好。这个也使用套接字,但是代替图片,您处理视频帧:

server.py

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
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

=============

关于python - 来自Raspberry PI的视频流-Python与raspivid + netcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com