Spaces:
Running
Running
import gradio as gr | |
import cv2 | |
import insightface | |
from insightface.app import FaceAnalysis | |
def predict(image_in_video, image_in_img): | |
if image_in_video == None and image_in_img == None: | |
raise gr.Error("Please capture an image using the webcam or upload an image.") | |
image = image_in_video or image_in_img | |
return swapi(image) | |
app = FaceAnalysis(name='buffalo_l') | |
app.prepare(ctx_id=0, det_size=(640, 640)) | |
swapper = insightface.model_zoo.get_model('/content/drive/MyDrive/inswapper_128.onnx', download='FALSE', download_zip= 'FALSE') | |
def swapi(imagen): | |
img = cv2.cvtColor(np.array(imagen), cv2.COLOR_RGB2BGR) # Convert image from RGB to BGR format | |
faces = app.get(img) | |
if not faces: | |
return img # If no faces are detected, return the original image | |
source_face = faces[0] | |
bbox = source_face['bbox'] | |
bbox = [int(b) for b in bbox] | |
res = img.copy() | |
for face in faces: | |
res = swapper.get(res, face, source_face, paste_back=True) | |
return res[:, :, [2, 1, 0]] # Convert BGR to RGB for Gradio display | |
with gr.Blocks() as blocks: | |
gr.Markdown("### Capture Image Using WebCam or Upload") | |
with gr.Row(): | |
with gr.Column(): | |
image_or_file_opt = gr.Radio(["webcam", "file"], value="webcam", | |
label="How would you like to upload your image?") | |
image_in_video = gr.Image(source="webcam", type="filepath") | |
image_in_img = gr.Image(source="upload", visible=False, type="filepath") | |
# Update visibility based on selection | |
def toggle(choice): | |
if choice == "webcam": | |
return gr.update(visible=True, value=None), gr.update(visible=False, value=None) | |
else: | |
return gr.update(visible=False, value=None), gr.update(visible=True, value=None) | |
image_or_file_opt.change(fn=toggle, inputs=[image_or_file_opt], | |
outputs=[image_in_video, image_in_img], queue=False, show_progress=False) | |
with gr.Column(): | |
image_out = gr.Image() | |
run_btn = gr.Button("Run") | |
run_btn.click(fn=predict, inputs=[image_in_img, image_in_video], outputs=[image_out]) | |
gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out]) | |
blocks.queue() | |
blocks.launch() |