I am using Face Recognition. When I load a folder and choose an image of the person, after that the script will find all images associated with that person and delete them. But when I run the code it deletes all the pictures with faces.
CODE 1:
def load_file(self):
filename = askopenfilename()
self._file_field.delete(0, tk.END) # deletes the current value
self._file_field.insert(0, filename)
for widget in self._image_panel.winfo_children():
widget.destroy()
img = ImageTk.PhotoImage(Image.open(filename))
panel = tk.Label(self._image_panel, image=img)
panel.img = img
panel.pack(side="bottom", fill="both", expand="yes")
CODE 2:
def load_folder(self):
filename = askdirectory()
self._folder_field.delete(0, tk.END) # deletes the current value
self._folder_field.insert(0, filename)
CODE 3:
def start_process(self):
ref_path = self._file_field.get()
folder_path = self._folder_field.get()
self.find_and_delete(folder_path, ref_path)
CODE 4:
def run_process(self):
self._run_task = True
t = Thread(target=self.start_process)
t.start()
CODE 5:
def find_and_delete(self, directory, ref_image_path):
ref_face = self.get_ref_face(ref_image_path)
if ref_face is None:
messagebox.showinfo("Error", "Can't find a face. Reference Image quality is not enough")
return
files = os.listdir(directory)
i = 1
for filename in files:
try:
percentage = int((i / len(files)) * 100)
self._progress['value'] = percentage
self.update_idletasks()
i += 1
file_path = os.path.join(directory, filename)
if self.is_user_in_image(ref_face, file_path):
os.remove(file_path)
print(filename, 'deleted.')
except:
print('Error reading file', filename)
messagebox.showinfo("Information", "Process Finished")
CODE 6
def get_ref_face(self, ref_image_path):
try:
ref_image = face_recognition.load_image_file(ref_image_path)
ref_face = face_recognition.face_encodings(ref_image)[0]
return ref_face
except:
return None
CODE 7:
def is_user_in_image(self, ref_face, image_path):
unknown_picture = face_recognition.load_image_file(image_path)
unknown_faces = face_recognition.face_encodings(unknown_picture)
for unknown_face in unknown_faces:
results = face_recognition.compare_faces([ref_face], unknown_face)
if results[0]:
return True
return False