我正在创建一个通过套接字发送视频流的程序,并且正在使用pickle加载/转储我的图像numpy数组。我正在快速发送图片,因此可以快速接收帧并从中制作实时视频。尝试此操作时,出现错误 _pickle.UnpicklingError:无效的加载密钥'<'。
我试图不断地将数组加载到pickle(pickle.loads)中,并使用cv2打开框架,但是在几帧之后,它给出了错误 _pickle.UnpicklingError:无效的加载键'<'
Server.py (主要焦点是send_commands())
import os
import time
import socket
import sys
import threading
import cv2
import numpy as np
import pickle
loggedin = 1
all_addresses = []
all_connections = []
def socket_create():
if loggedin == 0:
return
try:
global host
global port
global s
host = ''
port = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Socket error > " + str(e))
return
def socket_bind():
try:
global host
global port
global s
host = ''
print("Socket binding - Port > " + str(port))
print(host + str(port))
s.bind((host, int(port)))
s.listen(1)
except socket.error as e:
print("Socket binding - Error > " + str(e))
time.sleep(3)
socket_bind()
def accept_connections():
for c in all_connections:
c.close()
del all_connections[:]
del all_addresses[:]
while True:
try:
conn, address = s.accept()
conn.setblocking(1)
all_connections.append(conn)
all_addresses.append(address)
print("\nC - IP > " + str(address[0]))
except socket.error as e:
print("Error: " + str(e))
time.sleep(3)
time.sleep(1)
def send_commands():
print("send")
time.sleep(2)
conn = all_connections[0]
conn.send(str.encode("#Video"))
while True:
p = conn.recv(40960000)
data_arr = pickle.loads(p, fix_imports=True, encoding="bytes")
frame = cv2.cvtColor(data_arr, cv2.COLOR_RGB2BGR)
cv2.imshow("frame", frame)
print("go")
cv2.waitKey(10)
def go():
socket_create()
socket_bind()
a = threading.Thread(name='background', target=accept_connections)
a.start()
send_commands()
go()
Client.py (主要焦点是img())
import threading
import time
import os
import sys
import socket
import pyscreenshot
import pickle
import numpy as np
from PIL import ImageGrab
host = "192.168.1.27"
port = 9999
def go():
global s
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
while True:
data = s.recv(40960000)
if data.decode("utf-8") != "#Video":
return
while True:
img()
print("img")
except:
print("Error")
time.sleep(2)
def img():
img = ImageGrab.grab()
img_np = np.array(img)
data_string = pickle.dumps(img_np)
s.sendall(data_string)
if __name__ == '__main__':
go()