1import cv2
2import insightface
3from insightface.app import FaceAnalysis
4
5app = FaceAnalysis(name="buffalo_l")
6app.prepare(ctx_id=0)
7
8source_img = cv2.imread("source.jpg")
9target_img = cv2.imread("target.jpg")
10if source_img is None:
11 raise FileNotFoundError("source.jpg not found")
12if target_img is None:
13 raise FileNotFoundError("target.jpg not found")
14
15source_faces = app.get(source_img)
16target_faces = app.get(target_img)
17if not source_faces or not target_faces:
18 raise RuntimeError("face detection failed")
19
20swapper = insightface.model_zoo.get_model("inswapper_128.onnx")
21result = swapper.get(target_img, target_faces[0], source_faces[0], paste_back=True)
22
23cv2.imwrite("swapped.jpg", result)
24print("saved swapped.jpg")