Showing
1 changed file
with
53 additions
and
12 deletions
... | @@ -7,24 +7,33 @@ from keras.models import load_model | ... | @@ -7,24 +7,33 @@ from keras.models import load_model |
7 | from keras.preprocessing import image | 7 | from keras.preprocessing import image |
8 | 8 | ||
9 | # import queue | 9 | # import queue |
10 | +import datetime | ||
10 | import numpy as np | 11 | import numpy as np |
11 | from queue import Full, Empty | 12 | from queue import Full, Empty |
12 | from multiprocessing import Process, Queue | 13 | from multiprocessing import Process, Queue |
14 | +import cv2 | ||
13 | 15 | ||
16 | +fname = 'croppedimg/{}.png' | ||
17 | +save_imgs = False | ||
18 | +HOST = '192.168.35.87' | ||
19 | +PORT = 9999 | ||
14 | 20 | ||
15 | class LabelingModule: | 21 | class LabelingModule: |
16 | def __init__(self): | 22 | def __init__(self): |
17 | - # self.model1 = load_model('svhn_model.h5') | 23 | + self.model1 = load_model('checker_model.h5') |
18 | self.model2 = load_model('svhn_model.h5') | 24 | self.model2 = load_model('svhn_model.h5') |
19 | self.image_queue = Queue(maxsize=3000) | 25 | self.image_queue = Queue(maxsize=3000) |
20 | self.label_queue = Queue(maxsize=10) | 26 | self.label_queue = Queue(maxsize=10) |
21 | self.signal_queue = Queue() | 27 | self.signal_queue = Queue() |
22 | - self.predict_process = Process(target=_predict, args=(self.model2, self.image_queue, self.label_queue, self.signal_queue)) | 28 | + self.predict_process = Process(target=_predict, \ |
29 | + args=( | ||
30 | + self.model1, self.model2, self.image_queue, self.label_queue, self.signal_queue)) | ||
23 | 31 | ||
24 | def run(self): | 32 | def run(self): |
25 | self.predict_process.start() | 33 | self.predict_process.start() |
26 | 34 | ||
27 | def close(self): | 35 | def close(self): |
36 | + self.signal_queue.put_nowait('stop') | ||
28 | self.image_queue.close() | 37 | self.image_queue.close() |
29 | self.label_queue.close() | 38 | self.label_queue.close() |
30 | 39 | ||
... | @@ -49,9 +58,26 @@ class LabelingModule: | ... | @@ -49,9 +58,26 @@ class LabelingModule: |
49 | img_tensor = img_tensor - img_tensor.mean() | 58 | img_tensor = img_tensor - img_tensor.mean() |
50 | return img_tensor | 59 | return img_tensor |
51 | 60 | ||
61 | +def decode(output): | ||
62 | + if(output[0]==0): | ||
63 | + return 'Noise' | ||
64 | + else: | ||
65 | + if(output[1] == 3): | ||
66 | + return str(output[2])+str(output[3])+str(output[4]) | ||
67 | + elif (output[1] == 4): | ||
68 | + return str(output[2]) + str(output[3]) + str(output[4])+'-'+ str(output[5]) | ||
52 | 69 | ||
53 | -def _predict(model, input_queue, output_queue, signal_queue): | 70 | +def send_predict_result(HOST, PORT,message): |
71 | + # (address family) IPv4, TCP | ||
72 | + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
73 | + # raspberry pi addr | ||
74 | + client_socket.connect((HOST, PORT)) | ||
75 | + client_socket.sendall('Door Plate Detected : '+message.encode('utf-8')) | ||
76 | + | ||
77 | +def _predict(model1, model2, input_queue, output_queue, signal_queue): | ||
54 | print('predict process started.') | 78 | print('predict process started.') |
79 | + | ||
80 | + index = 0 | ||
55 | while True: | 81 | while True: |
56 | try: | 82 | try: |
57 | signal = signal_queue.get_nowait() | 83 | signal = signal_queue.get_nowait() |
... | @@ -60,13 +86,28 @@ def _predict(model, input_queue, output_queue, signal_queue): | ... | @@ -60,13 +86,28 @@ def _predict(model, input_queue, output_queue, signal_queue): |
60 | except Empty: | 86 | except Empty: |
61 | pass | 87 | pass |
62 | 88 | ||
63 | - tensor = input_queue.get(timeout=-1) | 89 | + try: |
64 | - dat = model.predict(np.array([tensor])) | 90 | + tensor = input_queue.get(timeout=-1) |
65 | - o1 = np.argmax(dat[0]) | 91 | + except Empty: |
66 | - o2 = np.argmax(dat[1]) | 92 | + continue |
67 | - o3 = np.argmax(dat[2]) | 93 | + tensor = np.array([tensor]) |
68 | - o4 = np.argmax(dat[3]) | 94 | + has_number = model1.predict(tensor)[0] |
69 | - o5 = np.argmax(dat[4]) | 95 | + if int(has_number[0]) == 1: |
70 | - o6 = np.argmax(dat[5]) | 96 | + continue |
97 | + | ||
98 | + if save_imgs: | ||
99 | + img = cv2.cvtColor(tensor[0], cv2.COLOR_RGB2BGR) | ||
100 | + cv2.imwrite(fname.format(index), img) | ||
101 | + index += 1 | ||
102 | + | ||
103 | + label_data = model2.predict(tensor) | ||
104 | + o1 = np.argmax(label_data[0]) | ||
105 | + o2 = np.argmax(label_data[1]) | ||
106 | + o3 = np.argmax(label_data[2]) | ||
107 | + o4 = np.argmax(label_data[3]) | ||
108 | + o5 = np.argmax(label_data[4]) | ||
109 | + o6 = np.argmax(label_data[5]) | ||
71 | output = [o1, o2, o3, o4, o5, o6] | 110 | output = [o1, o2, o3, o4, o5, o6] |
72 | - print('[LabelingModule] predict result :', output) | 111 | + print('[LabelingModule] predict result :', decode(output)) |
112 | + send_predict_result(HOST,PORT) | ||
113 | + print(decode(output)+" : Sended To Edge") | ... | ... |
-
Please register or login to post a comment