victorisgeek commited on
Commit
2a67a31
1 Parent(s): 4e8017b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -5,7 +5,6 @@ import numpy as np
5
  import os
6
 
7
  UPLOAD_FOLDER = 'uploads'
8
- ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi'}
9
 
10
  detector = dlib.get_frontal_face_detector()
11
 
@@ -31,18 +30,23 @@ def swap_faces(img, face1, face2):
31
 
32
  return img
33
 
34
- def process_file(image):
35
- if image is None:
36
- return "Please upload an image."
37
 
38
- filename = os.path.join(UPLOAD_FOLDER, 'uploaded_image.jpg')
39
- cv2.imwrite(filename, image)
40
-
41
- faces, img = detect_faces(filename)
42
- if len(faces) < 2:
43
- return "Need at least two faces to swap."
44
 
45
- swapped_img = swap_faces(img, faces[0], faces[1])
 
 
 
 
 
 
46
 
47
  result_filename = os.path.join(UPLOAD_FOLDER, 'result_image.jpg')
48
  cv2.imwrite(result_filename, swapped_img)
@@ -53,10 +57,13 @@ if not os.path.exists(UPLOAD_FOLDER):
53
 
54
  iface = gr.Interface(
55
  fn=process_file,
56
- inputs=gr.Image(type="numpy", label="Upload an Image"), # Gradio handles the file upload
 
 
 
57
  outputs=gr.Image(type="numpy", label="Swapped Faces"),
58
  title="Face Swap",
59
- description="Upload an image with at least two faces, and this tool will swap the faces."
60
  )
61
 
62
  if __name__ == "__main__":
 
5
  import os
6
 
7
  UPLOAD_FOLDER = 'uploads'
 
8
 
9
  detector = dlib.get_frontal_face_detector()
10
 
 
30
 
31
  return img
32
 
33
+ def process_file(target_image, source_image):
34
+ if target_image is None or source_image is None:
35
+ return "Please upload both a target image and a source image."
36
 
37
+ target_filename = os.path.join(UPLOAD_FOLDER, 'target_image.jpg')
38
+ source_filename = os.path.join(UPLOAD_FOLDER, 'source_image.jpg')
39
+
40
+ cv2.imwrite(target_filename, target_image)
41
+ cv2.imwrite(source_filename, source_image)
 
42
 
43
+ faces_target, img_target = detect_faces(target_filename)
44
+ faces_source, img_source = detect_faces(source_filename)
45
+
46
+ if len(faces_target) < 1 or len(faces_source) < 1:
47
+ return "Both images must have at least one face."
48
+
49
+ swapped_img = swap_faces(img_target, faces_target[0], faces_source[0])
50
 
51
  result_filename = os.path.join(UPLOAD_FOLDER, 'result_image.jpg')
52
  cv2.imwrite(result_filename, swapped_img)
 
57
 
58
  iface = gr.Interface(
59
  fn=process_file,
60
+ inputs=[
61
+ gr.Image(type="numpy", label="Upload Target Image"), # Target image where the face will be swapped
62
+ gr.Image(type="numpy", label="Upload Source Face") # Source image to extract the face from
63
+ ],
64
  outputs=gr.Image(type="numpy", label="Swapped Faces"),
65
  title="Face Swap",
66
+ description="Upload a target image and a source face image, and this tool will swap the face from the source onto the target."
67
  )
68
 
69
  if __name__ == "__main__":