Skip to content

Commit

Permalink
Edit face recognition execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
computervisioneng committed Dec 17, 2022
1 parent efa42dd commit bf618ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
16 changes: 7 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os.path
import datetime
import subprocess
import pickle

import tkinter as tk
import cv2
from PIL import Image, ImageTk
import face_recognition

import util

Expand Down Expand Up @@ -52,12 +53,8 @@ def process_webcam(self):
self._label.after(20, self.process_webcam)

def login(self):
unknown_img_path = './.tmp.jpg'

cv2.imwrite(unknown_img_path, self.most_recent_capture_arr)

output = str(subprocess.check_output(['face_recognition', self.db_dir, unknown_img_path]))
name = output.split(',')[1][:-3]
name = util.recognize(self.most_recent_capture_arr, self.db_dir)

if name in ['unknown_person', 'no_persons_found']:
util.msg_box('Ups...', 'Unknown user. Please register new user or try again.')
Expand All @@ -67,8 +64,6 @@ def login(self):
f.write('{},{}\n'.format(name, datetime.datetime.now()))
f.close()

os.remove(unknown_img_path)

def register_new_user(self):
self.register_new_user_window = tk.Toplevel(self.main_window)
self.register_new_user_window.geometry("1200x520+370+120")
Expand Down Expand Up @@ -106,7 +101,10 @@ def start(self):
def accept_register_new_user(self):
name = self.entry_text_register_new_user.get(1.0, "end-1c")

cv2.imwrite(os.path.join(self.db_dir, '{}.jpg'.format(name)), self.register_new_user_capture)
embeddings = face_recognition.face_encodings(self.register_new_user_capture)[0]

file = open(os.path.join(self.db_dir, '{}.pickle'.format(name)), 'wb')
pickle.dump(embeddings, file)

util.msg_box('Success!', 'User was registered successfully !')

Expand Down
33 changes: 33 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
import pickle

import tkinter as tk
from tkinter import messagebox
import face_recognition


def get_button(window, text, color, command, fg='white'):
Expand Down Expand Up @@ -40,3 +44,32 @@ def get_entry_text(window):

def msg_box(title, description):
messagebox.showinfo(title, description)


def recognize(img, db_path):
# it is assumed there will be at most 1 match in the db

embeddings_unknown = face_recognition.face_encodings(img)
if len(embeddings_unknown) == 0:
return 'no_persons_found'
else:
embeddings_unknown = embeddings_unknown[0]

db_dir = sorted(os.listdir(db_path))

match = False
j = 0
while not match and j < len(db_dir):
path_ = os.path.join(db_path, db_dir[j])

file = open(path_, 'rb')
embeddings = pickle.load(file)

match = face_recognition.compare_faces([embeddings], embeddings_unknown)[0]
j += 1

if match:
return db_dir[j - 1][:-7]
else:
return 'unknown_person'

1 comment on commit bf618ae

@nayanaharsha06
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C:\Users\nnaya\AppData\Local\Programs\Python\Python311\python.exe C:\Users\nnaya\PycharmProjects\face-attendance-system1\main.py
Traceback (most recent call last):
File "C:\Users\nnaya\PycharmProjects\face-attendance-system1\main.py", line 99, in
app = App()
^^^^^
File "C:\Users\nnaya\PycharmProjects\face-attendance-system1\main.py", line 24, in init
self.add_webcam(self.webcam_label)
File "C:\Users\nnaya\PycharmProjects\face-attendance-system1\main.py", line 33, in add_webcam
self.process_webcam()
File "C:\Users\nnaya\PycharmProjects\face-attendance-system1\main.py", line 37, in process_webcam
img_ = cv2.cvtColor(self.most_recent_capture_arr, cv2.COLOR_BGR2RGB)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

showing this error. what should i do

Please sign in to comment.