-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
428 lines (343 loc) · 16.7 KB
/
main.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#====================================== IMPORTS =====================================#
from tkinter import *
import tkinter as tk, threading
from tkinter import messagebox
from tkinter import simpledialog
from tkinter import ttk
from tkinter.messagebox import showinfo
from PIL import Image,ImageTk
import database
import cv2
import os
import pickle
import time
import face_recognition
import database as db
import sqlite3
import time
import re
#====================================== DEFINES =====================================#
KNOWN_FACES_DIR = "cctv_rostos" #pasta com os ID's
TOLERANCE = 0.6 #valor maior = mais preciso
FRAME_THICKNESS = 3 #linha verde
FONT_THICKNESS = 2 #font
MODEL = "hog" #cnn
window=Tk()
#window.attributes('-fullscreen', True)
window.title('CCTV Owner')
window.geometry("1920x1080")
window.iconbitmap(default="logo.ico")
main = Frame(window)
style = ttk.Style()
style.theme_use('xpnative')
f1 = ("Arial", 20)
f2 = ("Levi Windows", 10)
f3 = ("Levi Windows", 10)
#====================================== FUNÇÕES =====================================#
def get_selected_row(event):
global selected_tuple
index = list1.focus()
index2 = list1.item(index)["values"]
selected_tuple = index2[0]
nome_text.set(index2[1])
apelido_text.set(index2[2])
idade_text.set(index2[3])
pontos_text.set(index2[4])
obs_text.set(index2[5])
id_match_text.set(index2[6])
def view_command():
list1.delete(0,END)
for row in database.view():
list1.insert(END,row)
def search_command():
list1.delete(0,END)
for row in database.search(nome_text.get(),apelido_text.get(),idade_text.get(),pontos_text.get(),obs_text.get(),id_match_text.get()):
list1.insert(END,row)
def edit_command():
try:
if selected_tuple:
if db.getState(selected_tuple) == 0:
if int(idade_text.get()) > 0:
isNumber = re.search("[0-9]", str(variavel_idade.get()))
if isNumber:
database.insert(nome_text.get(),apelido_text.get(),idade_text.get(),obs_text.get(),selected_tuple)
show()
messagebox.showinfo("CCTV Owner", "Informação editada com sucesso!")
else:
messagebox.showwarning('CCTV Owner','Apenas insira números.')
else:
messagebox.showwarning('CCTV Owner','Insira números maiores que 0.')
else:
messagebox.showwarning('CCTV Owner','Não é possivel editar este cidadão.')
else:
messagebox.showwarning('CCTV Owner','Selecione o cidadão que deseja editar.')
except NameError:
messagebox.showwarning('CCTV Owner','Selecione o cidadão que deseja editar.')
def delete_command():
try:
if selected_tuple:
res = messagebox.askquestion('CCTV Owner', 'Tem certeza que deseja remover o cidadão?')
if res == 'yes':
database.delete(selected_tuple)
messagebox.showinfo('CCTV Owner', 'Cidadão removido com sucesso.')
show()
elif res == 'no':
messagebox.showinfo('CCTV Owner', 'Tarefa cancelada.')
else:
messagebox.showwarning('CCTV Owner','Selecione o cidadão que deseja remover.')
except NameError:
messagebox.showerror('CCTV Owner','Contacte o fornecedor do sistema.')
def block_command():
try:
if selected_tuple:
password = "teste"
answer = simpledialog.askstring("CCTV Owner", "Insira a senha fornecida pelo sistema.")
if answer == password:
messagebox.showwarning('CCTV Owner','Cidadão Bloqueado com sucesso.')
db.setState(1, selected_tuple)
else:
messagebox.showwarning('CCTV Owner','Senha Errada.')
else:
messagebox.showwarning('CCTV Owner','Selecione o cidadão que deseja bloquear.')
except NameError:
messagebox.showwarning('CCTV Owner','Selecione o cidadão que deseja bloquear.')
def gosto():
try:
if selected_tuple:
points = pontos_text.get()
points = points + 1
pontos_text.set(points)
database.setPointsForID(points, selected_tuple)
messagebox.showwarning("CCTV Owner", "O cidadao {0} recebeu +1 ponto.".format(nome_text.get()))
show()
else:
messagebox.showwarning('CCTV Owner','Selecione o cidadão.')
except NameError:
messagebox.showwarning('CCTV Owner','Selecione o cidadão.')
def ngosto():
try:
if selected_tuple:
points = pontos_text.get()
points = points - 1
pontos_text.set(points)
database.setPointsForID(points, selected_tuple)
messagebox.showwarning("CCTV Owner", "O cidadao {0} recebeu -1 ponto.".format(nome_text.get()))
except NameError:
messagebox.showwarning('CCTV Owner','Selecione o cidadão.')
def show():
ws_ent.delete(0, END)
ws_ent.focus()
list1.selection()
conn = None
try:
conn = sqlite3.connect("cctv.db")
cursor = conn.cursor()
db = "select * from cctv"
cursor.execute(db)
fetchdata = list1.get_children()
for elements in fetchdata:
list1.delete(elements)
data = cursor.fetchall()
for d in data:
list1.insert("", END, values=d)
conn.commit()
except Exception as e:
messagebox.showerror('Erro.', e)
conn.rollback()
finally:
if conn is not None:
conn.close()
def searchdata():
list1.selection()
fetchdata = list1.get_children()
for f in fetchdata:
list1.delete(f)
conn = None
try:
conn = sqlite3.connect("cctv.db")
core = conn.cursor()
db = "select * from cctv where nome = '%s' "
name = ws_ent.get()
if (len(name) < 2) or (not name.isalpha()):
messagebox.showerror('CCTV Owner','Erro.')
else:
core.execute(db %(name))
data = core.fetchall()
for d in data:
list1.insert("", END, values=d)
except Exception as e:
showerror("issue", e)
finally:
if conn is not None:
conn.close()
def reset():
show()
def naoFechar():
messagebox.showwarning('CCTV Owner','Não estrague o projeto dos outros.')
# MOUSE EVENT VIDEO
#def mouseevent(event, x, y, flags, param):
#if event == cv2.EVENT_LBUTTONDOWN:
#print("Teste")
#====================================== DADOS - INFO - CIDADAO =====================================#
inf = LabelFrame(window,relief=SOLID)
inf.place(relx=0.7,rely=0.5, anchor="nw", relwidth=0.3,relheight=0.5)
nome_text=StringVar()
Label(inf, text="Nome").place(relx=0.35,rely=0.1, anchor="nw")
entry1 = Entry(window, textvariable=nome_text)
entry1.place(relx=0.85,rely=0.55, anchor="nw")
apelido_text=StringVar()
Label(inf, text="Apelido").place(relx=0.35,rely=0.2, anchor="nw")
entry2 = Entry(window, textvariable=apelido_text)
entry2.place(relx=0.85,rely=0.6, anchor="nw")
idade_text=IntVar()
Label(inf, text="Idade").place(relx=0.35,rely=0.3, anchor="nw")
entry3 = Entry(window, textvariable=idade_text)
entry3.place(relx=0.85,rely=0.65, anchor="nw")
obs_text=StringVar()
Label(inf, text="Observações", font=f2).place(relx=0.35,rely=0.4, anchor="nw")
entry4 = Entry(window, textvariable=obs_text)
entry4.place(relx=0.85,rely=0.7, anchor="nw")
pontos_text=IntVar()
id_match_text=IntVar()
Button(inf, text="Adicionar Info", command=edit_command).place(relx=0.5,rely=0.55, anchor="center")
Button(inf, text="+1 Ponto", command=gosto).place(relx=0.3,rely=0.7, anchor="center")
Button(inf, text="-1 Ponto", command=ngosto).place(relx=0.7,rely=0.7, anchor="center")
#====================================== TITLE =====================================#
Label(main, text="CCTV Control", font=f1, relief=SOLID).place(relx=0.25, rely=0, anchor="nw", relwidth=0.45, relheight=0.1)
#====================================== DATABASE - LISTBOX =====================================#
inf = LabelFrame(window,relief=SOLID)
inf.place(relx=0,rely=0.7, anchor="nw", relwidth=0.25,relheight=0.3)
scrollbarx = Scrollbar(window, orient=HORIZONTAL)
scrollbary = Scrollbar(window, orient=VERTICAL)
list1 = ttk.Treeview(window, columns=("id_match", "name", "apelido", "idade", "pontos"), show='headings', height=22)
list1.place(relx=0, rely=0, anchor="nw", relwidth=0.25, relheight=0.7)
list1.heading('id_match', text="ID", anchor=CENTER)
list1.column("id_match", stretch=NO, width = 30)
list1.heading('name', text="Nome", anchor=CENTER)
list1.column("name", stretch=NO, width = 112)
list1.heading('apelido', text="Apelido", anchor=CENTER)
list1.column("apelido", stretch=NO, width = 112)
list1.heading('idade', text="Idade", anchor=CENTER)
list1.column("idade", stretch=NO, width = 112)
list1.heading('pontos', text="Pontos", anchor=CENTER)
list1.column("pontos", stretch=NO, width = 112)
style = ttk.Style()
style.theme_use("default")
style.map("Treeview")
list1.bind('<ButtonRelease-1>', get_selected_row)
#Scrollbar - Desnecessário - Futuro
#scrollbary.config(command=treeview.yview)
#scrollbary.place(x = 480, y = 7)
#scrollbarx.config(command=treeview.xview)
#scrollbarx.place(x = 920, y = 960)
#====================================== CONFIG DATABASE =====================================#
Button(inf, text="Atualizar", command=reset).place(relx=0.6,rely=0.25, anchor="center")
Button(inf, text="Bloquear Cidadão",command=block_command).place(relx=0.2,rely=0.25, anchor="center")
ws_lbl = ttk.Label(inf, text = "Nome", font=('Levi Windows', 12, 'normal'))
ws_lbl.place(relx=0.1,rely=0.45)
ws_ent = Entry(inf, width = 20, font=('Levi Windows', 15, 'bold'))
ws_ent.place(relx=0.2,rely=0.45)
ws_btn1 = ttk.Button(inf, text = 'Procurar', command = searchdata)
ws_btn1.place(relx=0.8,rely=0.49, anchor="center")
ws_btn2 = ttk.Button(inf, text = 'Reset', command = reset)
ws_btn2.place(relx=0.8,rely=0.6, anchor="center")
show()
# CORRIGIR - BLOQUEAR MOVER TABELA - FUTURO
#def handle_click(event):
# if treeview.identify_region(event.x, event.y) == "separator":
# return "break"
#...
#treeview.bind('<Button-1>', handle_click)
#====================================== DADOS - INFO - CIDADAO =====================================#
user = LabelFrame(window)
user.place(relx=0.7,rely=0, anchor="nw", relwidth=0.3,relheight=0.5)
Label(user, text="Info Cidadão", font=f1).place(relx=0.55, rely=0.05, anchor="center")
Label(user, text="Nome", textvariable=nome_text, font=f1).place(relx=0.55, rely=0.4, anchor="center")
Label(user, text="Apelido: ",textvariable=apelido_text, font=f1).place(relx=0.55, rely=0.5, anchor="center")
Label(user, text="Idade:",textvariable=idade_text, font=f1).place(relx=0.55, rely=0.6, anchor="center")
Label(user, text="Pontos:",textvariable=pontos_text, font=f1).place(relx=0.55, rely=0.7, anchor="center")
Label(user, text="Observações:",textvariable=obs_text, font=f1).place(relx=0.55, rely=0.8, anchor="center")
#====================================== CONFIG VIDEO =====================================#
#Button(main,text="Em Construção",font=f1, relief=SOLID).place(relx=0.25, rely=0.7, anchor="nw", relwidth=0.45, relheight=0.3)
#====================================== DEFINES VIDEO =====================================#
def main_f():
main.pack(fill="both", expand=1)
vid = LabelFrame(window,relief=SOLID)
vid.place(relx=0.25,rely=0.1, anchor="nw", relwidth=0.45,relheight=0.6)
window.update()
width = vid.winfo_width()
height = vid.winfo_height()
#================================ VIDEO / FACE DETECTION ============================#
know_faces = []
know_names = []
global next_id
for name in os.listdir(KNOWN_FACES_DIR):
for filename in os.listdir(f"{KNOWN_FACES_DIR}\\{name}"):
encoding = pickle.load(open (f"{KNOWN_FACES_DIR}/{name}/{filename}", "rb"))
know_faces.append(encoding)
know_names.append(int(name))
if len(know_names) > 0:
next_id = max(know_names) + 1
else:
next_id = 1
video = cv2.VideoCapture(0)
def display_video(label):
while(video.isOpened()):
frame, image = video.read()
if frame == True:
locations = face_recognition.face_locations(image, model=MODEL)
encodings = face_recognition.face_encodings(image, locations)
for face_encoding, face_location in zip(encodings, locations):
results = face_recognition.compare_faces(know_faces, face_encoding, TOLERANCE)
match = None
if True in results:
match = know_names[results.index(True)]
print(f"Pessoa ID: {match}")
if db.matchExist(match) != 1:
db.insertMatch(match)
else: # new id
next_id = len(know_faces) +1
match = str(next_id)
next_id += 1
if db.matchExist(match) != 1:
db.insertMatch(match)
know_names.append(match)
know_faces.append(face_encoding)
os.mkdir(f"{KNOWN_FACES_DIR}\\{match}")
pickle.dump(face_encoding, open(f"{KNOWN_FACES_DIR}\\{match}\\{match}-{int(time.time())}.pkl", "wb"))
#====================================== DESIGN VIDEO =====================================#
top_left = (face_location[3], face_location[0])
bottom_right = (face_location[1], face_location[2])
color = [0, 255, 0]
nomecidadao = db.getNameFromID(match)
pontoscidadao = db.getPointsFromID(match)
idadecidadao = db.getAgeFromID(match)
obscidadao = db.getObsFromID(match)
cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS)
#CORRIGIR - ADJUST TO MOVIMENT
#res = cv2.addWeighted(sub_img, 0.5, white_rect, 0.5, 1.0)
cv2.rectangle(image, (face_location[3]+350, face_location[2]-200), (face_location[1]+10, face_location[2]-170), (82,82,82), cv2.FILLED)
cv2.rectangle(image, (face_location[3]+350, face_location[2]-160), (face_location[1]+10, face_location[2]-130), (82,82,82), cv2.FILLED)
cv2.rectangle(image, (face_location[3]+350, face_location[2]-120), (face_location[1]+10, face_location[2]-90), (82,82,82), cv2.FILLED)
cv2.rectangle(image, (face_location[3]+350, face_location[2]-80), (face_location[1]+10, face_location[2]-50), (82,82,82), cv2.FILLED)
cv2.rectangle(image, (face_location[3]+350, face_location[2]-40), (face_location[1]+10, face_location[2]-10), (82,82,82), cv2.FILLED)
cv2.putText(image, "Cidadao: {}".format(match), (face_location[1]+20, face_location[2]-180), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)
cv2.putText(image, "Nome: {}".format(nomecidadao), (face_location[1]+20, face_location[2]-140), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)
cv2.putText(image, "Idade: {}".format(idadecidadao), (face_location[1]+20, face_location[2]-100), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)
cv2.putText(image, "Pontos: {}".format(pontoscidadao), (face_location[1]+20, face_location[2]-60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)
cv2.putText(image, "{}".format(obscidadao), (face_location[1]+20, face_location[2]-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)
#====================================== VIDEO =====================================#
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img = Image.fromarray(image)
img2 = img.resize(((width,height)))
image_frame = ImageTk.PhotoImage(image = img2)
label.config(image=image_frame)
label.image = image_frame
my_vid = Label(vid)
my_vid.pack()
thread = threading.Thread(target=display_video, args=(my_vid,))
thread.start()
#====================================== CLOSE WINDOW =====================================#
window.protocol("WM_DELETE_WINDOW", naoFechar)
main_f()
window.mainloop()