Skip to content

Commit

Permalink
Updated create_new_faceEntry function to handle multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
Devasy23 committed Mar 8, 2024
1 parent 4e65f8d commit f4a4c48
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
14 changes: 9 additions & 5 deletions API/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import os
from datetime import datetime
from io import BytesIO

from API.utils import init_logging_config
from typing import List

from bson import ObjectId
from deepface import DeepFace
from fastapi import APIRouter, Form, HTTPException, Response
Expand All @@ -16,6 +15,7 @@
from pymongo import MongoClient

from API.database import Database
from API.utils import init_logging_config

init_logging_config()

Expand Down Expand Up @@ -64,24 +64,28 @@ async def create_new_faceEntry(Employee: Employee):
Department = Employee.Department
encoded_images = Employee.Images
time = datetime.now()

embeddings = []
for encoded_image in encoded_images:
img_recovered = base64.b64decode(encoded_image) # decode base64string
pil_image = Image.open(BytesIO(img_recovered))
logging.info(f"Image opened {Name}")
image_filename = f"{Name}.png"
pil_image.save(image_filename)
pil_image.save(f"Images\dbImages\{Name}.jpg")
face_image_data = DeepFace.extract_faces(
image_filename, detector_backend="mtcnn", enforce_detection=False
)
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"
)
embeddings.append(embedding)
logging.info(f"Embedding created Embeddings for {Name}")
os.remove(image_filename)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.


logging.debug(f"About to insert Embeddings: {embeddings}")
# Store the data in the database
client.insert_one(
collection,
Expand All @@ -95,7 +99,7 @@ async def create_new_faceEntry(Employee: Employee):
"Images": encoded_images,
},
)

return {"message": "Face entry created successfully"}


Expand Down
4 changes: 2 additions & 2 deletions API/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def format(self, record):
stderr_handler.setFormatter(CustomFormatter())
logger.addHandler(stderr_handler)

file_handler = logging.FileHandler("app.log", mode="w")
file_handler = logging.FileHandler("app.log", mode="w")
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(CustomFormatter(True))
logger.addHandler(file_handler)
logger.addHandler(file_handler)
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## [Unreleased]
## [0.0.1] - 2024-03-08 - 2:30

### Added
- Implemented all test cases in `test_face_cycle`
- Implemented mock test cases for `test_face_cycle` to work on online runners
- Implemented mock test cases for `test_face_cycle` to work on online runners

## [0.1.0] - 2024-03-08 - 17:10

### Added
- Updated `create_new_faceEntry` function in [`route.py`](route/route.py) to handle multiple images for each employee.
- Updated `test_face_lifecycle` function in [`test_face_cycle.py`](testing/test_face_cycle.py) to handle multiple images for each employee in the test data.

### Changed
- Modified the `Employee` and `UpdateEmployee` models in [`route.py`](route/route.py) to include a list of images instead of a single image.
- Adjusted the mock data and assertions in [`test_face_cycle.py`](testing/test_face_cycle.py) to handle multiple images for each employee.

### Fixed
- Resolved an issue where the `create_new_faceEntry` function in [`route.py`](route/route.py) was not correctly processing multiple images for each employee.
39 changes: 23 additions & 16 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,16 @@
"outputs": [],
"source": [
"url = \"http://127.0.0.1:8000/create_new_faceEntry\"\n",
"resp = requests.post(url=url, json={\n",
" \"EmployeeCode\": 134,\n",
" \"Name\": \"Name\",\n",
" \"gender\": \"gender\",\n",
" \"Department\": \"Department\",\n",
" \"Image\": \"your_image\",\n",
"})"
"resp = requests.post(\n",
" url=url,\n",
" json={\n",
" \"EmployeeCode\": 134,\n",
" \"Name\": \"Name\",\n",
" \"gender\": \"gender\",\n",
" \"Department\": \"Department\",\n",
" \"Image\": \"your_image\",\n",
" },\n",
")"
]
},
{
Expand Down Expand Up @@ -676,16 +679,20 @@
],
"source": [
"from API.database import Database\n",
"\n",
"EmployeeCode = 1\n",
"client = Database()\n",
"client.find_one(\"faceEntries\", filter={\"EmployeeCode\": EmployeeCode},\n",
" projection={\n",
" \"Name\": True,\n",
" \"gender\": True,\n",
" \"Department\": True,\n",
" \"Image\": True,\n",
" \"_id\": False,\n",
" })"
"client.find_one(\n",
" \"faceEntries\",\n",
" filter={\"EmployeeCode\": EmployeeCode},\n",
" projection={\n",
" \"Name\": True,\n",
" \"gender\": True,\n",
" \"Department\": True,\n",
" \"Image\": True,\n",
" \"_id\": False,\n",
" },\n",
")"
]
}
],
Expand All @@ -705,7 +712,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f4a4c48

Please sign in to comment.