Showing
1 changed file
with
37 additions
and
10 deletions
... | @@ -2,8 +2,10 @@ import socket | ... | @@ -2,8 +2,10 @@ import socket |
2 | import cv2 | 2 | import cv2 |
3 | import numpy as np | 3 | import numpy as np |
4 | from multiprocessing import Queue | 4 | from multiprocessing import Queue |
5 | - | 5 | +from queue import Queue |
6 | -from labeling_module import LabelingModule | 6 | +from _thread import * |
7 | +from labeling_module import LabelingModule as lm | ||
8 | +enclose_q = Queue() | ||
7 | #socket에서 수신한 버퍼를 반환 | 9 | #socket에서 수신한 버퍼를 반환 |
8 | def recvall(sock, count): | 10 | def recvall(sock, count): |
9 | # 바이트 문자열 | 11 | # 바이트 문자열 |
... | @@ -14,34 +16,59 @@ def recvall(sock, count): | ... | @@ -14,34 +16,59 @@ def recvall(sock, count): |
14 | buf += newbuf | 16 | buf += newbuf |
15 | count -= len(newbuf) | 17 | count -= len(newbuf) |
16 | return buf | 18 | return buf |
17 | - | 19 | +def send_threaded(Client_socket, addr, queue): |
20 | + print("Connected by : ", addr[0], " : ", addr[1]) | ||
21 | + while True: | ||
22 | + try : | ||
23 | + data = Client_socket.recv(1024) | ||
24 | + if not data: | ||
25 | + print("Disconnected") | ||
26 | + break | ||
27 | + StringData = queue.get() | ||
28 | + Client_socket.send(str(len(StringData)).ljust(16).encode()) | ||
29 | + Client_socket.send(StringData) | ||
30 | + except ConnectionResetError as e: | ||
31 | + print("Disconnected") | ||
32 | + Client_socket.close() | ||
18 | 33 | ||
19 | if __name__ == "__main__": | 34 | if __name__ == "__main__": |
20 | lm.predict_process.start() | 35 | lm.predict_process.start() |
21 | - HOST='127.0.0.1' | 36 | + RECV_HOST='127.0.0.1' |
22 | - PORT=9999 | 37 | + RECV_PORT=9999 #RECV PORT |
23 | 38 | ||
24 | #TCP 사용 | 39 | #TCP 사용 |
25 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) | 40 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
26 | print('Socket created') | 41 | print('Socket created') |
27 | 42 | ||
28 | #CoreCloud IP, PortNumber set | 43 | #CoreCloud IP, PortNumber set |
29 | - s.bind((HOST,PORT)) | 44 | + s.bind((RECV_HOST,RECV_PORT)) |
30 | print('Socket bind complete') | 45 | print('Socket bind complete') |
31 | - # Edge Cloud 접속wait (클라이언트 연결을 10개까지 받는다) | 46 | + # Edge Cloud 접속wait (클라이언트 연결을 10개까지 받음) |
32 | s.listen(10) | 47 | s.listen(10) |
33 | print('Socket now listening') | 48 | print('Socket now listening') |
34 | 49 | ||
35 | #연결, conn 소켓 객체, addr socket binded addr | 50 | #연결, conn 소켓 객체, addr socket binded addr |
36 | conn,addr=s.accept() | 51 | conn,addr=s.accept() |
52 | + | ||
53 | + SEND_HOST = '127.0.0.1' | ||
54 | + SEND_PORT = 9998 #SEND PORT | ||
55 | + | ||
56 | + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
57 | + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
58 | + server_socket.bind((SEND_HOST, SEND_PORT)) | ||
59 | + server_socket.listen() | ||
37 | while True: | 60 | while True: |
38 | # client에서 받은 stringData length (==(str(len(stringData))).encode().ljust(16)) | 61 | # client에서 받은 stringData length (==(str(len(stringData))).encode().ljust(16)) |
39 | length = recvall(conn, 16) | 62 | length = recvall(conn, 16) |
40 | stringData = recvall(conn, int(length)) | 63 | stringData = recvall(conn, int(length)) |
41 | data = np.fromstring(stringData, dtype = 'uint8') | 64 | data = np.fromstring(stringData, dtype = 'uint8') |
42 | - | ||
43 | #data decode | 65 | #data decode |
44 | cropped = cv2.imdecode(data, cv2.IMREAD_COLOR) | 66 | cropped = cv2.imdecode(data, cv2.IMREAD_COLOR) |
45 | cropped = cv2.resize(cropped, (48,48)) #Crop Image Resize | 67 | cropped = cv2.resize(cropped, (48,48)) #Crop Image Resize |
46 | - lm.new_tensor(cropped) # Predict result | ||
47 | - lm.predict_process.join() # thread join | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
68 | + result = lm.new_tensor(cropped) # Predict result | ||
69 | + lm.predict_process.join() # thread join | ||
70 | + edge_socket, addr = server_socket.accept() | ||
71 | + enclose_q.put(result) | ||
72 | + start_new_thread(send_threaded, (edge_socket, addr, enclose_q,)) | ||
73 | + if(conn): #연결 끊어질 경우 loop 탈출 | ||
74 | + break | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment