#server
import socket
import pickle
import cv2
import sounddevice as sd
import struct
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s.bind(("127.0.0.1",3000))
#s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65565)
all_data=b''
while(1):
data,addr=s.recvfrom(1024)
all_data=data
data_size=struct.unpack('!L',data[0:4])[0]
print(data_size)
while(len(all_data)<data_size+4):
data,addr=s.recvfrom(1024)
all_data+=data
print(len(all_data))
frame=pickle.loads(all_data[4:])
frame=cv2.imdecode(frame, 1)
cv2.imshow("cesgi",frame)
print("接收完毕")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
all_data=b''
#client
import socket
import cv2
import pickle
import sounddevice as sd
import struct
cap = cv2.VideoCapture(0)
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
while(1):
#my_recording = sd.rec(int(4410), samplerate=44100, channels=1)
ret, frame = cap.read()
_, send_data = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])
data=pickle.dumps(send_data)
data_size=struct.pack('!L',len(data))
all_data=data_size+data
data_list=[all_data[i:i+1024] for i in range(0,len(all_data),1024)]
#audio=pickle.dumps(my_recording)
if ret:
for i in data_list:
s.sendto(i,("127.0.0.1",3000))
#s.sendto(audio,("127.0.0.1",3000))
if cv2.waitKey(1) & 0xFF == ord('q'):
break