TCP传输图片

import socket
import pickle
import numpy as np
from PIL import Image
import io
import sys
import threading

def main(img):
# start_svc = datetime.datetime.now()
try:

img = img.convert('L')
img = img.resize((256,256))
# print(img.mode)
# print(img.size)
arr = np.asarray(img, dtype="float32")
# arr = arr.flatten()
arr = arr.reshape(1,-1)
#data.append(arr)


# name = "" + job_name + ".pkl"
# name = "AAA.pkl"

#end_svc = datetime.datetime.now()

pca_data = pca.transform(arr)
y_test_pred = svc.predict(pca_data)


#time = end_svc - start_svc
#print(time)
return y_test_pred

except Exception as err:

return str(err)



len_rev=0
def tcp_connected(s,addr):
print('Accept new connection from %s:%s...' % addr)
while 1:

data =s.recv(1600000)
len_rev=len(data)
# print(len_rev)
if len_rev>=1000000:
image = Image.open(io.BytesIO(data))
result=main(image)[0]
s.send(result.encode())
print(result)
elif len_rev<=100 and data.decode()== 'close':
sock.close()
sys.exit(0)

arg1 = "" + sys.argv[1]

s = socket.socket() # 创建 socket 对象


all_port=[6001,42683]
arg=int(arg1)
port=all_port[arg]
print(port)

#path_to_watch=r'image/Cam{}/'.format(arg+1)
s.bind(('127.0.0.1', port)) # 绑定端口

s.listen(2) # 监听连接,传入连接请求的最大数5

# 接受一个新连接
sock,ad=s.accept()
# 创建新线程来处理TCP连接
name = "SVC_PCA.pkl"
pca, svc = pickle.load(open(name, 'rb'))

t = threading.Thread(tcp=tcp_connected(sock, ad))
------ 本文结束 🎉🎉 谢谢观看 ------
0%