Dineth1222
commited on
Commit
•
a344818
1
Parent(s):
2dfd068
Ai
Browse files
Ai
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import dlib
|
3 |
+
|
4 |
+
def swap_faces(image_path1, image_path2, output_path):
|
5 |
+
# Load the images
|
6 |
+
img1 = cv2.imread(image_path1)
|
7 |
+
img2 = cv2.imread(image_path2)
|
8 |
+
|
9 |
+
# Initialize the face detector
|
10 |
+
detector = dlib.get_frontal_face_detector()
|
11 |
+
|
12 |
+
# Detect faces in both images
|
13 |
+
faces1 = detector(img1)
|
14 |
+
faces2 = detector(img2)
|
15 |
+
|
16 |
+
# Swap the faces
|
17 |
+
for face1, face2 in zip(faces1, faces2):
|
18 |
+
x1, y1, w1, h1 = face1.left(), face1.top(), face1.width(), face1.height()
|
19 |
+
x2, y2, w2, h2 = face2.left(), face2.top(), face2.width(), face2.height()
|
20 |
+
|
21 |
+
# Extract the face regions
|
22 |
+
face1_region = img1[y1:y1 + h1, x1:x1 + w1]
|
23 |
+
face2_region = img2[y2:y2 + h2, x2:x2 + w2]
|
24 |
+
|
25 |
+
# Swap the faces
|
26 |
+
img1[y1:y1 + h1, x1:x1 + w1] = face2_region
|
27 |
+
img2[y2:y2 + h2, x2:x2 + w2] = face1_region
|
28 |
+
|
29 |
+
# Save the output images
|
30 |
+
cv2.imwrite(output_path + "_1.jpg", img1)
|
31 |
+
cv2.imwrite(output_path + "_2.jpg", img2)
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
image1_path = "path/to/your/image1.jpg"
|
35 |
+
image2_path = "path/to/your/image2.jpg"
|
36 |
+
output_path = "path/to/output/directory/output"
|
37 |
+
|
38 |
+
swap_faces(image1_path, image2_path, output_path)
|