-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI-entranceStep.py
412 lines (340 loc) · 14 KB
/
GUI-entranceStep.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
### IMPORT PACKAGE ###
import tkinter as tk
import cv2, sys, timeit, gc, time
from gstStreamerbykemal import gstreamer_pipeline
from PIL import Image, ImageTk
from tkinter import ttk
from pymata4 import pymata4
from datetime import datetime
from deteksiplat import deteksiplat
from pengenalanplat import pengenalanplat
from queryData import insertDatabase, insertPlat, readData, mendaftarParkir
import numpy as np
from imgpreprocesingStep import correctionF,grayF,blurF,denoiseF,gammF,gammF,histF,threshF
from resizeImg import image_resize
from convertImage import piltoCv, cvtoPil
from fpsDisplay import display_frames_per_second
### FUNGSI-FUNGSI ###
def servo():
# untuk menghemat proses pemrosesan package arduino
# tidak dapat dipisah karena kita akan menggunakan
# dua program secara bergantian, yaitu program untuk jarak
#dan program untuk gerbang
#messageBar("Palang terbuka, silahkan masuk")
board.servo_write(servoPin, 0) # Atur servo 0 derajat (terbuka)
time.sleep(1)
while True:
jarakBenda = board.sonar_read(triggerPin) #ambil jarak kendaraan
messageBar(1,"Posisi Anda saat ini: {} cm".format(jarakBenda))
if jarakBenda[0] > 30: #jika jarak benda lebih dari 200cm maka kendaraan tersebut telah lewat
time.sleep(1)
board.servo_write(servoPin, 90)
break
#messageBar(1,"Silakan masuk, gerbang akan ditutup setelah jarak 200cm, jarak Anda saat ini: {} cm".format(jarakBenda))
def jarak():
jarakBenda = board.sonar_read(triggerPin)
return jarakBenda[0]
def messageBar(label,message=""):
"""
label = 1 => label 1
label = 2 => label 2
"""
if label==1:
processBar.set(message)
if label==2:
platResult.set(message)
root.update_idletasks() #untuk update window
def imageBar(frame,img,widthsize):
img = image_resize(img, width = widthsize)
img = cvtoPil(img)
if frame == 1:
koreksiImgFrame.paste(img)
if frame == 2:
grayImgFrame.paste(img)
if frame == 3:
blurImgFrame.paste(img)
if frame == 4:
denoiseImgFrame.paste(img)
if frame == 5:
histImgFrame.paste(img)
if frame == 6:
gammaImgFrame.paste(img)
if frame == 7:
tresholdImgFrame.paste(img)
if frame == 8:
faceEntryImgFrame.paste(img)
if frame == 9:
faceExitImgFrame.paste(img)
if frame == 0:
photo.paste(img)
root.update_idletasks() #untuk update window
def timecounting(waktuAwal):
waktuAkhir = timeit.default_timer()
timetotal = waktuAkhir - waktuAwal
return timetotal
def update_frame():
START_TIME = time.time()
global image
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #format waktu
TIMETXT.set(now) # mengatur waktu pada GUI
# _, frame = cap.read() # Membaca input kamera gstreamer
ret, frame = videowebCam.read()
if frame is not None: # jika kamera terbaca
jarakBenda = jarak() #ditangkap keluaran berupa jarak dalam satuan cm.
if jarakBenda < 5:
tic = timeit.default_timer() # Waktu awal memulai suatu program
messageBar(1,"Gambar sedang di proses")
MODE=0 #MODE 0 adalah mode uji saat presentasi akan mereplace gambar kamera dengan gambar yang ada
if MODE==0:
pathImage = "/home/kemal/Downloads/AutomaticParkByKemal/images/gambar tes/uji gambar/driver 1/driver1_30deg.jpg"
frame = cv2.imread(pathImage)
imageBar(0,frame,1280)
#DETEKSIPLAT#
tic1 = timeit.default_timer() # Waktu untuk deteksi plat
imgDeteksiPlat = deteksiplat(frame,4)
#jika array isinya nol maka:
if imgDeteksiPlat == []:
messageBar(2,"Plat tidak terdeteksi. Mohon ulangi kembali")
update_frame()
message = "Deteksi plat: {} s".format(timecounting(tic1))
messageBar(1,message)
print(message)
#IMG PRE-RPOCESSIMG#
tic2 = timeit.default_timer() # waktu untuk pemrosesan
hasilPreprocessing=correctionF(imgDeteksiPlat)
imageBar(1,hasilPreprocessing,270)
hasilPreprocessing=grayF(hasilPreprocessing)
imageBar(2,hasilPreprocessing,270)
hasilPreprocessing=blurF(hasilPreprocessing)
imageBar(3,hasilPreprocessing,270)
hasilPreprocessing=denoiseF(hasilPreprocessing)
imageBar(4,hasilPreprocessing,270)
hasilPreprocessing=histF(hasilPreprocessing)
imageBar(5,hasilPreprocessing,270)
hasilPreprocessing=gammF(hasilPreprocessing)
imageBar(6,hasilPreprocessing,270)
hasilPreprocessing=threshF(hasilPreprocessing)
imageBar(7,hasilPreprocessing,480)
message="Pre-processing: {} s".format(timecounting(tic2))
messageBar(1,message)
print(message)
#RECOGNITION#
tic3 = timeit.default_timer() # waktu untuk pengenalan
karakterplat = pengenalanplat(hasilPreprocessing,scaler=1)
if len(karakterplat)==0:
messageBar(1,"Plat tidak terbaca. Mohon ulangi kembali")
update_frame()
message="Karakter plat: {} s".format(timecounting(tic3))
messageBar(1,message)
print(message)
messageBar(2,karakterplat)
print(karakterplat)
#SIMPAN KARAKTER PLAT KE DATABASE#
insertPlat(karakterplat)
#BACA DAN SIMPAN DI DATABASE DAN LOCAL (UNTUK GAMBARNYA) APABILA KENDARAAN BELOM ENTRY KE DALAM PARKIRAN#
tic4 = timeit.default_timer() # waktu untuk pengenalan wajah
hasilMendaftarParkir = mendaftarParkir(karakterplat,frame,imgDeteksiPlat,scaler=2)
if not hasilMendaftarParkir:
messageBar(2,"Wajah tidak terbaca. Mohon ulangi kembali")
update_frame()
status,croppedFace,img_name_rawImg,img_name_faceImg,img_name_platImg = hasilMendaftarParkir
message="Encoding: {} s & Total: {} s".format(timecounting(tic4),timecounting(tic))
messageBar(1,message)
print(message)
#TAMPILKAN GAMBAR JIKA BELOM TERINPUT
if status == 0:
imageBar(8,croppedFace,240)
else:
messageBar(2,"Kendaraan sudah pernah terinput")
#Buka gerbang
servo()
else:
messageBar(1,"Posisi Anda saat ini: {} cm".format(jarakBenda))
frame = cv2.flip(frame, 1)
frame = display_frames_per_second(frame, START_TIME)
image = cvtoPil(frame)
else:
print("mohon restart kamera menggunakan perintah: 'sudo service nvargus-daemon restart'")
photo.paste(image)
root.after(round(10), update_frame) #update displayed image after: round(1000/FPS) [in milliseconds]
### ARDUINO SETUP ###
triggerPin = 11
echoPin = 12
servoPin = 10
board= pymata4.Pymata4()
board.set_pin_mode_sonar(triggerPin, echoPin)
board.set_pin_mode_servo(servoPin)
### GUI SETUP ###
APP_WIDTH = 1920 #minimal width of the GUI
APP_HEIGHT = 1080 #minimal height of the gui
# cap = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
videowebCam = cv2.VideoCapture(0)
WIDTH = 1280#int(cap.get(3)) #webcam's picture width
HEIGHT = 720#int(cap.get(4))#wabcam's picture height
root = tk.Tk() # start of GUI
root.title("Automatic Gate Parking By Kemal")
root.minsize(APP_WIDTH,APP_HEIGHT)
root["bg"]="#c6c6c6"
### GUI elements ###
#Jarak setiap widget 0.015
# image default
defaultImg = cv2.imread("/home/kemal/Downloads/AutomaticParkByKemal/images/app/licenseDefault.png")
defaultImg = image_resize(defaultImg, width = 270)
defaultImg = cvtoPil(defaultImg)
defaultImgwide = cv2.imread("/home/kemal/Downloads/AutomaticParkByKemal/images/app/licenseDefaultwide.png")
defaultImgwide = image_resize(defaultImgwide, width = 480)
defaultImgwide = cvtoPil(defaultImgwide)
defaultProfile = cv2.imread("/home/kemal/Downloads/AutomaticParkByKemal/images/app/profile.png")
defaultProfile = image_resize(defaultProfile, width = 240)
defaultProfile = cvtoPil(defaultProfile)
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT,bg="white")
canvas.place(relx=0.01,rely=0.305)
#KOREKSI FRAME
MESSAGE1 = tk.StringVar()
message = "Koreksi warna gambar"
MESSAGE1.set(message)
message_label1=tk.Label(root,textvariable=MESSAGE1, wraplength = "10c", bg="black", fg="white")
message_label1.place(relx=0.150,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label1.config(font=(None, 11))
koreksiImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
correctionImg = tk.Label(root, image = koreksiImgFrame)
correctionImg.image = koreksiImgFrame
correctionImg.place(relx=0.150,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#GRAY FRAME
MESSAGE2 = tk.StringVar()
messageGray = "Grays"
MESSAGE2.set(messageGray)
message_label2=tk.Label(root,textvariable=MESSAGE2, wraplength = "10c", bg="black", fg="white")
message_label2.place(relx=0.298,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label2.config(font=(None, 11))
grayImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
grayImg = tk.Label(root, image = grayImgFrame)
grayImg.image = grayImgFrame
grayImg.place(relx=0.298,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#BLUR FRAME
MESSAGE3 = tk.StringVar()
messageBlur = "Blur"
MESSAGE3.set(messageBlur)
message_label3=tk.Label(root,textvariable=MESSAGE3, wraplength = "10c", bg="black", fg="white")
message_label3.place(relx=0.445,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label3.config(font=(None, 11))
blurImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
blurImg = tk.Label(root, image = blurImgFrame)
blurImg.image = blurImgFrame
blurImg.place(relx=0.445,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#DENOISE FRAME
MESSAGE4 = tk.StringVar()
messageDenoise = "Denoise"
MESSAGE4.set(messageDenoise)
message_label4=tk.Label(root,textvariable=MESSAGE4, wraplength = "10c", bg="black", fg="white")
message_label4.place(relx=0.593,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label4.config(font=(None, 11))
denoiseImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
denoiseImg = tk.Label(root, image = denoiseImgFrame)
denoiseImg.image = denoiseImgFrame
denoiseImg.place(relx=0.593,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#HIST FRAME
MESSAGE5 = tk.StringVar()
messageHist = "Histogram"
MESSAGE5.set(messageHist)
message_label5=tk.Label(root,textvariable=MESSAGE5, wraplength = "10c", bg="black", fg="white")
message_label5.place(relx=0.741,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label5.config(font=(None, 11))
histImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
histImg = tk.Label(root, image = histImgFrame)
histImg.image = histImgFrame
histImg.place(relx=0.741,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#GAMMA FRAME
MESSAGE6 = tk.StringVar()
messageGamma = "Gamma"
MESSAGE6.set(messageGamma)
message_label6=tk.Label(root,textvariable=MESSAGE6, wraplength = "10c", bg="black", fg="white")
message_label6.place(relx=0.889,rely=0.02,relwidth=0.140,relheight=0.03,anchor="ne")
message_label6.config(font=(None, 11))
gammaImgFrame = ImageTk.PhotoImage(defaultImg)
# Create a Label Widget to display the text or Image
gammaImg = tk.Label(root, image = gammaImgFrame)
gammaImg.image = gammaImgFrame
gammaImg.place(relx=0.889,rely=0.052,relwidth=0.140,relheight=0.25,anchor="ne")
#treshold
MESSAGE7 = tk.StringVar()
messageThresh = "THRESHOLD"
MESSAGE7.set(messageThresh)
message_label7=tk.Label(root,textvariable=MESSAGE7, wraplength = "10c", bg="black", fg="white")
message_label7.place(relx=0.93,rely=0.305,relwidth=0.25,relheight=0.03,anchor="ne")
message_label7.config(font=(None, 11))
tresholdImgFrame = ImageTk.PhotoImage(defaultImgwide)
# Create a Label Widget to display the text or Image
tresholdImg = tk.Label(root, image = tresholdImgFrame,bg="black")
tresholdImg.image = tresholdImgFrame
tresholdImg.place(relx=0.93,rely=0.335,relwidth=0.25,relheight=0.25,anchor="ne")
#face entry
MESSAGE8 = tk.StringVar()
messageFaceEntry = "FACE ENTRY"
MESSAGE8.set(messageFaceEntry)
message_label8=tk.Label(root,textvariable=MESSAGE8, wraplength = "10c", bg="black", fg="white")
message_label8.place(relx=0.805,rely=0.6,relwidth=0.125,relheight=0.03,anchor="ne")
message_label8.config(font=(None, 11))
faceEntryImgFrame = ImageTk.PhotoImage(defaultProfile)
# Create a Label Widget to display the text or Image
imageFaceEntry = tk.Label(root, image = faceEntryImgFrame)
imageFaceEntry.image = faceEntryImgFrame
imageFaceEntry.place(relx=0.805,rely=0.63,relwidth=0.125,relheight=0.222,anchor="ne")
#face exit
MESSAGE9 = tk.StringVar()
messageFaceExit = "FACE EXIT"
MESSAGE9.set(messageFaceExit)
message_label9=tk.Label(root,textvariable=MESSAGE9, wraplength = "10c", bg="black", fg="white")
message_label9.place(relx=0.93,rely=0.6,relwidth=0.125,relheight=0.03,anchor="ne")
message_label9.config(font=(None, 11))
# Create an object of tkinter ImageTk
faceExitImgFrame = ImageTk.PhotoImage(defaultProfile)
# Create a Label Widget to display the text or Image
imageFaceExit = tk.Label(root, image = faceExitImgFrame)
imageFaceExit.image = faceExitImgFrame
imageFaceExit.place(relx=0.93,rely=0.63,relwidth=0.125,relheight=0.222,anchor="ne")
TIMETXT = tk.StringVar()
timetext = "22/04/2022 19.00"
TIMETXT.set(timetext)
time_label=tk.Label(root,textvariable=TIMETXT, wraplength = "10c", bg="black", fg="white")
time_label.place(relx=0.93,rely=0.867,relwidth=0.25,relheight=0.03,anchor="ne")
time_label.config(font=(None, 14))
processBar = tk.StringVar()
messageProccess = "SELAMAT DATANG DI KEMAL PARK"
processBar.set(messageProccess)
process_label=tk.Label(root,textvariable=processBar, wraplength = "10c", bg="black", fg="white")
process_label.place(relx=0.93,rely=0.91,relwidth=0.25,relheight=0.05,anchor="ne")
process_label.config(font=(None, 14))
platResult = tk.StringVar()
messagePlat = "SELAMAT DATANG DI KEMAL PARK"
platResult.set(messagePlat)
platResult_label=tk.Label(root,textvariable=platResult, wraplength = "10c", bg="black", fg="white")
platResult_label.place(relx=0.93,rely=0.957,relwidth=0.25,relheight=0.03,anchor="ne")
platResult_label.config(font=(None, 14))
#####################
### Initial frame ###
#####################
# _, frame = cap.read()
ret, frame = videowebCam.read()
if frame is not None:
image = cvtoPil(frame)
photo = ImageTk.PhotoImage(image=image)
canvas.create_image(WIDTH, HEIGHT, image=photo, anchor="se")
else:
print("kamera sedang error, mohon restart atau clean GSTREAMER")
######################
### Start the show ###
######################
if __name__ == '__main__':
update_frame()
#create the GUI.
root.mainloop()
#free memory
# cap.release()
videowebCam.release()
gc.collect()