socket 전송 이미지 Django + socket

1402 단어 django
-----------------------------------------server-----------------------------------------
import numpy as np
import socket
import demjson
import base64

host='0.0.0.0'
port=9999
ip_port = (host,port)
Asocket = socket.socket()
Asocket.bind(ip_port)
Asocket.listen(5)
Emotion = Emotion()
print('server listening to '+host+':'+str(port)+'....')

while True:
    try:
        conn,addr = Asocket.accept()
        print("connect address:", addr)
        data = conn.recv(1024).decode()
        img_b64decode = base64.b64decode(data) 
        img_array = np.fromstring(img_b64decode,np.uint8)
        img = cv2.imdecode(img_array,cv2.COLOR_BGR2RGB) 
        # import cv2
        # data = cv2.imread("../Emotion/media/face/" + str(data))
        result = Emotion.R(img)
        result = str(result).encode()
        conn.sendall(result)
        conn.close()
    except Exception as e:
        print(e)
-----------------------------------------Client-----------------------------------------
import cv2
import socket
import base64
import numpy as np
import matplotlib.pyplot as plt

s = socket.socket()
host = '127.0.0.1'
port = 9999
s.connect((host, port))

img = open(r'A.jpg','rb')
img = base64.b64encode(img.read())

s.sendall(img)
tips = eval(s.recv(1024).decode())
print(tips)

좋은 웹페이지 즐겨찾기