Spaces:
Running
Running
File size: 3,160 Bytes
c59c099 88d76b5 c59c099 88d76b5 c59c099 2576604 7ee87ef c59c099 2576604 c59c099 2576604 c59c099 7ee87ef c59c099 7ee87ef c59c099 ea735df 722a587 ea735df 00ea62f c59c099 ea735df |
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 |
import insightface
import os
import onnxruntime
import cv2
import gfpgan
import tempfile
import time
import gradio as gr
class Predictor:
def __init__(self):
self.setup()
def setup(self):
os.makedirs('models', exist_ok=True)
os.chdir('models')
if not os.path.exists('GFPGANv1.4.pth'):
os.system(
'wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth'
)
if not os.path.exists('inswapper_128.onnx'):
os.system(
'wget https://huggingface.co/ashleykleynhans/inswapper/resolve/main/inswapper_128.onnx'
)
os.chdir('..')
"""💎 Load the model into memory to make running multiple predictions efficient"""
self.face_swapper = insightface.model_zoo.get_model('models/inswapper_128.onnx',
providers=onnxruntime.get_available_providers())
self.face_enhancer = gfpgan.GFPGANer(model_path='models/GFPGANv1.4.pth', upscale=1)
self.face_analyser = insightface.app.FaceAnalysis(name='buffalo_l')
self.face_analyser.prepare(ctx_id=0, det_size=(640, 640))
def get_face(self, img_data):
analysed = self.face_analyser.get(img_data)
try:
largest = max(analysed, key=lambda x: (x.bbox[2] - x.bbox[0]) * (x.bbox[3] - x.bbox[1]))
return largest
except:
print("❌ No face found")
return None
def predict(self, input_image_path, swap_image_path):
"""🧶 Run a single prediction on the model"""
try:
frame = cv2.imread(input_image_path)
face = self.get_face(frame)
source_face = self.get_face(cv2.imread(swap_image_path))
try:
print(frame.shape, face.shape, source_face.shape)
except:
print("⚙️ printing shapes failed.")
result = self.face_swapper.get(frame, face, source_face, paste_back=True)
_, _, result = self.face_enhancer.enhance(
result,
paste_back=True
)
out_path = tempfile.mkdtemp() + f"/{str(int(time.time()))}.jpg"
cv2.imwrite(out_path, result)
return out_path
except Exception as e:
print(f"{e}")
return None
# Instantiate the Predictor class
predictor = Predictor()
title = "🧸 Auto adjust Models 🧸"
# Create Gradio Interface with the specified theme
with gr.Blocks(theme='victorisgeek/gray') as demo:
with gr.Row(equal_height=True):
with gr.Column(scale=10):
iface = gr.Interface(
fn=predictor.predict,
inputs=[
gr.Image(type="filepath", label="Target Image"),
gr.Image(type="filepath", label="Swap Image")
],
outputs=gr.Image(type="filepath", label="Result"),
title=title,
examples=[["input.jpg", "swap img.jpg"]]
)
iface.render()
# Launch the Gradio Interface
demo.launch()
|