File size: 1,497 Bytes
c794b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()}")