From dcff0d034d7051ec20cea6b698bc667fdf4fc197 Mon Sep 17 00:00:00 2001 From: Devasy Patel <110348311+Devasy23@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:30:14 +0530 Subject: [PATCH] Refactor API route and remove deprecated code --- API/route.py | 6 +-- route/__init__.py | 0 route/route.py | 100 ------------------------------------- testing/test_face_cycle.py | 3 +- 4 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 route/__init__.py delete mode 100644 route/route.py diff --git a/API/route.py b/API/route.py index 0807f30..0f7b048 100644 --- a/API/route.py +++ b/API/route.py @@ -174,13 +174,12 @@ async def read_employee(EmployeeCode: int): 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. + + 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: @@ -240,6 +239,7 @@ 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): diff --git a/route/__init__.py b/route/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/route/route.py b/route/route.py deleted file mode 100644 index 100979a..0000000 --- a/route/route.py +++ /dev/null @@ -1,100 +0,0 @@ -import os -import uuid -from datetime import datetime -from random import randint - -from deepface import DeepFace -from fastapi import FastAPI, File, UploadFile -from fastapi.responses import FileResponse -from matplotlib import pyplot as plt -from pymongo import MongoClient - -IMAGEDIR = "test-faces/" - -mongodb_uri = "mongodb://localhost:27017/" -port = 8000 -client = MongoClient(mongodb_uri, port) - -db = client["ImageDB"] -faceEntries = db["faceEntries"] - -app = FastAPI() - - -# deprecated -@app.post("/upload/") -async def register_face(file: UploadFile = File(...)): - - file.filename = f"{uuid.uuid4()}.jpg" - contents = await file.read() - - # save the file - with open(f"{IMAGEDIR}{file.filename}", "wb") as f: - f.write(contents) - # db.images.insert_one({"filename": file.filename, "contents": contents}) - return {"filename": file.filename} - - -@app.post("/create_new_faceEntry") -async def create_new_faceEntry( - id: int, age: int, gender: str, image: UploadFile = File(...) -): - # Generate a unique ID - # id = uuid.uuid4() - - # Get the current time - time = datetime.now() - - # Read the image file - image_data = await image.read() - print(image.filename) - # Save the original image in a specified directory - with open(f"../Images/dbImages/{image.filename}", "wb") as f: - f.write(image_data) - - # Extract the face from the image - face_image_data = DeepFace.extract_faces( - f"../Images/dbImages/{image.filename}", detector_backend="mtcnn" - ) - - # Save the face image in a specified directory - plt.imsave(f"../Images/Faces/{image.filename}", face_image_data[0]["face"]) - - # Calculate the embeddings of the face image - embeddings = DeepFace.represent( - f"../Images/dbImages/{image.filename}", - model_name="Facenet", - detector_backend="mtcnn", - ) - - # Store the data in the database - db.faceEntries.insert_one( - { - "id": id, - "age": age, - "gender": gender, - "time": time, - "embeddings": embeddings, - # "face-img": face_image_data, - } - ) - - return {"message": "Face entry created successfully"} - - -@app.get("/show/") -async def read_random_file(): - - # get random file from the image directory - files = os.listdir(IMAGEDIR) - random_index = randint(0, len(files) - 1) - - path = f"{IMAGEDIR}{files[random_index]}" - - return FileResponse(path) - - -@app.delete("/delete/{filename}") -async def delete_file(filename: str): - os.remove(f"{IMAGEDIR}/{filename}") - return {"message": "Face deleted successfully"} diff --git a/testing/test_face_cycle.py b/testing/test_face_cycle.py index 72e214f..fdd3488 100644 --- a/testing/test_face_cycle.py +++ b/testing/test_face_cycle.py @@ -14,7 +14,6 @@ @patch("API.database.Database.find_one") @patch("API.database.Database.find") @patch("API.database.Database.insert_one") - def test_face_lifecycle( mock_insert_one: MagicMock, mock_find: MagicMock, @@ -75,7 +74,7 @@ def test_face_lifecycle( with open("./test-faces/devansh.jpg", "rb") as image_file: encoded_string2 = base64.b64encode(image_file.read()).decode("utf-8") - + # Update a face response = client.put( "/update/1",