LogicGoInfotechSpaces commited on
Commit
f6e4783
·
verified ·
1 Parent(s): e52549a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -2
app.py CHANGED
@@ -151,6 +151,37 @@ async def log_faceswap_hit(token: str, status: str = "success"):
151
  # --------------------- Face Swap Pipeline ---------------------
152
  swap_lock = threading.Lock()
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  def multi_face_swap(src_img, tgt_img):
155
  src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
156
  tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
@@ -348,8 +379,9 @@ def build_multi_faceswap_gradio():
348
 
349
  def process(src_img, tgt_img):
350
  try:
351
- result = multi_face_swap(src_img, tgt_img)
352
- return result, ""
 
353
  except Exception as e:
354
  return None, str(e)
355
 
 
151
  # --------------------- Face Swap Pipeline ---------------------
152
  swap_lock = threading.Lock()
153
 
154
+ def enhance_image_with_codeformer(rgb_img, temp_dir=None):
155
+ if temp_dir is None:
156
+ temp_dir = os.path.join(tempfile.gettempdir(), f"enhance_{uuid.uuid4().hex[:8]}")
157
+ os.makedirs(temp_dir, exist_ok=True)
158
+
159
+ input_path = os.path.join(temp_dir, "input.jpg")
160
+ cv2.imwrite(input_path, cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR))
161
+
162
+ python_cmd = sys.executable if sys.executable else "python3"
163
+ cmd = (
164
+ f"{python_cmd} {CODEFORMER_PATH} "
165
+ f"-w 0.7 "
166
+ f"--input_path {input_path} "
167
+ f"--output_path {temp_dir} "
168
+ f"--bg_upsampler realesrgan "
169
+ f"--face_upsample"
170
+ )
171
+
172
+ result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
173
+ if result.returncode != 0:
174
+ raise RuntimeError(result.stderr)
175
+
176
+ final_dir = os.path.join(temp_dir, "final_results")
177
+ files = [f for f in os.listdir(final_dir) if f.endswith(".png")]
178
+ if not files:
179
+ raise RuntimeError("No enhanced output")
180
+
181
+ final_path = os.path.join(final_dir, files[0])
182
+ enhanced = cv2.imread(final_path)
183
+ return cv2.cvtColor(enhanced, cv2.COLOR_BGR2RGB)
184
+
185
  def multi_face_swap(src_img, tgt_img):
186
  src_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
187
  tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
 
379
 
380
  def process(src_img, tgt_img):
381
  try:
382
+ swapped = multi_face_swap(src_img, tgt_img)
383
+ enhanced = enhance_image_with_codeformer(swapped)
384
+ return enhanced, ""
385
  except Exception as e:
386
  return None, str(e)
387