Spaces:
Running
Running
File size: 4,015 Bytes
281e227 00c59b4 59b61b8 00c59b4 59b61b8 97885f2 00c59b4 59b61b8 a915ad8 59b61b8 00c59b4 59b61b8 959dfe6 00c59b4 59b61b8 959dfe6 59b61b8 959dfe6 59b61b8 959dfe6 00c59b4 59b61b8 97885f2 00c59b4 59b61b8 00c59b4 59b61b8 97885f2 59b61b8 97885f2 59b61b8 97885f2 59b61b8 97885f2 59b61b8 00c59b4 59b61b8 97885f2 59b61b8 97885f2 59b61b8 959dfe6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# Dependencies
import os
os.system("pip uninstall -y gradio")
os.system("pip install gradio==3.50")
os.system("pip uninstall -y numpy")
os.system("pip install numpy==1.23.5")
import gradio as gr
import cv2
import insightface
import numpy as np
import gradio.components as gr_comp
import asyncio
import logging
from PIL import Image
# Setup logging
logging.basicConfig(level=logging.INFO)
# Constants for DPI and pixel dimensions
DPI = 300
WIDTH_PIXELS = 1182
HEIGHT_PIXELS = 1773
# Load the insightface model once
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] if cv2.cuda.getCudaEnabledDeviceCount() > 0 else ["CPUExecutionProvider"]
model_path = 'inswapper_128.onnx'
model_swap_insightface = insightface.model_zoo.get_model(model_path, providers=providers)
# Prepare the FaceAnalysis model once
FACE_ANALYSER = insightface.app.FaceAnalysis(
name="buffalo_l",
root=".", providers=providers, allowed_modules=["landmark_3d_68", "landmark_2d_106", "detection", "recognition"]
)
FACE_ANALYSER.prepare(ctx_id=0 if cv2.cuda.getCudaEnabledDeviceCount() > 0 else -1, det_size=(640, 640))
# Function to update template choices based on gender
def update_templates(gender):
if gender == 'Male':
return gr.Dropdown.update(choices=['Boy Template 1.JPG', 'Boy Template 2.JPG', 'Boy Template 3.JPG'])
elif gender == 'Female':
return gr.Dropdown.update(choices=['Girl Template 1.JPG', 'Girl Template 2.JPG', 'Girl Template 3.JPG'])
else:
return gr.Dropdown.update(choices=[])
# Main function for face swapping
async def face_swap_and_merge(src_image, gender, template_choice):
try:
logging.info("Starting face swap and merge process")
# Resize the source image to the required dimensions for uniform processing
src_image = cv2.resize(src_image, (WIDTH_PIXELS, HEIGHT_PIXELS))
template_image_path = f'{template_choice}'
template_image = cv2.imread(template_image_path, cv2.IMREAD_UNCHANGED)
# Resize the template image to the required dimensions for uniform processing
template_image = cv2.resize(template_image, (WIDTH_PIXELS, HEIGHT_PIXELS))
src_faces = FACE_ANALYSER.get(src_image)
template_faces = FACE_ANALYSER.get(template_image)
img_fake = model_swap_insightface.get(img=template_image, target_face=template_faces[0], source_face=src_faces[0], paste_back=True)
img_fake_rgb = cv2.cvtColor(img_fake, cv2.COLOR_BGR2RGB)
# Convert to PIL image to set DPI
pil_image = Image.fromarray(img_fake_rgb)
pil_image.info['dpi'] = (DPI, DPI)
logging.info("Face swap and merge process completed successfully")
return np.array(pil_image)
except Exception as e:
logging.error(f"Error in face_swap_and_merge: {e}")
return None
# Function to clear only the output image
def clear_output():
return None
# Function to clear all inputs and outputs
def clear_all():
return None, None, None, None
# Create Gradio interface with title and theme
with gr.Blocks(theme='upsatwal/mlsc_tiet') as iface:
gr.Markdown("# Face Changer")
gr.Markdown(" ")
src_image = gr.Image(type="numpy", label="Input Image")
gender = gr.Dropdown(["Male", "Female"], label="Select Gender", interactive=True)
template_choice = gr.Dropdown([], label="Select Template", interactive=True)
gender.change(fn=update_templates, inputs=gender, outputs=template_choice)
result_image = gr.Image(label="Output Image")
submit_button = gr.Button("Submit")
submit_button.click(fn=face_swap_and_merge, inputs=[src_image, gender, template_choice], outputs=result_image)
clear_output_button = gr.Button("Clear Output")
clear_output_button.click(fn=clear_output, inputs=[], outputs=result_image)
clear_all_button = gr.Button("Clear All")
clear_all_button.click(fn=clear_all, inputs=[], outputs=[src_image, gender, template_choice, result_image])
iface.launch(debug=True)
|