github-actions
Deploy to Hugging Face
c794b6b
Raw
History Blame Contribute Delete
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()}")