diff --git a/API/route.py b/API/route.py index 8aa14a7..b4cc2a8 100644 --- a/API/route.py +++ b/API/route.py @@ -21,13 +21,10 @@ router = APIRouter() - client = Database() collection = "faceEntries" - -# Models for the data to be sent and received by the server class Employee(BaseModel): EmployeeCode: int Name: str @@ -35,15 +32,12 @@ class Employee(BaseModel): Department: str Images: List[str] - class UpdateEmployee(BaseModel): Name: str gender: str Department: str Images: List[str] - -# To create new entries of employee @router.post("/create_new_faceEntry") async def create_new_faceEntry(Employee: Employee): """ @@ -74,13 +68,13 @@ async def create_new_faceEntry(Employee: Employee): image_filename = f"{Name}.png" pil_image.save(image_filename) pil_image.save(f"Images\dbImages\{Name}.jpg") - face_image_data = DeepFace.detectFace( - image_filename, enforce_detection=False + face_image_data = DeepFace.extract_faces( + image_filename, detector_backend="mtcnn", enforce_detection=False ) - plt.imsave(f"Images/Faces/{Name}.jpg", face_image_data) + 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" + image_filename, model_name="Facenet", detector_backend="mtcnn" ) embeddings.append(embedding) logging.info(f"Embedding created Embeddings for {Name}") @@ -103,8 +97,6 @@ async def create_new_faceEntry(Employee: Employee): return {"message": "Face entry created successfully"} - -# To display all records @router.get("/Data/", response_model=list[Employee]) async def get_employees(): """ @@ -128,8 +120,6 @@ async def get_employees(): ] return employees - -# To display specific record info @router.get("/read/{EmployeeCode}", response_class=Response) async def read_employee(EmployeeCode: int): """ @@ -173,27 +163,8 @@ async def read_employee(EmployeeCode: int): except Exception as e: print(e) - @router.put("/update/{EmployeeCode}", response_model=str) async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): - """ - Update employee information based on the provided EmployeeCode. - - Whenever user clicks on update employee button, in the frontend part, all the images will be visible - they can be deleted or new images can be added. - 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. - - Returns: - str: A message indicating the success of the update operation. - - Raises: - HTTPException: If the employee with the given EmployeeCode is not found. - HTTPException: If no data was updated during the update operation. - HTTPException: If an internal server error occurs. - """ logging.info(f"Updating for EmployeeCode: {EmployeeCode}") try: user_id = client.find_one( @@ -202,9 +173,8 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): 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 embeddings = [] for encoded_image in encoded_images: @@ -213,11 +183,11 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): image_filename = f"{Employee.Name}.png" pil_image.save(image_filename) logging.info(f"Image saved {Employee.Name}") - face_image_data = DeepFace.detectFace( - image_filename, enforce_detection=False + face_image_data = DeepFace.extract_faces( + image_filename, detector_backend="mtcnn", enforce_detection=False ) embedding = DeepFace.represent( - image_filename, model_name="Facenet" + image_filename, model_name="Facenet", detector_backend="mtcnn" ) logging.info(f"Embedding created {Employee.Name}") embeddings.append(embedding) @@ -239,8 +209,6 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): except Exception as e: raise HTTPException(status_code=500, detail="Internal server error") - -# To delete employee record @router.delete("/delete/{EmployeeCode}") async def delete_employees(EmployeeCode: int): """