Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import torch
|
| 4 |
+
import cv2
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Download sample images
|
| 9 |
+
os.makedirs("sample_images", exist_ok=True)
|
| 10 |
+
os.chdir("sample_images")
|
| 11 |
+
os.system("gdown 1LiKfKhksfu8f_A4x-BhOjZgZHADdLdWz -O img1.jpg")
|
| 12 |
+
os.system("gdown 1jJ6uP_bpdnCRqCuiqZjJG8yeexsz6IUp -O img2.jpg")
|
| 13 |
+
os.system("gdown 1usp9c4VcTX2yche5o2WCi7yJw85n_yeZ -O img3.jpg")
|
| 14 |
+
|
| 15 |
+
os.chdir("../")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
examples = [['sample_images/img1.jpg', "Cartoonify"],
|
| 19 |
+
['sample_images/img2.jpg', "Cartoonify"],
|
| 20 |
+
['sample_images/img3.jpg', "Cartoonify"],
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
inference_on = ['Full Resolution Image', 'Downsampled Image']
|
| 24 |
+
|
| 25 |
+
title = "Face Cartoonify for Video Call Privacy"
|
| 26 |
+
description = """
|
| 27 |
+
Gradio demo for <b>Face Cartoonify for Video Call Privacy</b>.\n
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
article = "<p style='text-align: center'><a href='#'> Face Cartoonify for Video Call Privacy </a> | <a href='#'>Github Repo</a></p>"
|
| 31 |
+
|
| 32 |
+
model = torch.hub.load(
|
| 33 |
+
"AK391/animegan2-pytorch:main",
|
| 34 |
+
"generator",
|
| 35 |
+
pretrained=True,
|
| 36 |
+
device="cpu",
|
| 37 |
+
progress=False
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
face2paint = torch.hub.load(
|
| 41 |
+
'AK391/animegan2-pytorch:main', 'face2paint',
|
| 42 |
+
size=512, device="cpu",side_by_side=False
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
def inference(img, method):
|
| 46 |
+
if not os.path.exists('temp'):
|
| 47 |
+
os.system('mkdir temp')
|
| 48 |
+
|
| 49 |
+
img.save("temp/image.jpg", "JPEG")
|
| 50 |
+
|
| 51 |
+
if method == "Cartoonify AnimeGanV2":
|
| 52 |
+
out = face2paint(model, img)
|
| 53 |
+
return out
|
| 54 |
+
else:
|
| 55 |
+
os.system("python cartoon.py --input_path 'temp/image.jpg' --result_dir './temp/'")
|
| 56 |
+
return 'temp/image.jpg'
|
| 57 |
+
|
| 58 |
+
gr.Interface(
|
| 59 |
+
inference,
|
| 60 |
+
[
|
| 61 |
+
gr.inputs.Image(type="pil", label="Input"),
|
| 62 |
+
gr.inputs.Radio(["Cartoonify OpenCV", "Cartoonify AnimeGanV2"], default="Cartoonify AnimeGanV2", label='Method'),
|
| 63 |
+
|
| 64 |
+
],
|
| 65 |
+
gr.outputs.Image(type="file", label="Output"),
|
| 66 |
+
title=title,
|
| 67 |
+
description=description,
|
| 68 |
+
article=article,
|
| 69 |
+
theme ="huggingface",
|
| 70 |
+
examples=examples,
|
| 71 |
+
allow_flagging=False,
|
| 72 |
+
).launch(debug=True,enable_queue=True)
|