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 857ea37 commit f507553
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions API/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = []
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -175,16 +175,16 @@ 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.
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.
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.
Expand All @@ -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
Expand All @@ -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"}

0 comments on commit f507553

Please sign in to comment.