我正在研究使用Raspberry PI 3 B +的网络视频流解决方案,而低延迟是关键。
我使用的第一种方法是将标准输出从raspivid传递到netcat TCP流中:
a_i
这种方法的延迟很短,我对结果总体上满意。
但是,我需要在客户端进行一些图像处理。我所做的是尝试使用python复制上述方法。我在documentation of the 'picamera' Python module中找到了类似的解决方案:
在树莓上:
# 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 -
在客户端上:
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()
此方法的延迟要差得多,我正在尝试找出原因。与第一种方法一样,它使用TCP流从内存缓冲区发送帧。
目标只是尽可能快地准备好帧以在客户端上使用OpenCV进行处理。因此,如果有比上述方法更好的方法来实现这一目标,请与我分享。
答案 0 :(得分:0)
这主要来自另一篇我现在找不到的帖子。但是我在那里修改了给定的代码。在这一帧上,平均每帧传输时间为0.35秒,与netcat相比仍然很差,但比您提到的顺序捕获代码要好一些。这个也使用套接字,但是代替图片,您处理视频帧:
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)
=============
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
=============