【发布时间】:2019-05-17 17:58:36
【问题描述】:
我一直在做一个项目,我使用树莓派将实时视频源发送到我的服务器。这有点工作,但不是我想要的。 问题主要是速度。现在我可以以大约 3.5 FPS 的速度发送一个 640x480 的视频流,以大约 0.5 FPS 的速度发送一个 1920x1080 的视频流,这太糟糕了。由于我不是专业人士,我认为应该有一种方法可以改进我的代码。
发件人(树莓派):
def send_stream():
connection = True
while connection:
ret,frame = cap.read()
if ret:
# You might want to enable this while testing.
# cv2.imshow('camera', frame)
b_frame = pickle.dumps(frame)
b_size = len(b_frame)
try:
s.sendall(struct.pack("<L", b_size) + b_frame)
except socket.error:
print("Socket Error!")
connection = False
else:
print("Received no frame from camera, exiting.")
exit()
接收方(服务器):
def recv_stream(self):
payload_size = struct.calcsize("<L")
data = b''
while True:
try:
start_time = datetime.datetime.now()
# keep receiving data until it gets the size of the msg.
while len(data) < payload_size:
data += self.connection.recv(4096)
# Get the frame size and remove it from the data.
frame_size = struct.unpack("<L", data[:payload_size])[0]
data = data[payload_size:]
# Keep receiving data until the frame size is reached.
while len(data) < frame_size:
data += self.connection.recv(32768)
# Cut the frame to the beginning of the next frame.
frame_data = data[:frame_size]
data = data[frame_size:]
frame = pickle.loads(frame_data)
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
end_time = datetime.datetime.now()
fps = 1/(end_time-start_time).total_seconds()
print("Fps: ",round(fps,2))
self.detect_motion(frame,fps)
self.current_frame = frame
except (socket.error,socket.timeout) as e:
# The timeout got reached or the client disconnected. Clean up the mess.
print("Cleaning up: ",e)
try:
self.connection.close()
except socket.error:
pass
self.is_connected = False
break
【问题讨论】:
-
过了一会儿,我尝试在本地机器上模仿这种情况(所以没有涉及树莓派)。我使用了 connection.recv() 中的值,当我增加第二个值时得到了显着更高的 fps。但遗憾的是,在将这个解决方案应用于树莓派之后,这并没有改变任何东西......所以我的结论是,问题出在树莓派方面。
-
我真的很想知道 C++ 是否会比 python 有所改进。
标签: python opencv networking stream frame-rate