zerovic commited on
Commit
9d694f4
·
verified ·
1 Parent(s): f69fddb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -13,7 +13,6 @@ class Predictor:
13
  self.setup()
14
 
15
  def setup(self):
16
- # Models are already in /app/models thanks to Dockerfile
17
  model_dir = "models"
18
 
19
  print("Loading models...")
@@ -31,13 +30,14 @@ class Predictor:
31
  self.face_analyser.prepare(ctx_id=0, det_size=(640, 640))
32
 
33
  def get_face(self, img_data):
 
34
  faces = self.face_analyser.get(img_data)
35
  if len(faces) == 0:
36
  return None
37
- # Return the largest face
38
  return max(faces, key=lambda x: (x.bbox[2] - x.bbox[0]) * (x.bbox[3] - x.bbox[1]))
39
 
40
  def predict(self, target_image, swap_image):
 
41
  try:
42
  frame = cv2.imread(target_image)
43
  source = cv2.imread(swap_image)
@@ -51,10 +51,10 @@ class Predictor:
51
  if target_face is None or source_face is None:
52
  return None, "❌ Could not detect a clear face in one or both images."
53
 
54
- # Face swap
55
  result = self.face_swapper.get(frame, target_face, source_face, paste_back=True)
56
 
57
- # Enhancement
58
  _, _, result = self.face_enhancer.enhance(result, paste_back=True)
59
 
60
  # Save result
@@ -68,45 +68,48 @@ class Predictor:
68
  return None, f"❌ Error: {str(e)}"
69
 
70
 
71
- # Load predictor once at startup
72
  predictor = Predictor()
73
 
74
- # Modern UI with examples
75
  with gr.Blocks(title="Swap Face Model", theme=gr.themes.Soft()) as demo:
76
  gr.Markdown("# 😻 Face Swap Model")
77
- gr.Markdown("Upload a target image and a face to swap in. Works best with clear, well-lit frontal faces.")
78
 
79
  with gr.Row():
80
  with gr.Column():
81
- target_input = gr.Image(type="filepath", label="Target Image", height=450)
 
 
 
 
82
  with gr.Column():
83
- swap_input = gr.Image(type="filepath", label="Swap Face Image", height=450)
 
 
 
 
84
 
85
  with gr.Row():
86
  swap_btn = gr.Button("🔄 Swap Faces", variant="primary", size="large")
87
 
88
  with gr.Row():
89
- output_image = gr.Image(type="filepath", label="Result", height=500)
 
 
 
 
90
 
91
  status = gr.Textbox(label="Status", interactive=False)
92
 
93
- # Examples (add your own sample images to the repo)
94
- gr.Examples(
95
- examples=[["examples/target1.jpg", "examples/swap1.jpg"],
96
- ["examples/target2.jpg", "examples/swap2.jpg"]],
97
- inputs=[target_input, swap_input],
98
- outputs=[output_image, status],
99
- fn=predictor.predict,
100
- cache_examples=False
101
- )
102
-
103
  swap_btn.click(
104
  fn=predictor.predict,
105
  inputs=[target_input, swap_input],
106
  outputs=[output_image, status]
107
  )
108
 
109
- gr.Markdown("**Tips:** Use high-resolution photos. Results are better with similar lighting and angles.")
110
 
111
  if __name__ == "__main__":
112
  demo.launch(
 
13
  self.setup()
14
 
15
  def setup(self):
 
16
  model_dir = "models"
17
 
18
  print("Loading models...")
 
30
  self.face_analyser.prepare(ctx_id=0, det_size=(640, 640))
31
 
32
  def get_face(self, img_data):
33
+ """Return the largest detected face"""
34
  faces = self.face_analyser.get(img_data)
35
  if len(faces) == 0:
36
  return None
 
37
  return max(faces, key=lambda x: (x.bbox[2] - x.bbox[0]) * (x.bbox[3] - x.bbox[1]))
38
 
39
  def predict(self, target_image, swap_image):
40
+ """Face swap function"""
41
  try:
42
  frame = cv2.imread(target_image)
43
  source = cv2.imread(swap_image)
 
51
  if target_face is None or source_face is None:
52
  return None, "❌ Could not detect a clear face in one or both images."
53
 
54
+ # Perform face swap
55
  result = self.face_swapper.get(frame, target_face, source_face, paste_back=True)
56
 
57
+ # Enhance with GFPGAN
58
  _, _, result = self.face_enhancer.enhance(result, paste_back=True)
59
 
60
  # Save result
 
68
  return None, f"❌ Error: {str(e)}"
69
 
70
 
71
+ # Load the predictor once at startup
72
  predictor = Predictor()
73
 
74
+ # Clean UI without examples
75
  with gr.Blocks(title="Swap Face Model", theme=gr.themes.Soft()) as demo:
76
  gr.Markdown("# 😻 Face Swap Model")
77
+ gr.Markdown("Upload a **target image** and a **face to swap in**. Works best with clear, well-lit frontal faces.")
78
 
79
  with gr.Row():
80
  with gr.Column():
81
+ target_input = gr.Image(
82
+ type="filepath",
83
+ label="Target Image (the photo you want to change)",
84
+ height=450
85
+ )
86
  with gr.Column():
87
+ swap_input = gr.Image(
88
+ type="filepath",
89
+ label="Swap Face Image (the face you want to use)",
90
+ height=450
91
+ )
92
 
93
  with gr.Row():
94
  swap_btn = gr.Button("🔄 Swap Faces", variant="primary", size="large")
95
 
96
  with gr.Row():
97
+ output_image = gr.Image(
98
+ type="filepath",
99
+ label="Result",
100
+ height=500
101
+ )
102
 
103
  status = gr.Textbox(label="Status", interactive=False)
104
 
105
+ # Button action
 
 
 
 
 
 
 
 
 
106
  swap_btn.click(
107
  fn=predictor.predict,
108
  inputs=[target_input, swap_input],
109
  outputs=[output_image, status]
110
  )
111
 
112
+ gr.Markdown("**Tips:**\n• Use high-resolution, well-lit photos\n• Frontal or near-frontal faces work best\n• Results improve with similar lighting and angles.")
113
 
114
  if __name__ == "__main__":
115
  demo.launch(