-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeepface.py
More file actions
146 lines (117 loc) · 5.07 KB
/
Deepface.py
File metadata and controls
146 lines (117 loc) · 5.07 KB
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
#!/usr/bin/env python
# coding: utf-8
import subprocess
import sys
import pkg_resources
# Download the latest version of DeepFace silently
subprocess.call(['pip', 'install', '--upgrade', 'deepface'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Get DeepFace version
deepface_version = pkg_resources.get_distribution("deepface").version
import tkinter as tk
from tkinter import *
from tkinter import font
from tkinter import filedialog
from tkinter import ttk
from deepface import DeepFace
import os
import pandas as pd
db_path = ""
img2_path = ""
dfs = [] # Initialize dfs as an empty list
models = [
"VGG-Face",
"Facenet",
"Facenet512",
"OpenFace",
"DeepFace",
"DeepID",
"ArcFace",
"Dlib",
"SFace",
]
# Function to open a folder dialog and update db_path
def select_folder():
global db_path
db_path = filedialog.askdirectory()
db_path_entry.delete(0, tk.END)
db_path_entry.insert(0, db_path)
# Function to open a file dialog and update img2_path
def select_image():
global img2_path
img2_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png")])
img2_path_entry.delete(0, tk.END)
img2_path_entry.insert(0, img2_path)
# Function to open the selected second image
def open_second_image():
global img2_path
if img2_path:
try:
os.startfile(img2_path)
except Exception as e:
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, f"Error opening the image: {str(e)}")
else:
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, "Please select a second image.")
# Function to perform the DeepFace.find() when the user clicks the button 'find button'
def find_similar_faces():
global db_path, img2_path, dfs, result_text
if not db_path or not img2_path:
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, "Please select both a database folder and a second image.")
else:
# pd.set_option('display.max_colwidth', -1)
pd.options.display.max_rows = 4000
selected_model = model_selection.get() # Get the selected model from the dropdown
dfs = DeepFace.find(img2_path, db_path=db_path, model_name=selected_model, distance_metric='cosine', enforce_detection=False)
# Display the results in the text widget
result_text.delete(1.0, tk.END) # Clear previous results
# Extract the DataFrame from the sublist
df = dfs[0]
#"Facenet512": {"cosine": 0.30, "euclidean": 23.56, "euclidean_l2": 1.04},
# Insert the string representation of df into the result_text widget
result_text.insert(tk.END, df.to_string(float_format="{:.6f}".format, index=True))
# Create a main window
root = tk.Tk()
root.title("Simple DeepFace Gui by Ehtz")
# Label to display DeepFace version
deepface_version_label = tk.Label(root, text=f"DeepFace Version: {deepface_version}")
deepface_version_label.grid(row=0, column=3)
# Create a label and an entry widget for db_path
db_path_label = tk.Label(root, text="Database Images Folder:")
db_path_label.grid(row=1, column=0)
select_folder_button = tk.Button(root, text="Select Folder", command=select_folder)
select_folder_button.grid(row=1, column=1)
db_path_entry = tk.Entry(root, textvariable=db_path, width=50)
db_path_entry.grid(row=1, column=2)
# Create a label and an entry widget for img2_path
img2_path_label = tk.Label(root, text="Base Image:")
img2_path_label.grid(row=2, column=0, pady=25)
# Create a button to open the file selection dialog for img2_path
select_image_button = tk.Button(root, text="Select Image", command=select_image)
select_image_button.grid(row=2, column=1)
# Create a button to open the selected second image
open_second_image_button = tk.Button(root, text="Open Base Image", command=open_second_image)
open_second_image_button.grid(row=2, column=3)
img2_path_entry = tk.Entry(root, textvariable=img2_path, width=50)
img2_path_entry.grid(row=2, column=2)
# Create a dropdown menu for selecting the model
model_label = tk.Label(root, text="Select Model:")
model_label.grid(row=4, column=0)
model_selection = ttk.Combobox(root, values=models)
model_selection.set("Facenet512")
model_selection.grid(row=4, column=1)
# Create a button to trigger the find_similar_faces function
find_button = tk.Button(root, text="Find Similar Faces", pady=15, bg="red", fg='white', command=find_similar_faces)
find_button.grid(row=6, column=0, columnspan=5, padx=10, pady=10)
# Create a text wido display the results
#Tried increase hight to get more entries i don't think it is the tk limiting output entries but more of the models themselves
result_text = tk.Text(root, width=200, height=100, selectbackground="yellow", selectforeground='black', undo=True)
result_text.grid(row=7, column=0, columnspan=5, padx=10, pady=10)
# Create a scrollbar and attach it to the text widget
text_scroll = tk.Scrollbar(root, command=result_text.yview)
text_scroll.grid(row=7, column=5, sticky="ns")
result_text.config(yscrollcommand=text_scroll.set)
# Start the tkinter main loop
root.geometry("1650x500")
root.mainloop()