-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
234 lines (182 loc) · 8.96 KB
/
gui.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
import tkinter as tk
from tkinter import messagebox, Toplevel, ttk
from PIL import Image, ImageTk, ImageDraw
import os
import logging
from database import connect_to_db
from search import search_faces_by_id, get_all_face_ids
import face_recognition
import threading
import queue
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("app.log"),
logging.StreamHandler()
]
)
class FaceSearchApp:
def __init__(self, master):
self.master = master
self.master.title("Face Search Application")
self.master.geometry("900x700")
self.master.configure(bg="#f0f0f0")
self.collection = connect_to_db()
self.face_frames = []
self.face_ids = []
self.selected_face_index = None
self.create_widgets()
self.load_faces()
def create_widgets(self):
# Title
title_label = tk.Label(self.master, text="Face Search Application", font=("Helvetica", 24, "bold"), bg="#f0f0f0")
title_label.pack(pady=20)
# Main frame
main_frame = tk.Frame(self.master, bg="#f0f0f0")
main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
# Faces frame with scrollbar
faces_frame = tk.Frame(main_frame, bg="#ffffff", bd=2, relief=tk.GROOVE)
faces_frame.pack(fill=tk.BOTH, expand=True, pady=10)
self.canvas = tk.Canvas(faces_frame, bg="#ffffff")
scrollbar = ttk.Scrollbar(faces_frame, orient="horizontal", command=self.canvas.xview)
self.images_frame = tk.Frame(self.canvas, bg="#ffffff")
self.canvas.configure(xscrollcommand=scrollbar.set)
scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.canvas.create_window((0, 0), window=self.images_frame, anchor="nw")
self.images_frame.bind("<Configure>", self.on_frame_configure)
# Search button
self.search_button = ttk.Button(main_frame, text="Search Selected Face", command=self.search_selected_face, style="TButton")
self.search_button.pack(pady=20)
# Configure ttk styles
style = ttk.Style()
style.configure("TButton", padding=10, font=("Helvetica", 12))
def on_frame_configure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def load_faces(self):
unique_faces_folder = './unique_faces'
for filename in os.listdir(unique_faces_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
self.display_face(filename)
def display_face(self, filename):
face_id = os.path.splitext(filename)[0]
frame = tk.Frame(self.images_frame, bg="#ffffff", bd=2, relief=tk.RAISED)
frame.pack(side=tk.LEFT, padx=10, pady=10)
image_path = os.path.join('./unique_faces', filename)
try:
image = Image.open(image_path)
image.thumbnail((120, 120))
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame, image=photo, bg="#ffffff")
label.image = photo
label.pack(padx=5, pady=5)
id_label = tk.Label(frame, text=f"ID: {face_id}", bg="#ffffff", font=("Helvetica", 10))
id_label.pack()
self.face_frames.append((frame, label))
self.face_ids.append(int(face_id))
frame.bind("<Button-1>", lambda event, idx=len(self.face_ids)-1: self.select_face(idx))
label.bind("<Button-1>", lambda event, idx=len(self.face_ids)-1: self.select_face(idx))
except Exception as e:
logging.error(f"Error loading image {image_path}: {e}")
def select_face(self, index):
if self.selected_face_index is not None:
self.face_frames[self.selected_face_index][0].configure(bg="#ffffff")
self.selected_face_index = index
self.face_frames[index][0].configure(bg="#e0e0ff")
face_id = self.face_ids[index]
logging.info(f"Selected face ID: {face_id}")
def search_selected_face(self):
if self.selected_face_index is None:
messagebox.showwarning("No Selection", "Please select a face first.")
return
selected_face_id = self.face_ids[self.selected_face_index]
result = search_faces_by_id(self.collection, selected_face_id)
if result:
self.show_search_results(selected_face_id, result)
else:
messagebox.showinfo("No Images Found", f"No images found for face ID {selected_face_id}.")
def show_search_results(self, face_id, result):
logging.info(f"Displaying search results for Face ID: {face_id}")
result_window = Toplevel(self.master)
result_window.title(f"Search Results for Face ID: {face_id}")
result_window.geometry("900x700")
result_window.configure(bg="#f0f0f0")
title_label = tk.Label(result_window, text=f"Search Results for Face ID: {face_id}", font=("Helvetica", 18, "bold"), bg="#f0f0f0")
title_label.pack(pady=20)
canvas = tk.Canvas(result_window, bg="#ffffff")
scrollbar = ttk.Scrollbar(result_window, orient="vertical", command=canvas.yview)
scrollable_frame = tk.Frame(canvas, bg="#ffffff")
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True, padx=20, pady=10)
scrollbar.pack(side="right", fill="y")
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(result_window, variable=progress_var, maximum=100, style="TProgressbar")
progress_bar.pack(pady=10, padx=20, fill=tk.X)
self.image_queue = queue.Queue()
occurrences = result.get('occurrences', [])
logging.info(f"Number of occurrences found: {len(occurrences)}")
def process_images():
for i, occurrence in enumerate(occurrences):
filename = occurrence['filename']
bounding_box = occurrence['bounding_box']
image_path = os.path.join('images', filename)
if os.path.exists(image_path):
logging.info(f"Processing image: {image_path}")
processed_image = self.process_image_with_face(image_path, bounding_box)
self.image_queue.put((processed_image, filename))
logging.info(f"Processed image: {filename} ({i + 1}/{len(occurrences)})")
else:
logging.warning(f"Image file not found: {image_path}")
progress_var.set((i + 1) / len(occurrences) * 100)
self.image_queue.put(None) # Signal that processing is complete
logging.info("Image processing complete.")
def update_ui():
try:
item = self.image_queue.get_nowait()
if item is None:
progress_bar.pack_forget() # Hide progress bar when done
logging.info("All images processed, progress bar hidden.")
return
processed_image, filename = item
self.display_processed_image(scrollable_frame, processed_image, filename)
result_window.after(10, update_ui)
except queue.Empty:
result_window.after(100, update_ui)
threading.Thread(target=process_images, daemon=True).start()
result_window.after(100, update_ui)
def process_image_with_face(self, image_path, bounding_box):
try:
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
# Face_recognition provides (top, right, bottom, left), adjust it to (x0, y0, x1, y1)
top, right, bottom, left = bounding_box
# Draw bounding box
draw.rectangle([left, top, right, bottom], outline="yellow", width=30)
# Optionally, you can resize the image if needed
image.thumbnail((400, 400))
return image
except Exception as e:
logging.error(f"Error processing image {image_path}: {e}")
return None
def display_processed_image(self, parent_frame, pil_image, filename):
if pil_image:
frame = tk.Frame(parent_frame, bg="#ffffff", bd=2, relief=tk.GROOVE)
frame.pack(pady=10, padx=10, fill=tk.X)
photo = ImageTk.PhotoImage(pil_image)
label = tk.Label(frame, image=photo, bg="#ffffff")
label.image = photo
label.pack(pady=5)
filename_label = tk.Label(frame, text=os.path.basename(filename), bg="#ffffff", font=("Helvetica", 10))
filename_label.pack()
if __name__ == "__main__":
root = tk.Tk()
app = FaceSearchApp(root)
root.mainloop()