-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialGUI.py
207 lines (179 loc) · 6.62 KB
/
SerialGUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import serial
import numpy as np
import tkinter as tk
import cv2
import argparse
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from utils.detection import get_bounding_boxes
from utils.TfliteInference import self_invoke
from time import gmtime, strftime
from PIL import Image, ImageTk
# program parameters
IMG_SIZE = (192, 192)
PREDICTION_LEN = 24 * 24 * 24 # grid_len * grid_len * yolo
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="specify the port of WE-1 Plus",
dest="port", default="/dev/ttyUSB0")
parser.add_argument("-b", "--baud", help="specify the baud rate of transferring\
image and predictions",
dest="baud", default=921600)
parser.add_argument("-t", "--thresh", help="specify the thresh of detecting",
dest="thresh", default=0.15)
parser.add_argument("-i", "--iou_thresh", help="specify the iou thresh of\
detecting",
dest="iou_thresh", default=0.1)
args = parser.parse_args()
PORT = args.port
BAUD_RATE = int(args.baud)
THRESH = float(args.thresh)
IOU_THRESH = float(args.iou_thresh)
# open serial port
try:
ser = serial.Serial(PORT, BAUD_RATE)
except serial.serialutil.SerialException:
print("[Error] Specified port device not found\nAbort program\n")
exit(-1)
has_defect = 0
is_paused = 0
frame_count = 0
status_str = ''
frame_count_str = ''
detect_result_str = ''
img_RGB = np.array([0])
def pause_handler():
global is_paused
global status_str
global img_RGB
is_paused = not is_paused
if is_paused:
status_str = "Image capture paused!"
pause_btn['text'] = 'Resume'
else:
status_str = 'Image capture resumed'
pause_btn['text'] = 'Pause'
img_RGB = np.array([0])
str_label3['text'] = status_str
def save_handler():
global img_RGB
global frame_count
global status_str
current_time = strftime("%Y-%m-%d_%H:%M:%S", gmtime())
img_name = "detection_{}_{}.png".format(frame_count, current_time)
cv2.imwrite(img_name, img_RGB)
status_str = "Image Saved successfully"
str_label3['text'] = status_str
print("{}\nwritten!".format(img_name))
def exit_handler():
print("exit program...")
exit(0)
# gui parameters
pad = 5
align_mode = 'nswe'
window = tk.Tk()
window.title("Solder Joint Inspection")
window.geometry("600x430")
window.configure(bg='floral white')
window.resizable(0, 0) # not resizable
captured_frame = tk.Label(
window, width=IMG_SIZE[0] * 2, height=IMG_SIZE[1] * 2, bg='white')
message_frame = tk.Frame(
window, width=IMG_SIZE[0], height=IMG_SIZE[1], bg='white')
operate_frame = tk.Frame(
window, width=IMG_SIZE[0], height=IMG_SIZE[1], bg='floral white')
captured_frame.grid(column=0, row=0, padx=pad, pady=pad,
rowspan=2, sticky=align_mode)
message_frame.grid(column=1, row=0, padx=pad, pady=pad, sticky=align_mode)
operate_frame.grid(column=1, row=1, padx=pad, pady=pad, sticky=align_mode)
ser.reset_input_buffer() # clear input buffer
# message frame setup
str_label1 = tk.Label(
message_frame, text=frame_count_str, width=23, height=6, bg='white')
str_label2 = tk.Label(
message_frame, text=detect_result_str, width=23, height=6, bg='white')
str_label2.config(font=("Courier", 10, 'bold'))
str_label3 = tk.Label(
message_frame, text=status_str, width=23, height=6, bg='white')
str_label1.grid(column=0, row=0)
str_label2.grid(column=0, row=1)
str_label3.grid(column=0, row=2)
# operate frame setup
pause_btn = tk.Button(
operate_frame, text="Resume" if is_paused else "Pause", width=20)
save_btn = tk.Button(operate_frame, text="Save Image", width=20)
exit_btn = tk.Button(operate_frame, text="Exit")
pause_btn.grid(column=0, row=0)
save_btn.grid(column=0, row=1)
exit_btn.grid(column=0, row=2)
pause_btn['command'] = pause_handler
save_btn['command'] = save_handler
exit_btn['command'] = exit_handler
def capture_stream():
img_data = []
predictions = []
start_signal_count = 0
global has_defect
global frame_count
global img_RGB
global is_paused
global status_str
# transfer image (traferred in byte type)
if not is_paused:
while ser.inWaiting and start_signal_count != 10:
data = ser.read()
if data == b'7':
start_signal_count += 1
else:
start_signal_count = 0
start_signal_count = 0
while ser.inWaiting:
data = ser.read(IMG_SIZE[0])
img_data.append(list(data))
if len(img_data) == IMG_SIZE[1]:
frame_count += 1
data_array = np.array(img_data, dtype=np.uint8)
# image upscaling
data_array = np.repeat(data_array, repeats=2, axis=0)
data_array = np.repeat(data_array, repeats=2, axis=1)
print("image transfer complete!")
# transfer predictions (transferred in int type)
byte_queue = []
while ser.inWaiting and start_signal_count != 10:
data = ser.read()
if data == b'8':
start_signal_count += 1
else:
start_signal_count = 0
start_signal_count = 0
while ser.inWaiting:
current_line = ser.readline().strip()
value = current_line.decode('ascii')
byte_queue.append(value)
if (len(byte_queue) == PREDICTION_LEN):
break
predictions = np.array(byte_queue, dtype=np.int8)
img_RGB, has_defect = get_bounding_boxes(data_array,
predictions,
frame_count,
THRESH,
IOU_THRESH)
break
# message frame setup
frame_count_str = "Frame no.{}".format(frame_count)
if has_defect:
detect_result_str = "Defect joint detected!"
str_label2['fg'] = 'IndianRed2'
else:
detect_result_str = "Board QC test passed!"
str_label2['fg'] = 'SeaGreen2'
str_label1['text'] = frame_count_str
str_label2['text'] = detect_result_str
# image showing setup
img_pil = Image.fromarray(img_RGB)
img_tk = ImageTk.PhotoImage(image=img_pil)
captured_frame.configure(image=img_tk)
captured_frame.image = img_tk
window.after(10 if not is_paused else 1000, capture_stream)
capture_stream()
window.mainloop()
cv2.destroyAllWindows()