| |
|
|
| import cv2 |
| import numpy as np |
| from insightface.app import FaceAnalysis |
|
|
| |
| app = FaceAnalysis(name='buffalo_sc', allowed_modules=['detection', 'recognition']) |
| app.prepare(ctx_id=-1, det_size=(640, 640)) |
|
|
| |
| 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.") |
|
|
| |
| faces = app.get(img) |
|
|
| print("Detected faces:", len(faces)) |
|
|
| for i, f in enumerate(faces): |
| |
| bbox = [float(x) for x in f.bbox] |
|
|
| |
| landmarks = f.kps.tolist() if hasattr(f, "kps") and f.kps is not None else [] |
|
|
| |
| det_score = float(getattr(f, "det_score", 0.0)) |
|
|
| |
| 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) |
|
|
| |
| norm = np.linalg.norm(emb) |
| if norm > 0: |
| emb = emb / norm |
|
|
| print(f" embedding sample (first 6): {emb[:6].tolist()}") |