def load_data(self): log.info("Loading {}'s data".format(self.name)) if not osp.exists(self.data_path): log.info("No employee data was found") else: with open(self.data_path, 'r') as file: self.data = json.load(file)
def load_faces(self): for emp in self.employees: if not emp.encoded_face.any(): log.info("{}'s face did not encoded yet. encoding and saving.". format(emp.name)) img = self.load_img(emp.img_path) emp.save_encoded_face(self.faces_encodings_from_img(img)[0])
def run(self): log.info("Running!") while True: # Get a new frame (into self.np_output) self.camera.capture(self.np_output, format="rgb") face_loc_list = self.face_locations(self.np_output) if face_loc_list: self.analyze_frame(face_loc_list)
def __init__(self, name): self.name = name self.conf = Config() self.img_path = self.conf.employee_img_path(name) self.encoded_img_path = self.conf.employee_encoded_img_path(name) self.data_path = self.conf.employee_data_path(name) self.encoded_face = None self.data = {} self.in_timestamp = None self.last_timestamp = None self.load_encoding() log.info("Employee named {} was loaded".format(self.name))
def load_employees(self): log.info("Getting all existed employees") # Check if there is an employees dir if not osp.exists(self.employees_dir): log.info("Creating employees directory") os.makedirs(self.employees_dir) # Get all saved emplyees directories saved_employees = os.listdir(self.employees_dir) if not len(saved_employees): return None # Check if employees directories are valid. If so, add them for emp in saved_employees: img_path = osp.join(self.employees_dir, emp, "{}.jpg".format(emp)) if osp.exists(img_path): self.employees.append(Employee(emp))
from src.employee import Employee, Employees_manager from src.recognizer import Recognizer from src.config import log log.info("Begin!") manager = Employees_manager() employees = manager.get_employees() recognizer = Recognizer(employees) recognizer.run()
def save_img_to_archive(self, img_path, img): log.info("Saving image: {}".format(img_path)) Image.fromarray(img).save(img_path)
def save_encoded_face(self, encoded_face): log.info("Saving {}'s face encoding".format(self.name)) self.encoded_face = encoded_face np.save(self.encoded_img_path, self.encoded_face)
def load_encoding(self): log.info("Loading {}'s face encoding".format(self.name)) if not osp.exists(self.encoded_img_path): log.info("No face encoding was found") else: self.encoded_face = np.load(self.encoded_img_path)
def save_data(self): log.info("Saving {}'s data".format(self.name)) with open(self.data_path, 'w') as file: json.dump(self.data, file) del self.data
def analyze_frame(self, face_loc_list): log.info("Found {} faces".format(len(face_loc_list))) faces_encodings = self.faces_encodings_from_img( self.np_output, face_loc_list) for emp in self.employees: self.compare_faces(emp, faces_encodings)
def compare_faces(self, employee, frame_faces, tolerance=0.6): if True in list( np.linalg.norm(frame_faces - employee.encoded_face, axis=1) <= tolerance): log.info("{} was found!".format(employee.name)) employee.add_timestamp(self.np_output, datetime.now())