11import logging
22from binascii import Error as Base64Error
33import base64
4+ from typing import Annotated
45import uuid
56import os
6- from fastapi import APIRouter , HTTPException , status
7+ from fastapi import APIRouter , HTTPException , Query , status
78from app .database .face_clusters import (
89 db_get_cluster_by_id ,
910 db_update_cluster ,
2223 GetClusterImagesData ,
2324 ImageInCluster ,
2425)
25- from app .schemas .images import AddSingleBase64ImageRequest , AddSingleImageRequest
26+ from app .schemas .images import AddSingleBase64ImageRequest , AddSingleImageRequest , InputType
2627from app .utils .faceSearch import perform_face_search
2728
2829
@@ -208,35 +209,32 @@ def get_cluster_images(cluster_id: str):
208209 "/face-search" ,
209210 responses = {code : {"model" : ErrorResponse } for code in [400 , 500 ]},
210211)
211- def face_tagging (payload : AddSingleImageRequest ):
212- image_path = payload .path
213- if not os .path .isfile (image_path ):
214- raise HTTPException (
215- status_code = status .HTTP_400_BAD_REQUEST ,
216- detail = ErrorResponse (
217- success = False ,
218- error = "Invalid file path" ,
219- message = "The provided path is not a valid file" ,
220- ).model_dump (),
221- )
222- return perform_face_search (image_path )
223-
212+ def face_tagging (payload : AddSingleImageRequest | AddSingleBase64ImageRequest , input_type : Annotated [InputType , Query (description = "Choose input type: 'path' or 'base64'" )] = InputType .path ):
213+ image_path = None
224214
225- @router .post ("/face-search-base64" )
226- def face_search_base64 (payload : AddSingleBase64ImageRequest ):
227- base64_data = payload .base64_data
228- if not base64_data :
229- raise HTTPException (
230- status_code = status .HTTP_400_BAD_REQUEST ,
231- detail = ErrorResponse (
232- success = False ,
233- error = "No base64 data" ,
234- message = "Base64 image data is required." ,
235- ).model_dump (),
236- )
215+ if input_type == InputType .path :
216+ if not os .path .isfile (payload .path ):
217+ raise HTTPException (
218+ status_code = status .HTTP_400_BAD_REQUEST ,
219+ detail = ErrorResponse (
220+ success = False ,
221+ error = "Invalid file path" ,
222+ message = "The provided path is not a valid file" ,
223+ ).model_dump (),
224+ )
225+ image_path = payload .path
237226
238- image_path = None
239- try :
227+ elif input_type == InputType .base64 :
228+ base64_data = payload .base64_data
229+ if not base64_data :
230+ raise HTTPException (
231+ status_code = status .HTTP_400_BAD_REQUEST ,
232+ detail = ErrorResponse (
233+ success = False ,
234+ error = "No base64 data" ,
235+ message = "Base64 image data is required." ,
236+ ).model_dump (),
237+ )
240238 try :
241239 image_bytes = base64 .b64decode (base64_data .split ("," )[- 1 ])
242240 except (Base64Error , ValueError ):
@@ -251,14 +249,11 @@ def face_search_base64(payload: AddSingleBase64ImageRequest):
251249 image_id = str (uuid .uuid4 ())[:8 ]
252250 temp_dir = "temp_uploads"
253251 os .makedirs (temp_dir , exist_ok = True )
254- image_path = os .path .join (temp_dir , f"{ image_id } .jpeg" )
252+ local_image_path = os .path .join (temp_dir , f"{ image_id } .jpeg" )
255253
256- with open (image_path , "wb" ) as f :
254+ with open (local_image_path , "wb" ) as f :
257255 f .write (image_bytes )
258256
259- result = perform_face_search (image_path )
260- return result
257+ image_path = local_image_path
261258
262- finally :
263- if image_path and os .path .exists (image_path ):
264- os .remove (image_path )
259+ return perform_face_search (image_path )
0 commit comments