Download backend/Face_Recognition/test_face.py from SolutionChallenge/solution_challenge_backend: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/SolutionChallenge/solution_challenge_backend/resolve/main/backend/Face_Recognition/test_face.py
- Command line
-
hf download hf://spaces/SolutionChallenge/solution_challenge_backend/backend/Face_Recognition/test_face.py
-
curl -L -o test_face.py https://huggingface.co/spaces/SolutionChallenge/solution_challenge_backend/resolve/main/backend/Face_Recognition/test_face.py
1.5 kB
| # test_face.py | |
| import cv2 | |
| import numpy as np | |
| from insightface.app import FaceAnalysis | |
| # Initialize FaceAnalysis (CPU mode) | |
| app = FaceAnalysis(name='buffalo_sc', allowed_modules=['detection', 'recognition']) | |
| app.prepare(ctx_id=-1, det_size=(640, 640)) # -1 = CPU | |
| # Load image | |
| img_path = "test.jpg" | |
| img = cv2.imread(img_path) | |
| if img is None: | |
| raise SystemExit(f"ERROR: could not open image at {img_path}. " | |
| f"Put a valid file named test.jpg in the folder.") | |
| # Run detection + recognition | |
| faces = app.get(img) | |
| print("Detected faces:", len(faces)) | |
| for i, f in enumerate(faces): | |
| # Bounding box | |
| bbox = [float(x) for x in f.bbox] | |
| # Landmarks (if available) | |
| landmarks = f.kps.tolist() if hasattr(f, "kps") and f.kps is not None else [] | |
| # Detection confidence | |
| det_score = float(getattr(f, "det_score", 0.0)) | |
| # Embedding | |
| embedding = f.embedding if hasattr(f, "embedding") else None | |
| emb_len = len(embedding) if embedding is not None else 0 | |
| print(f"\nFace {i}:") | |
| print(f" bbox={bbox}") | |
| print(f" det_score={det_score}") | |
| print(f" embedding_length={emb_len}") | |
| if landmarks: | |
| print(f" landmarks={landmarks}") | |
| if embedding is not None: | |
| emb = np.array(embedding, dtype=np.float32) | |
| # Normalize embedding (important for cosine similarity) | |
| norm = np.linalg.norm(emb) | |
| if norm > 0: | |
| emb = emb / norm | |
| print(f" embedding sample (first 6): {emb[:6].tolist()}") |