From f5075534341a8bf33c3860e91831e3b7f4f0b449 Mon Sep 17 00:00:00 2001 From: "codeshwar-preview[bot]" <160849357+codeshwar-preview[bot]@users.noreply.github.com> Date: Sat, 16 Mar 2024 17:58:21 +0000 Subject: [PATCH] Updated the file in response to the PR comment --- API/route.py | 67 ++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/API/route.py b/API/route.py index b5da194..15908de 100644 --- a/API/route.py +++ b/API/route.py @@ -45,12 +45,12 @@ class UpdateEmployee(BaseModel): # To create new entries of employee @router.post("/create_new_faceEntry") -async def create_new_faceEntry(Employee: Employee): +async def create_new_faceEntry(employee: Employee): """ Create a new face entry for an employee. Args: - Employee (Employee): The employee object containing the employee details. + employee (Employee): The employee object containing the employee details. Returns: dict: A dictionary with a success message. @@ -59,11 +59,11 @@ async def create_new_faceEntry(Employee: Employee): None """ logging.info("Creating new face entry") - Name = Employee.Name - EmployeeCode = Employee.EmployeeCode - gender = Employee.gender - Department = Employee.Department - encoded_images = Employee.Images + Name = employee.Name + EmployeeCode = employee.EmployeeCode + gender = employee.gender + Department = employee.Department + encoded_images = employee.Images time = datetime.now() embeddings = [] @@ -73,11 +73,11 @@ async def create_new_faceEntry(Employee: Employee): logging.info(f"Image opened {Name}") image_filename = f"{Name}.png" pil_image.save(image_filename) - pil_image.save(os.path.join("Images", "dbImages", f"{Name}.jpg")) + pil_image.save(f"Images\dbImages\{Name}.jpg") face_image_data = DeepFace.extract_faces( image_filename, detector_backend="mtcnn", enforce_detection=False ) - plt.imsave(os.path.join("Images", "Faces", f"{Name}.jpg"), face_image_data[0]["face"]) + plt.imsave(f"Images/Faces/{Name}.jpg", face_image_data[0]["face"]) logging.info(f"Face saved {Name}") embedding = DeepFace.represent( image_filename, model_name="Facenet", detector_backend="mtcnn" @@ -131,12 +131,12 @@ async def get_employees(): # To display specific record info @router.get("/read/{EmployeeCode}", response_class=Response) -async def read_employee(EmployeeCode: int): +async def read_employee(employeeCode: int): """ Retrieve employee information based on the provided EmployeeCode. Args: - EmployeeCode (int): The unique code of the employee. + employeeCode (int): The unique code of the employee. Returns: Response: A response object containing the employee information in JSON format. @@ -145,12 +145,12 @@ async def read_employee(EmployeeCode: int): HTTPException: If the employee is not found. """ - logging.info(f"Display information for {EmployeeCode}") + logging.info(f"Display information for {employeeCode}") try: - logging.info(f"Start {EmployeeCode}") + logging.info(f"Start {employeeCode}") items = client.find_one( collection, - filter={"EmployeeCode": EmployeeCode}, + filter={"EmployeeCode": employeeCode}, projection={ "Name": True, "gender": True, @@ -175,7 +175,7 @@ async def read_employee(EmployeeCode: int): @router.put("/update/{EmployeeCode}", response_model=str) -async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): +async def update_employees(employeeCode: int, employee: UpdateEmployee): """ Update employee information based on the provided EmployeeCode. @@ -183,8 +183,8 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): Accordingly, the embeddings will be recalculated and updated in the database. Args: - EmployeeCode (int): The unique code of the employee to be updated. - Employee (UpdateEmployee): The updated employee data. + employeeCode (int): The unique code of the employee to be updated. + employee (UpdateEmployee): The updated employee data. Returns: str: A message indicating the success of the update operation. @@ -194,32 +194,31 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): HTTPException: If no data was updated during the update operation. HTTPException: If an internal server error occurs. """ - logging.info(f"Updating for EmployeeCode: {EmployeeCode}") + logging.info(f"Updating for EmployeeCode: {employeeCode}") try: user_id = client.find_one( - collection, {"EmployeeCode": EmployeeCode}, projection={"_id": True} + collection, {"EmployeeCode": employeeCode}, projection={"_id": True} ) print(user_id) if not user_id: raise HTTPException(status_code=404, detail="Employee not found") - Employee_data = Employee.model_dump(by_alias=True, exclude_unset=True) + Employee_data = employee.dict(by_alias=True, exclude_unset=True) logging.info(f"Employee data {Employee_data}") - # Calculate and store embeddings for the updated image array - encoded_images = Employee.Images + encoded_images = employee.Images embeddings = [] for encoded_image in encoded_images: img_recovered = base64.b64decode(encoded_image) # decode base64string pil_image = Image.open(BytesIO(img_recovered)) - image_filename = f"{Employee.Name}.png" + image_filename = f"{employee.Name}.png" pil_image.save(image_filename) - logging.info(f"Image saved {Employee.Name}") + logging.info(f"Image saved {employee.Name}") face_image_data = DeepFace.extract_faces( image_filename, detector_backend="mtcnn", enforce_detection=False ) embedding = DeepFace.represent( image_filename, model_name="Facenet", detector_backend="mtcnn" ) - logging.info(f"Embedding created {Employee.Name}") + logging.info(f"Embedding created {employee.Name}") embeddings.append(embedding) os.remove(image_filename) Employee_data["embeddings"] = embeddings @@ -242,29 +241,19 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): # To delete employee record @router.delete("/delete/{EmployeeCode}") -async def delete_employees(EmployeeCode: int): +async def delete_employees(employeeCode: int): """ Delete an employee from the collection based on the provided EmployeeCode. Args: - EmployeeCode (int): The unique code of the employee to be deleted. - - Returns: - dict: A dictionary containing a success message. - - """ - """ - Delete an employee from the collection based on the provided EmployeeCode. - - Args: - EmployeeCode (int): The unique code of the employee to be deleted. + employeeCode (int): The unique code of the employee to be deleted. Returns: dict: A dictionary containing a success message. """ logging.info("Deleting Employee") - logging.info(f"Deleting for EmployeeCode: {EmployeeCode}") - client.find_one_and_delete(collection, {"EmployeeCode": EmployeeCode}) + logging.info(f"Deleting for EmployeeCode: {employeeCode}") + client.find_one_and_delete(collection, {"EmployeeCode": employeeCode}) return {"Message": "Successfully Deleted"} \ No newline at end of file