File size: 10,313 Bytes
0fef0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#4:
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
import cv2
import base64
import logging
import os
from pathlib import Path
from face_recognition_system import FaceRecognitionSystem

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Initialize FastAPI app
app = FastAPI(
    title="Face Recognition API",
    description="API for face detection and recognition using InsightFace",
    version="1.0.0"
)

# Add CORS middleware for Hugging Face Spaces
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Create necessary directories
MODELS_DIR = Path("models")
KNOWN_FACES_DIR = Path("known_faces")
for directory in [MODELS_DIR, KNOWN_FACES_DIR]:
    directory.mkdir(parents=True, exist_ok=True)

# Initialize face recognition system
try:
    face_recog_system = FaceRecognitionSystem(
        model_name="buffalo_l",
        model_root=str(MODELS_DIR)
    )
    face_recog_system.process_known_faces(str(KNOWN_FACES_DIR))
    logger.info("Face recognition system initialized successfully")
except Exception as e:
    logger.error(f"Failed to initialize face recognition system: {e}")
    raise

@app.get("/")
async def root():
    """Health check endpoint"""
    model_files = list(MODELS_DIR.glob("*"))
    known_faces = list(KNOWN_FACES_DIR.glob("*"))
    return {
        "status": "ok",
        "message": "Face Recognition API is running",
        "model_directory": str(MODELS_DIR),
        "known_faces_directory": str(KNOWN_FACES_DIR),
        "model_files": [str(f.name) for f in model_files],
        "known_faces": [str(f.name) for f in known_faces]
    }

@app.post("/detect_faces")
async def detect_faces(file: UploadFile = File(...)):
    """

    Endpoint to detect and identify faces in an uploaded image

    """
    try:
        # Validate file type
        if not file.content_type.startswith('image/'):
            raise HTTPException(status_code=400, detail="File must be an image")

        # Read and decode image
        image_data = await file.read()
        nparr = np.frombuffer(image_data, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if img is None:
            raise HTTPException(status_code=400, detail="Failed to decode image")

        # Process image
        detected_img = face_recog_system.detect_and_identify(img)

        # Encode processed image to base64
        success, buffer = cv2.imencode('.jpg', detected_img)
        if not success:
            raise HTTPException(status_code=500, detail="Failed to encode processed image")

        processed_image_base64 = base64.b64encode(buffer).decode("utf-8")

        # Prepare response
        serializable_embeddings = {
            name: embedding.tolist() if isinstance(embedding, np.ndarray) else embedding 
            for name, embedding in face_recog_system.known_face_embeddings.items()
        }

        return JSONResponse(content={
            "status": "success",
            "processed_image": processed_image_base64,
            "faces": serializable_embeddings
        })

    except HTTPException as he:
        raise he
    except Exception as e:
        logger.error(f"Error processing image: {e}")
        raise HTTPException(status_code=500, detail=str(e))

# Configuration for Hugging Face Spaces
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)




















#3:

# from fastapi import FastAPI, File, UploadFile, HTTPException
# from fastapi.responses import JSONResponse
# from fastapi.middleware.cors import CORSMiddleware
# import numpy as np
# import cv2
# import base64
# import logging
# from face_recognition_system import FaceRecognitionSystem

# # Set up logging
# logging.basicConfig(
#     level=logging.INFO,
#     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# )
# logger = logging.getLogger(__name__)

# # Initialize FastAPI app
# app = FastAPI(
#     title="Face Recognition API",
#     description="API for face detection and recognition using InsightFace",
#     version="1.0.0"
# )

# # Add CORS middleware for Hugging Face Spaces
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],
#     allow_credentials=True,
#     allow_methods=["*"],
#     allow_headers=["*"],
# )

# # Initialize face recognition system
# try:
#     face_recog_system = FaceRecognitionSystem()
#     # Update the path to match your Hugging Face Spaces directory structure
#     face_recog_system.process_known_faces("known_faces")
#     logger.info("Face recognition system initialized successfully")
# except Exception as e:
#     logger.error(f"Failed to initialize face recognition system: {e}")
#     raise

# @app.get("/")
# async def root():
#     """Health check endpoint"""
#     return {"status": "ok", "message": "Face Recognition API is running"}

