Skip to content

Commit 897f30a

Browse files
committed
Add InsightFace face detection and analysis example
1 parent a051ab5 commit 897f30a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
This code demonstrates the use of SOTA face embeddings model ARCFACE
3+
through wrapper called insightface
4+
5+
It uses Retinaface Face detector to detect faces.
6+
7+
References : https://www.insightface.ai/
8+
"""
9+
10+
import cv2
11+
import numpy as np
12+
from insightface.app import FaceAnalysis
13+
14+
15+
def prepare_model(model_name: str = "buffalo_l", ctx_id: int = -1) -> FaceAnalysis:
16+
"""
17+
Initialize and Prepare face analysis model
18+
19+
Args:
20+
- model_name = default 'buffalo_l'
21+
- ctx_id = gpu number 1,2... , for CPU -1
22+
23+
Return:
24+
Configured FaceAnalysis instance ready for inference
25+
26+
"""
27+
app = FaceAnalysis(model_name)
28+
app.prepare(ctx_id=ctx_id)
29+
30+
return app
31+
32+
33+
def get_facial_data(face_analysis_model: FaceAnalysis, frame: np.ndarray) -> list[dict]:
34+
"""
35+
Extract facial data from a frame using the face analysis model
36+
37+
Args:
38+
- face analysis model : prepared FaceanAlysis instance
39+
- frame : BGR image as numpy array (opencv format)
40+
Returns:
41+
- List of dict, each containing facial data for one detected face:
42+
- bounding_box: Face bounding box coordinates
43+
- keypoints: 5-point facial landmark
44+
- landmark_3d_68: 68-point 3d landmarks
45+
- landmakr_2d_106: 106-points 2d landmarks
46+
- pose: Head pose (pitch yaw roll)
47+
- gender: Predicted gender (0-female,1:male)
48+
- age: Predicted age
49+
- embeddings: 512-dimensional face vector
50+
51+
Example:
52+
>>> # model = prepare_model(ctx_id = -1)
53+
>>> # frame = cv2.imread("test_face.jpg")
54+
>>> # faces = get_facial_data(model,frame)
55+
>>> # len(faces)>=0
56+
57+
"""
58+
59+
results = face_analysis_model.get(frame)
60+
faces = []
61+
for result in results:
62+
face_data = {
63+
"bounding_box": result["bbox"],
64+
"keypoints": result["kps"],
65+
"landmark_3d_68": result["landmark_3d_68"],
66+
"pose": result["pose"],
67+
"landmark_2d_106": result["landmark_2d_106"],
68+
"gender": result["gender"],
69+
"age": result["age"],
70+
"embedding": result["embedding"],
71+
}
72+
faces.append(face_data)
73+
74+
return faces
75+
76+
77+
def run_webcam_demo(ctx_id: int = -1, source: str | int = 0) -> None:
78+
"""
79+
Run live face analysis on wecam feed
80+
81+
Args:
82+
- ctx_id: GPU context id -1 for cpu and >1 for gpu
83+
- source: camera int for webcam or path for video file
84+
"""
85+
face_model = prepare_model(ctx_id=ctx_id) # doctest: +SKIP
86+
capture = cv2.VideoCapture(source)
87+
88+
while True:
89+
ret, frame = capture.read()
90+
if not ret:
91+
break
92+
93+
faces = get_facial_data(face_model, frame)
94+
95+
for face in faces:
96+
bbox = [int(coord) for coord in face["bounding_box"]]
97+
cv2.rectangle(
98+
frame,
99+
(bbox[0], bbox[1]),
100+
(bbox[2], bbox[3]),
101+
color=(0, 255, 0),
102+
thickness=2,
103+
)
104+
105+
cv2.imshow("Face Analysis", frame)
106+
if cv2.waitKey(1) & 0xFF == ord("q"):
107+
break
108+
109+
capture.release()
110+
cv2.destroyAllWindows()
111+
112+
113+
if __name__ == "__main__":
114+
run_webcam_demo(ctx_id=1)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"fake-useragent>=1.5.1",
1515
"httpx>=0.28.1",
1616
"imageio>=2.36.1",
17+
"insightface>=0.7.3",
1718
"keras>=3.7",
1819
"lxml>=6",
1920
"matplotlib>=3.9.3",

0 commit comments

Comments
 (0)