-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add vector_search function for pipeline aggregation #30
Changes from 2 commits
c8c30fc
a86697a
df1a373
c3a0f54
c4f777e
f25d6ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,22 +8,27 @@ | |
|
||
from bson import ObjectId | ||
from deepface import DeepFace | ||
from fastapi import APIRouter, HTTPException, Response | ||
from fastapi import APIRouter, HTTPException, Response, UploadFile, File | ||
from matplotlib import pyplot as plt | ||
from PIL import Image | ||
from pydantic import BaseModel | ||
|
||
from API.database import Database | ||
from API.utils import init_logging_config | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
init_logging_config() | ||
|
||
MONGO_URI = os.getenv("MONGO_URL1") | ||
router = APIRouter() | ||
|
||
|
||
client = Database() | ||
client2 = Database(MONGO_URI, "FaceRec") | ||
|
||
collection = "faceEntries" | ||
collection2 = "ImageDB" | ||
|
||
|
||
# Models for the data to be sent and received by the server | ||
Devasy23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -267,3 +272,37 @@ async def delete_employees(EmployeeCode: int): | |
client.find_one_and_delete(collection, {"EmployeeCode": EmployeeCode}) | ||
|
||
return {"Message": "Successfully Deleted"} | ||
|
||
|
||
@router.post("/recognize_face", response_class=Response) | ||
async def recognize_face(Face: UploadFile = File(...)): | ||
""" | ||
Recognize a face from the provided image. | ||
|
||
Args: | ||
Face (UploadFile): The image file to be recognized. | ||
|
||
Returns: | ||
Response: A response object containing the recognized employee information in JSON format. | ||
|
||
Raises: | ||
HTTPException: If an internal server error occurs. | ||
""" | ||
logging.info("Recognizing Face") | ||
try: | ||
img_data = await Face.read() | ||
with open("temp.png", "wb") as f: | ||
f.write(img_data) | ||
|
||
embedding = DeepFace.represent(img_path="temp.png", model_name="Facenet") | ||
result = client2.vector_search(collection2, embedding[0]['embedding']) | ||
logging.info(f"Result: {result}") | ||
os.remove("temp.png") | ||
except Exception as e: | ||
logging.error(f"Error: {e}") | ||
os.remove("temp.png") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
return Response( | ||
content=bytes(json.dumps(result[0], default=str), "utf-8"), | ||
media_type="application/json", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
from tempfile import NamedTemporaryFile
async def recognize_face(Face: UploadFile = File(...)):
logging.info("Recognizing Face")
try:
img_data = await Face.read()
with NamedTemporaryFile(delete=True, suffix=".png") as temp_file:
temp_file.write(img_data)
temp_file.flush()
embedding = DeepFace.represent(img_path=temp_file.name, model_name="Facenet")
result = client2.vector_search(collection2, embedding[0]['embedding'])
except Exception as e:
logging.error(f"Error: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
Overall, ensure every aspect adheres to scalability, security, and maintainability principles. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested changes has been made 🎉 |
This file was deleted.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extracting the query and projection into variables for better readability and maintainability. This practice enhances code clarity and simplifies future modifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes has been made 🎉