Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788# -*- coding:UTF-8 -*-# !/usr/bin/env pythonimport spacesimport numpy as npimport gradio as grimport gradio.exceptionsimport roop.globalsfrom roop.core import (start,decode_execution_providers,)from roop.processors.frame.core import get_frame_processors_modulesfrom roop.utilities import normalize_output_pathimport osfrom PIL import Imageimport uuidimport onnxruntime as ortimport cv2from roop.face_analyser import get_one_face@spaces.GPUdef swap_face(source_file, target_file, doFaceEnhancer):session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việcsession_dir = f"temp/{session_id}"os.makedirs(session_dir, exist_ok=True)source_path = os.path.join(session_dir, "input.jpg")target_path = os.path.join(session_dir, "target.jpg")source_image = Image.fromarray(source_file)source_image.save(source_path)target_image = Image.fromarray(target_file)target_image.save(target_path)print("source_path: ", source_path)print("target_path: ", target_path)# Check if a face is detected in the source imagesource_face = get_one_face(cv2.imread(source_path))if source_face is None:raise gradio.exceptions.Error("No face in source path detected.")# Check if a face is detected in the target imagetarget_face = get_one_face(cv2.imread(target_path))if target_face is None:raise gradio.exceptions.Error("No face in target path detected.")output_path = os.path.join(session_dir, "output.jpg")normalized_output_path = normalize_output_path(source_path, target_path, output_path)frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]for frame_processor in get_frame_processors_modules(frame_processors):if not frame_processor.pre_check():print(f"Pre-check failed for {frame_processor}")raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")roop.globals.source_path = source_pathroop.globals.target_path = target_pathroop.globals.output_path = normalized_output_pathroop.globals.frame_processors = frame_processorsroop.globals.headless = Trueroop.globals.keep_fps = Trueroop.globals.keep_audio = Trueroop.globals.keep_frames = Falseroop.globals.many_faces = Falseroop.globals.video_encoder = "libx264"roop.globals.video_quality = 18roop.globals.execution_providers = ["CUDAExecutionProvider"]roop.globals.reference_face_position = 0roop.globals.similar_face_distance = 0.6roop.globals.max_memory = 60roop.globals.execution_threads = 50start()return normalized_output_pathapp = gr.Interface(fn=swap_face,inputs=[gr.Image(),gr.Image(),gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")],outputs="image")app.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788# -*- coding:UTF-8 -*-# !/usr/bin/env pythonimport spacesimport numpy as npimport gradio as grimport gradio.exceptionsimport roop.globalsfrom roop.core import (start,decode_execution_providers,)from roop.processors.frame.core import get_frame_processors_modulesfrom roop.utilities import normalize_output_pathimport osfrom PIL import Imageimport uuidimport onnxruntime as ortimport cv2from roop.face_analyser import get_one_face@spaces.GPU()def swap_face(source_file, target_file, doFaceEnhancer):session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việcsession_dir = f"temp/{session_id}"os.makedirs(session_dir, exist_ok=True)source_path = os.path.join(session_dir, "input.jpg")target_path = os.path.join(session_dir, "target.jpg")source_image = Image.fromarray(source_file)source_image.save(source_path)target_image = Image.fromarray(target_file)target_image.save(target_path)print("source_path: ", source_path)print("target_path: ", target_path)# Check if a face is detected in the source imagesource_face = get_one_face(cv2.imread(source_path))if source_face is None:raise gradio.exceptions.Error("No face in source path detected.")# Check if a face is detected in the target imagetarget_face = get_one_face(cv2.imread(target_path))if target_face is None:raise gradio.exceptions.Error("No face in target path detected.")output_path = os.path.join(session_dir, "output.jpg")normalized_output_path = normalize_output_path(source_path, target_path, output_path)frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]for frame_processor in get_frame_processors_modules(frame_processors):if not frame_processor.pre_check():print(f"Pre-check failed for {frame_processor}")raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")roop.globals.source_path = source_pathroop.globals.target_path = target_pathroop.globals.output_path = normalized_output_pathroop.globals.frame_processors = frame_processorsroop.globals.headless = Trueroop.globals.keep_fps = Trueroop.globals.keep_audio = Trueroop.globals.keep_frames = Falseroop.globals.many_faces = Falseroop.globals.video_encoder = "libx264"roop.globals.video_quality = 18roop.globals.execution_providers = ["CUDAExecutionProvider"]roop.globals.reference_face_position = 0roop.globals.similar_face_distance = 0.6roop.globals.max_memory = 60roop.globals.execution_threads = 50start()return normalized_output_pathapp = gr.Interface(fn=swap_face,inputs=[gr.Image(),gr.Image(),gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")],outputs="image")app.launch()
app.py+87-87