我正在使用Raspberry Pi 3 B 在网络视频流解决方案上工作,而低潜伏期为关键。
我使用的第一种方法是将raspivid的Stdout输送到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复制上述方法。我在" picamera" python模块的文档中找到了类似的解决方案:
在覆盆子上:
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
==============