From fb316874ef91f95d9c17b5eba65c7199985fe3ce Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 29 Jan 2025 17:02:40 -0700 Subject: [PATCH] Quick fix for face rec (#16226) * Check both * Fix api order --- frigate/api/classification.py | 54 +++++++++---------- .../real_time/face_processor.py | 2 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/frigate/api/classification.py b/frigate/api/classification.py index 65a85a056..b42e3ed8b 100644 --- a/frigate/api/classification.py +++ b/frigate/api/classification.py @@ -41,19 +41,36 @@ def get_faces(): return JSONResponse(status_code=200, content=face_dict) -@router.post("/faces/{name}") -async def register_face(request: Request, name: str, file: UploadFile): +@router.post("/faces/reprocess") +def reclassify_face(request: Request, body: dict = None): if not request.app.frigate_config.face_recognition.enabled: return JSONResponse( status_code=400, content={"message": "Face recognition is not enabled.", "success": False}, ) + json: dict[str, any] = body or {} + training_file = os.path.join( + FACE_DIR, f"train/{sanitize_filename(json.get('training_file', ''))}" + ) + + if not training_file or not os.path.isfile(training_file): + return JSONResponse( + content=( + { + "success": False, + "message": f"Invalid filename or no file exists: {training_file}", + } + ), + status_code=404, + ) + context: EmbeddingsContext = request.app.embeddings - result = context.register_face(name, await file.read()) + response = context.reprocess_face(training_file) + return JSONResponse( - status_code=200 if result.get("success", True) else 400, - content=result, + content=response, + status_code=200, ) @@ -100,36 +117,19 @@ def train_face(request: Request, name: str, body: dict = None): ) -@router.post("/faces/reprocess") -def reclassify_face(request: Request, name: str, body: dict = None): +@router.post("/faces/{name}") +async def register_face(request: Request, name: str, file: UploadFile): if not request.app.frigate_config.face_recognition.enabled: return JSONResponse( status_code=400, content={"message": "Face recognition is not enabled.", "success": False}, ) - json: dict[str, any] = body or {} - training_file = os.path.join( - FACE_DIR, f"train/{sanitize_filename(json.get('training_file', ''))}" - ) - - if not training_file or not os.path.isfile(training_file): - return JSONResponse( - content=( - { - "success": False, - "message": f"Invalid filename or no file exists: {training_file}", - } - ), - status_code=404, - ) - context: EmbeddingsContext = request.app.embeddings - response = context.reprocess_face(training_file) - + result = context.register_face(name, await file.read()) return JSONResponse( - content=response, - status_code=200, + status_code=200 if result.get("success", True) else 400, + content=result, ) diff --git a/frigate/data_processing/real_time/face_processor.py b/frigate/data_processing/real_time/face_processor.py index ee981f5ec..086c59658 100644 --- a/frigate/data_processing/real_time/face_processor.py +++ b/frigate/data_processing/real_time/face_processor.py @@ -215,7 +215,7 @@ class FaceProcessor(RealTimeProcessorApi): if not self.landmark_detector: return None - if not self.recognizer: + if not self.label_map or not self.recognizer: self.__build_classifier() if not self.recognizer: