Rooni commited on
Commit
747205d
·
verified ·
1 Parent(s): 3266b60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -23,7 +23,7 @@ def swap_face(source_file, target_file, doFaceEnhancer):
23
  session_dir = "temp" # Sử dụng thư mục cố định
24
  os.makedirs(session_dir, exist_ok=True)
25
 
26
- # Создаем уникальные имена файлов
27
  source_filename = f"source_{random.randint(1000, 9999)}.jpg"
28
  target_filename = f"target_{random.randint(1000, 9999)}.jpg"
29
  output_filename = f"output_{random.randint(1000, 9999)}.jpg"
@@ -31,21 +31,19 @@ def swap_face(source_file, target_file, doFaceEnhancer):
31
  source_path = os.path.join(session_dir, source_filename)
32
  target_path = os.path.join(session_dir, target_filename)
33
 
34
- # Сохраняем изображения по указанным путям
35
- source_image = Image.open(source_file.name)
36
- source_image.save(source_path)
37
- target_image = Image.open(target_file.name)
38
- target_image.save(target_path)
39
 
40
  print("source_path: ", source_path)
41
  print("target_path: ", target_path)
42
 
43
- # Проверяем, обнаружена ли лицо на изображении-источнике
44
  source_face = get_one_face(cv2.imread(source_path))
45
  if source_face is None:
46
  raise gradio.exceptions.Error("No face in source path detected.")
47
 
48
- # Проверяем, обнаружена ли лицо на изображении-цели
49
  target_face = get_one_face(cv2.imread(target_path))
50
  if target_face is None:
51
  raise gradio.exceptions.Error("No face in target path detected.")
@@ -78,15 +76,18 @@ def swap_face(source_file, target_file, doFaceEnhancer):
78
  roop.globals.execution_threads = 8
79
 
80
  start()
81
- return normalized_output_path
 
 
 
82
 
83
  app = gr.Interface(
84
  fn=swap_face,
85
  inputs=[
86
- gr.Image(type="filepath"), # Изменено на "filepath"
87
- gr.Image(type="filepath"), # Изменено на "filepath"
88
  gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
89
  ],
90
  outputs="image"
91
  )
92
- app.launch()
 
23
  session_dir = "temp" # Sử dụng thư mục cố định
24
  os.makedirs(session_dir, exist_ok=True)
25
 
26
+ # Tạo tên file ngẫu nhiên
27
  source_filename = f"source_{random.randint(1000, 9999)}.jpg"
28
  target_filename = f"target_{random.randint(1000, 9999)}.jpg"
29
  output_filename = f"output_{random.randint(1000, 9999)}.jpg"
 
31
  source_path = os.path.join(session_dir, source_filename)
32
  target_path = os.path.join(session_dir, target_filename)
33
 
34
+ # Сохранение изображений
35
+ Image.fromarray(source_file).save(source_path)
36
+ Image.fromarray(target_file).save(target_path)
 
 
37
 
38
  print("source_path: ", source_path)
39
  print("target_path: ", target_path)
40
 
41
+ # Проверка наличия лица на изображении источника
42
  source_face = get_one_face(cv2.imread(source_path))
43
  if source_face is None:
44
  raise gradio.exceptions.Error("No face in source path detected.")
45
 
46
+ # Проверка наличия лица на изображении целевого изображения
47
  target_face = get_one_face(cv2.imread(target_path))
48
  if target_face is None:
49
  raise gradio.exceptions.Error("No face in target path detected.")
 
76
  roop.globals.execution_threads = 8
77
 
78
  start()
79
+
80
+ # Возвращаем изображение как массив байтов
81
+ with open(normalized_output_path, "rb") as image_file:
82
+ return image_file.read()
83
 
84
  app = gr.Interface(
85
  fn=swap_face,
86
  inputs=[
87
+ gr.Image(type="numpy"),
88
+ gr.Image(type="numpy"),
89
  gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
90
  ],
91
  outputs="image"
92
  )
93
+ app.launch()