英文:
some issue with sending numpy arrays over socket
问题
我想创建一个Web服务器,用于从连接的用户(单数)中流式传输实时网络摄像头视频。
目前,我正在尝试通过套接字发送来自网络摄像头的视频帧,但出现了一个奇怪的问题。客户端在大约0.1秒后停止发送帧。
以下是server.py的输出以及完整的输出。客户端停止发送帧,如我所说。
与('127.0.0.1', 63585)建立连接
[[[138 157 155]
[142 160 158]
[145 161 157]
...
[ 60 67 94]
[ 57 64 90]
[ 67 75 99]]
[[144 160 159]
[144 160 157]
[145 160 156]
...
[ 67 73 99]
[ 63 70 94]
[ 71 78 101]]
[ 14 20 29]
[ 13 21 28]
...
[ 25 35 36]
[ 23 33 34]
[ 22 32 32]]
[[ 15 19 28]
[ 15 20 29]
[ 15 21 28]
...
[ 22 33 34]
[ 22 33 33]
[ 21 32 32]]
[[ 16 19 28]
[ 16 20 29]
[ 16 21 29]
...
[ 20 33 32]
[ 20 33 32]
[ 20 33 32]]]
我创建了2个Python文件:server.py和client.py
server.py
import cv2
import pickle
import socket
import struct
TCP_IP = '127.0.0.1'
TCP_PORT = 9501
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立TCP连接
sock.bind((TCP_IP, TCP_PORT))
sock.listen(5)
data = b''
payload_size = struct.calcsize("I")
while True:
client_socket, client_address = sock.accept() # 等待客户端连接
print('与 ' ,client_address, '建立连接')
while len(data) < payload_size:
data += client_socket.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("I", packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
if frame_data=='':
break
frame=pickle.loads(frame_data)
print(frame)
sock.close()
client.py
import cv2
import pickle
import socket
import struct
TCP_IP = '127.0.0.1'
TCP_PORT = 9501
server_address = (TCP_IP, TCP_PORT)
streamer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
streamer.connect((TCP_IP,TCP_PORT))
while True:
cap = cv2.VideoCapture(0)
pos_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
frame = pickle.dumps(frame)
size = len(frame)
p = struct.pack('I', size)
frame = p + frame
streamer.sendall(frame)
else:
cap.set(cv2.CAP_PROP_POS_FRAMES, pos_frame-1)
if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
size = 10
p = struct.pack("I", size)
streamer.send(p)
streamer.send('')
break
streamer.close()
英文:
I want to create a web server that will stream live webcam video from connected user(singular).
Currently im trying to send video frames coming from webcam over socket but having a strange issue. The client stops sending frames after like 0.1 secs.
Here is the output from server.py and its full output. Client stops sending frames as i said.
connection established with ('127.0.0.1', 63585)
[[[138 157 155]
[142 160 158]
[145 161 157]
...
[ 60 67 94]
[ 57 64 90]
[ 67 75 99]]
[[144 160 159]
[144 160 157]
[145 160 156]
...
[ 67 73 99]
[ 63 70 94]
[ 71 78 101]]
[ 14 20 29]
[ 13 21 28]
...
[ 25 35 36]
[ 23 33 34]
[ 22 32 32]]
[[ 15 19 28]
[ 15 20 29]
[ 15 21 28]
...
[ 22 33 34]
[ 22 33 33]
[ 21 32 32]]
[[ 16 19 28]
[ 16 20 29]
[ 16 21 29]
...
[ 20 33 32]
[ 20 33 32]
[ 20 33 32]]]
I created 2 python files: server.py and client.py
server.py
import cv2
import pickle
import socket
import struct
TCP_IP = '127.0.0.1'
TCP_PORT = 9501
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # establishing a tcp connection
sock.bind((TCP_IP, TCP_PORT))
sock.listen(5)
data = b''
payload_size = struct.calcsize("I")
while True:
client_socket, client_address = sock.accept() # wait for client
print('connection established with ' ,client_address)
while len(data) < payload_size:
data += client_socket.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("I", packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
if frame_data=='':
break
frame=pickle.loads(frame_data)
print(frame)
socket.close()
client.py
import cv2
import pickle
import socket
import struct
TCP_IP = '127.0.0.1'
TCP_PORT = 9501
server_address = (TCP_IP, TCP_PORT)
streamer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
streamer.connect((TCP_IP,TCP_PORT))
while True:
cap = cv2.VideoCapture(0)
pos_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
frame = pickle.dumps(frame)
size = len(frame)
p = struct.pack('I', size)
frame = p + frame
streamer.sendall(frame)
else:
cap.set(cv2.CAP_PROP_POS_FRAMES, pos_frame-1)
if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
size = 10
p = struct.pack("I", size)
streamer.send(p)
streamer.send('')
break
streamer.close()
答案1
得分: 1
@malcom32发布了这个有效的解决方案。出于完整性考虑,我在这里发布答案。
由于您只需要设置一次套接字,您可以将这行代码放在server.py的while循环之外。
client_socket, client_address = sock.accept()
while True:
print('已建立连接:', client_address)
...
英文:
@malcom32 posted this worked. I'm posting the answer here for completeness.
Since you only need to set up the socket once, you can put this line outside the server.py while loop.
client_socket, client_address = sock.accept()
while True:
print('connection established with ' ,client_address)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论