Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,33 @@
|
|
| 1 |
-
# -*- coding:UTF-8 -*-
|
| 2 |
-
# !/usr/bin/env python
|
| 3 |
-
import spaces
|
| 4 |
-
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
-
from roop.processors.frame.core import get_frame_processors_modules
|
| 13 |
-
from roop.utilities import normalize_output_path
|
| 14 |
-
import os
|
| 15 |
-
import random
|
| 16 |
-
from PIL import Image
|
| 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):
|
| 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"
|
| 30 |
-
|
| 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.")
|
| 50 |
|
| 51 |
-
output_path = os.path.join(session_dir, output_filename)
|
| 52 |
-
normalized_output_path = normalize_output_path(source_path, target_path, output_path)
|
| 53 |
-
|
| 54 |
-
frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
|
| 55 |
-
|
| 56 |
-
for frame_processor in get_frame_processors_modules(frame_processors):
|
| 57 |
-
if not frame_processor.pre_check():
|
| 58 |
-
print(f"Pre-check failed for {frame_processor}")
|
| 59 |
-
raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
|
| 60 |
-
|
| 61 |
-
roop.globals.source_path = source_path
|
| 62 |
-
roop.globals.target_path = target_path
|
| 63 |
-
roop.globals.output_path = normalized_output_path
|
| 64 |
-
roop.globals.frame_processors = frame_processors
|
| 65 |
-
roop.globals.headless = True
|
| 66 |
-
roop.globals.keep_fps = True
|
| 67 |
-
roop.globals.keep_audio = True
|
| 68 |
-
roop.globals.keep_frames = False
|
| 69 |
-
roop.globals.many_faces = False
|
| 70 |
-
roop.globals.video_encoder = "libx264"
|
| 71 |
-
roop.globals.video_quality = 18
|
| 72 |
-
roop.globals.execution_providers = decode_execution_providers(['cpu'])
|
| 73 |
-
roop.globals.reference_face_position = 0
|
| 74 |
-
roop.globals.similar_face_distance = 0.6
|
| 75 |
-
roop.globals.max_memory = 60
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
|
| 4 |
+
# Создаем клиента для API
|
| 5 |
+
client = Client("tuan2308/face-swap")
|
| 6 |
+
|
| 7 |
+
def swap_face_api(source_img, target_img, doFaceEnhancer):
|
| 8 |
+
try:
|
| 9 |
+
result = client.predict(
|
| 10 |
+
source_file=handle_file(source_img),
|
| 11 |
+
target_file=handle_file(target_img),
|
| 12 |
+
doFaceEnhancer=doFaceEnhancer,
|
| 13 |
+
api_name="/predict"
|
| 14 |
+
)
|
| 15 |
+
return result
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Ошибка при вызове API: {e}")
|
| 18 |
+
return None # Или какое-то изображение-заглушку
|
| 19 |
+
|
| 20 |
+
# Создаем интерфейс
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=swap_face_api,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.Image(type="filepath", label="Source Image"),
|
| 25 |
+
gr.Image(type="filepath", label="Target Image"),
|
| 26 |
+
gr.Checkbox(label="Face Enhancer?")
|
| 27 |
+
],
|
| 28 |
+
outputs=gr.Image(label="Output Image"),
|
| 29 |
+
title="Face Swap via API"
|
| 30 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|