# @app.post("/detect_faces")
# async def detect_faces(file: UploadFile = File(...)):
#     """
#     Endpoint to detect and identify faces in an uploaded image
#     """
#     try:
#         # Validate file type
#         if not file.content_type.startswith('image/'):
#             raise HTTPException(status_code=400, detail="File must be an image")

#         # Read and decode image
#         image_data = await file.read()
#         nparr = np.frombuffer(image_data, np.uint8)
#         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

#         if img is None:
#             raise HTTPException(status_code=400, detail="Failed to decode image")

#         # Process image
#         detected_img = face_recog_system.detect_and_identify(img)

#         # Encode processed image to base64
#         success, buffer = cv2.imencode('.jpg', detected_img)
#         if not success:
#             raise HTTPException(status_code=500, detail="Failed to encode processed image")

#         processed_image_base64 = base64.b64encode(buffer).decode("utf-8")

#         # Prepare response
#         serializable_embeddings = {
#             name: embedding.tolist() if isinstance(embedding, np.ndarray) else embedding 
#             for name, embedding in face_recog_system.known_face_embeddings.items()
#         }

#         return JSONResponse(content={
#             "status": "success",
#             "processed_image": processed_image_base64,
#             "faces": serializable_embeddings
#         })

#     except HTTPException as he:
#         raise he
#     except Exception as e:
#         logger.error(f"Error processing image: {e}")
#         raise HTTPException(status_code=500, detail=str(e))

# # Configuration for Hugging Face Spaces
# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=7860)





# initial:
# from fastapi import FastAPI

# app = FastAPI()

# @app.get("/")
# def home():
#     '''Fuck Everyday Bitch'''
#     return {"Everything's": "OK bTICH✅"}


# final:
# #2
# from fastapi import FastAPI, File, UploadFile
# from fastapi.responses import JSONResponse
# import numpy as np
# import cv2
# import base64
# import logging
# from face_recognition_system import FaceRecognitionSystem  # import your class

# # Set up logging
# logging.basicConfig(level=logging.INFO)

# app = FastAPI()
# face_recog_system = FaceRecognitionSystem()

# # Load known faces
# try:
#     face_recog_system.process_known_faces("./data/known/custom/")
#     logging.info("Loaded known faces successfully.")
# except Exception as e:
#     logging.error(f"Error loading known faces: {e}")

# @app.post("/detect_faces")
# async def detect_faces(file: UploadFile = File(...)):
#     try:
#         # Read and decode image from the uploaded file
#         image_data = await file.read()
#         nparr = np.frombuffer(image_data, np.uint8)
#         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

#         # Check if image is loaded
#         if img is None:
#             logging.error("Failed to decode image. Ensure the uploaded file is a valid image.")
#             return JSONResponse(content={"error": "Invalid image file"}, status_code=400)

#         # Run detection and identification
#         detected_img = face_recog_system.detect_and_identify(img)

#         # Encode imNONOFage to base64
#         success, buffer = cv2.imencode('.jpg', detected_img)
#         if not success:
#             logging.error("Image encoding failed.")
#             return JSONResponse(content={"error": "Image encoding failed"}, status_code=500)

#         processed_image_base64 = base64.b64encode(buffer).decode("utf-8")

#         # Optional: Check if face embeddings were created
#         if not face_recog_system.known_face_embeddings:
#             logging.warning("No faces detected.")

#         # NOTE:
#         # Convert numpy arrays to lists for JSON serialization
#         serializable_embeddings = {
#             name: embedding.tolist() if isinstance(embedding, np.ndarray) else embedding 
#             for name, embedding in face_recog_system.known_face_embeddings.items()
#         }
#         return JSONResponse(content={
#             "processed_image": processed_image_base64, 
#             "faces": serializable_embeddings
#         })
        
#         # return JSONResponse(content={"processed_image": processed_image_base64, "faces": face_recog_system.known_face_embeddings})

#     except Exception as e:
#         logging.error(f"Error processing image: {e}")
#         return JSONResponse(content={"error": "An error occurred while processing the image"}, status_code=500)


# # main:
# # NOTE: ALWAYS FIRST CHECK IPv4-Address via: <ipconfig>
# # import uvicorn
# # if __name__ == "__main__":
# #     uvicorn.run(app='app:app',
# #                 host='192.168.1.17', port=7860, reload=True)