tuan2308 commited on
Commit
205b8f5
1 Parent(s): 45d39d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -1,7 +1,8 @@
1
- # -* coding:UTF-8 -*
2
  # !/usr/bin/env python
3
  import numpy as np
4
  import gradio as gr
 
5
  import roop.globals
6
  from roop.core import (
7
  start,
@@ -14,6 +15,8 @@ from PIL import Image
14
  import spaces
15
  import uuid
16
  import onnxruntime as ort
 
 
17
 
18
  @spaces.GPU
19
  def swap_face(source_file, target_file, doFaceEnhancer):
@@ -32,6 +35,16 @@ def swap_face(source_file, target_file, doFaceEnhancer):
32
  print("source_path: ", source_path)
33
  print("target_path: ", target_path)
34
 
 
 
 
 
 
 
 
 
 
 
35
  output_path = os.path.join(session_dir, "output.jpg")
36
  normalized_output_path = normalize_output_path(source_path, target_path, output_path)
37
 
@@ -43,7 +56,7 @@ def swap_face(source_file, target_file, doFaceEnhancer):
43
  many_faces = False
44
  video_encoder = "libx264"
45
  video_quality = 18
46
- max_memory = "4G"
47
  execution_providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] # Ưu tiên GPU, sau đó là CPU
48
  execution_threads = 4
49
  reference_face_position = 0
@@ -55,7 +68,7 @@ def swap_face(source_file, target_file, doFaceEnhancer):
55
  for frame_processor in get_frame_processors_modules(frame_processors):
56
  if not frame_processor.pre_check():
57
  print(f"Pre-check failed for {frame_processor}")
58
- return
59
 
60
  roop.globals.source_path = source_path
61
  roop.globals.target_path = target_path
@@ -75,7 +88,7 @@ def swap_face(source_file, target_file, doFaceEnhancer):
75
  roop.globals.similar_face_distance = similar_face_distance
76
 
77
  start()
78
- return output_path
79
 
80
  app = gr.Interface(
81
  fn=swap_face,
 
1
+ # -*- coding:UTF-8 -*-
2
  # !/usr/bin/env python
3
  import numpy as np
4
  import gradio as gr
5
+ import gradio.exceptions
6
  import roop.globals
7
  from roop.core import (
8
  start,
 
15
  import spaces
16
  import uuid
17
  import onnxruntime as ort
18
+ import cv2
19
+ from roop.face_analyser import get_one_face
20
 
21
  @spaces.GPU
22
  def swap_face(source_file, target_file, doFaceEnhancer):
 
35
  print("source_path: ", source_path)
36
  print("target_path: ", target_path)
37
 
38
+ # Check if a face is detected in the source image
39
+ source_face = get_one_face(cv2.imread(source_path))
40
+ if source_face is None:
41
+ raise gradio.exceptions.Error("No face in source path detected.")
42
+
43
+ # Check if a face is detected in the target image
44
+ target_face = get_one_face(cv2.imread(target_path))
45
+ if target_face is None:
46
+ raise gradio.exceptions.Error("No face in target path detected.")
47
+
48
  output_path = os.path.join(session_dir, "output.jpg")
49
  normalized_output_path = normalize_output_path(source_path, target_path, output_path)
50
 
 
56
  many_faces = False
57
  video_encoder = "libx264"
58
  video_quality = 18
59
+ max_memory = "40G"
60
  execution_providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] # Ưu tiên GPU, sau đó là CPU
61
  execution_threads = 4
62
  reference_face_position = 0
 
68
  for frame_processor in get_frame_processors_modules(frame_processors):
69
  if not frame_processor.pre_check():
70
  print(f"Pre-check failed for {frame_processor}")
71
+ raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
72
 
73
  roop.globals.source_path = source_path
74
  roop.globals.target_path = target_path
 
88
  roop.globals.similar_face_distance = similar_face_distance
89
 
90
  start()
91
+ return normalized_output_path
92
 
93
  app = gr.Interface(
94
  fn=swap_face,