Skip to content

Commit

Permalink
Updated the file in response to the PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
codeshwar-preview[bot] authored Mar 16, 2024
1 parent ba5626c commit 711e2e3
Showing 1 changed file with 8 additions and 40 deletions.
48 changes: 8 additions & 40 deletions API/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,23 @@

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
gender: str
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):
"""
Expand Down Expand Up @@ -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}")
Expand All @@ -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():
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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):
"""
Expand Down

0 comments on commit 711e2e3

Please sign in to comment.