Python27。我正在尝试从客户端和服务器应用程序之间的相机流式传输视频。 我正在学习python几天......我已经知道通过UDP发送数据我需要序列化数据......我找到了pickle库但代码对我不起作用。有关数据序列化的更多信息是否能够通过网络发送数据?如何将其划分为较小的数据部分?当我检查类型时,它表示它是大小约为1MB甚至更多的字符串。我猜这就是我收到错误的原因:
socket.error:[Errno 10040]在数据报套接字上发送的消息大于内部消息缓冲区或其他一些网络限制,或者用于接收数据报的缓冲区小于数据报本身< / em>的
TCP版本的工作正常,但性能很差。我想试试UDP 这是我的代码:
__AUTHOR__=u"DK"
import cv2
import numpy as np
import socket
import sys
import pickle
import struct
def StartStreamSending():
u"""
Funkcja przesyłająca stream z lokalnej kamery[0] na serwer.
"""
UDP_IP = "127.0.0.1"
UDP_PORT = 8012
cap=cv2.VideoCapture(0)
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
ret,frame=cap.read()
#result, img_str = cv2.imencode('.jpeg', frame)
#img_str2 = np.array(img_str)
#pik = pickle.Pickler()
#frame = np.reshape(frame, (240,320,3))
data = np.array(frame)
dataToSend = pickle.dumps(data)
size = sys.getsizeof(dataToSend)
print(size)
print(type(dataToSend));
clientsocket.sendto(dataToSend, (UDP_IP, UDP_PORT))
StartStreamSending()
服务器:
__AUTHOR__=u"DK"
import socket
import sys
import cv2
import pickle
import numpy as np
import struct
HOST='localhost'
PORT=8012
def WaitAndReceiveVideoStream(_strHOST, _iPORT):
u"""
Funkcja odbierająca stream z kamery klienta.
"""
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
print 'Socket created'
s.bind((_strHOST,_iPORT))
print 'Socket bind complete'
data = ""
#payload_size = struct.calcsize("L")
i = 0
while True:
data, addr = s.recvfrom(512)
frame=pickle.loads(data)
i = i + 1
print 'coming frame' + str(i)
#frame = numpy.fromstring(data, dtype=numpy.uint8)
#frame = numpy.reshape(frame, (240,320,3))
#if 3 == i :
# cv2.SaveImage("C:\\image.",frame)
cv2.imshow('frame',frame)
cv2.waitKey(4) #Important delay - for test try 2 or 4 value.
s.close()
WaitAndReceiveVideoStream(HOST, PORT)