Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
·
e40c95c
1
Parent(s):
62d5329
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
from random import randint
|
| 7 |
+
import sys
|
| 8 |
+
from subprocess import call
|
| 9 |
+
|
| 10 |
+
torch.hub.download_url_to_file('https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth', 'experiments/pretrained_models/RealESRGAN_x4plus.pth')
|
| 11 |
+
|
| 12 |
+
def run_cmd(command):
|
| 13 |
+
try:
|
| 14 |
+
print(command)
|
| 15 |
+
call(command, shell=True)
|
| 16 |
+
except KeyboardInterrupt:
|
| 17 |
+
print("Process interrupted")
|
| 18 |
+
sys.exit(1)
|
| 19 |
+
|
| 20 |
+
run_cmd("python setup.py develop")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def inference(img):
|
| 24 |
+
_id = randint(1, 10000)
|
| 25 |
+
INPUT_DIR = "/tmp/input_image" + str(_id) + "/"
|
| 26 |
+
OUTPUT_DIR = "/tmp/output_image" + str(_id) + "/"
|
| 27 |
+
run_cmd("rm -rf " + INPUT_DIR)
|
| 28 |
+
run_cmd("rm -rf " + OUTPUT_DIR)
|
| 29 |
+
run_cmd("mkdir " + INPUT_DIR)
|
| 30 |
+
run_cmd("mkdir " + OUTPUT_DIR)
|
| 31 |
+
basewidth = 256
|
| 32 |
+
wpercent = (basewidth/float(img.size[0]))
|
| 33 |
+
hsize = int((float(img.size[1])*float(wpercent)))
|
| 34 |
+
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
|
| 35 |
+
img.save(INPUT_DIR + "1.jpg", "JPEG")
|
| 36 |
+
run_cmd("python inference_realesrgan.py --model_path experiments/pretrained_models/RealESRGAN_x4plus.pth --input "+ INPUT_DIR + " --output " + OUTPUT_DIR + " --netscale 4 --outscale 3.5")
|
| 37 |
+
return os.path.join(OUTPUT_DIR, "1_out.jpg")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
title = "Anime2Sketch"
|
| 41 |
+
description = "demo for Anime2Sketch. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
| 42 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.05703'>Adversarial Open Domain Adaption for Sketch-to-Photo Synthesis</a> | <a href='https://github.com/Mukosame/Anime2Sketch'>Github Repo</a></p>"
|
| 43 |
+
|
| 44 |
+
gr.Interface(
|
| 45 |
+
inference,
|
| 46 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
| 47 |
+
gr.outputs.Image(type="file", label="Output"),
|
| 48 |
+
title=title,
|
| 49 |
+
description=description,
|
| 50 |
+
article=article,
|
| 51 |
+
).launch(debug=True)
|