이재빈

Core Cloud 전처리 프레임 수신 및 예측모듈 작성

1 +import socket
2 +import cv2
3 +import numpy as np
4 +from multiprocessing import Queue
5 +
6 +from labeling_module import LabelingModule
7 +#socket에서 수신한 버퍼를 반환함.
8 +def recvall(sock, count):
9 + # 바이트 문자열
10 + buf = b''
11 + while count:
12 + newbuf = sock.recv(count)
13 + if not newbuf: return None
14 + buf += newbuf
15 + count -= len(newbuf)
16 + return buf
17 +
18 +
19 +if __name__ == "__main__":
20 + lm.predict_process.start()
21 + HOST='127.0.0.1'
22 + PORT=9999
23 +
24 + #TCP 사용
25 + s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
26 + print('Socket created')
27 +
28 + #서버의 아이피와 포트번호 지정
29 + s.bind((HOST,PORT))
30 + print('Socket bind complete')
31 + # 클라이언트의 접속을 기다린다. (클라이언트 연결을 10개까지 받는다)
32 + s.listen(10)
33 + print('Socket now listening')
34 +
35 + #연결, conn에는 소켓 객체, addr은 소켓에 바인드 된 주소
36 + conn,addr=s.accept()
37 + while True:
38 + # client에서 받은 stringData의 크기 (==(str(len(stringData))).encode().ljust(16))
39 + length = recvall(conn, 16)
40 + stringData = recvall(conn, int(length))
41 + data = np.fromstring(stringData, dtype = 'uint8')
42 +
43 + #data를 디코딩한다.
44 + cropped = cv2.imdecode(data, cv2.IMREAD_COLOR)
45 + 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
1 +# plaidml
2 +# import plaidml.keras
3 +# plaidml.keras.install_backend()
4 +
5 +# packages
6 +from keras.models import load_model
7 +from keras.preprocessing import image
8 +
9 +# import queue
10 +import numpy as np
11 +from queue import Full, Empty
12 +from multiprocessing import Process, Queue
13 +
14 +class LabelingModule:
15 + def __init__(self):
16 + # self.model1 = load_model('svhn_model.h5')
17 + self.model2 = load_model('svhn_model.h5')
18 + self.image_queue = Queue(maxsize=3000)
19 + self.label_queue = Queue(maxsize=10)
20 + self.signal_queue = Queue()
21 + self.predict_process = Process(target=_predict, \
22 + args=(self.model2, self.image_queue, self.label_queue, self.signal_queue))
23 +
24 + def run(self):
25 + self.predict_process.start()
26 +
27 + def close(self):
28 + self.image_queue.close()
29 + self.label_queue.close()
30 +
31 + def new_tensor(self, tensor):
32 + try:
33 + self.image_queue.put(tensor)
34 + except Full:
35 + print('[LabelingModule] image_queue is full')
36 +
37 + def new_image(self, filename):
38 + tensor = self._img_to_tensor(filename)
39 + try:
40 + self.image_queue.put(tensor)
41 + except Full:
42 + print('[LabelingModule] image_queue is full')
43 +
44 + def _img_to_tensor(self, filename):
45 + img = image.load_img(filename, target_size=(48, 48))
46 + img_tensor = image.img_to_array(img)
47 + img_tensor = np.squeeze(img_tensor)
48 + img_tensor /= 255.
49 + img_tensor = img_tensor - img_tensor.mean()
50 + return img_tensor
51 +
52 +def _predict(model, input_queue, output_queue, signal_queue):
53 + print('predict process started.')
54 + while True:
55 + try:
56 + signal = signal_queue.get_nowait()
57 + if signal == 'stop':
58 + break
59 + except Empty:
60 + pass
61 +
62 + tensor = input_queue.get(timeout=-1)
63 + dat = model.predict(np.array([tensor]))
64 + o1 = np.argmax(dat[0])
65 + o2 = np.argmax(dat[1])
66 + o3 = np.argmax(dat[2])
67 + o4 = np.argmax(dat[3])
68 + o5 = np.argmax(dat[4])
69 + o6 = np.argmax(dat[5])
70 + output = [o1, o2, o3, o4, o5, o6]
71 + print('[LabelingModule] predict result :', output)