mirror of
				https://github.com/blakeblackshear/frigate.git
				synced 2025-10-27 10:52:11 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			69 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """Object classification APIs."""
 | |
| 
 | |
| import logging
 | |
| import os
 | |
| from pathvalidate import sanitize_filename
 | |
| 
 | |
| from fastapi import APIRouter, Request, UploadFile
 | |
| from fastapi.responses import JSONResponse
 | |
| 
 | |
| from frigate.api.defs.tags import Tags
 | |
| from frigate.const import FACE_DIR
 | |
| from frigate.embeddings import EmbeddingsContext
 | |
| 
 | |
| logger = logging.getLogger(__name__)
 | |
| 
 | |
| router = APIRouter(tags=[Tags.events])
 | |
| 
 | |
| 
 | |
| @router.get("/faces")
 | |
| def get_faces():
 | |
|     face_dict: dict[str, list[str]] = {}
 | |
| 
 | |
|     for name in os.listdir(FACE_DIR):
 | |
|         face_dict[name] = []
 | |
|         for file in os.listdir(os.path.join(FACE_DIR, name)):
 | |
|             face_dict[name].append(file)
 | |
| 
 | |
|     return JSONResponse(status_code=200, content=face_dict)
 | |
| 
 | |
| 
 | |
| @router.post("/faces/{name}")
 | |
| async def register_face(request: Request, name: str, file: UploadFile):
 | |
|     # if not file.content_type.startswith("image"):
 | |
|     #    return JSONResponse(
 | |
|     #        status_code=400,
 | |
|     #        content={
 | |
|     #            "success": False,
 | |
|     #            "message": "Only an image can be used to register a face.",
 | |
|     #        },
 | |
|     #    )
 | |
| 
 | |
|     context: EmbeddingsContext = request.app.embeddings
 | |
|     context.register_face(name, await file.read())
 | |
|     return JSONResponse(
 | |
|         status_code=200,
 | |
|         content={"success": True, "message": "Successfully registered face."},
 | |
|     )
 | |
| 
 | |
| 
 | |
| @router.post("/faces/{name}/delete")
 | |
| def deregister_faces(request: Request, name: str, body: dict = None):
 | |
|     json: dict[str, any] = body or {}
 | |
|     list_of_ids = json.get("ids", "")
 | |
| 
 | |
|     if not list_of_ids or len(list_of_ids) == 0:
 | |
|         return JSONResponse(
 | |
|             content=({"success": False, "message": "Not a valid list of ids"}),
 | |
|             status_code=404,
 | |
|         )
 | |
| 
 | |
|     context: EmbeddingsContext = request.app.embeddings
 | |
|     context.delete_face_ids(
 | |
|         name, map(lambda file: sanitize_filename(file), list_of_ids)
 | |
|     )
 | |
|     return JSONResponse(
 | |
|         content=({"success": True, "message": "Successfully deleted faces."}),
 | |
|         status_code=200,
 | |
|     )
 |