Spaces:
Build error
Build error
add: rest of the files.
Browse files- README.md +1 -1
- app.py +54 -0
- cartoonizer.onnx +3 -0
- mountain.jpeg +0 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Cartoonizer Demo Onnx
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Cartoonizer Demo Onnx
|
| 3 |
+
emoji: 🗻
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import onnxruntime as ort
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
_sess_options = ort.SessionOptions()
|
| 10 |
+
_sess_options.intra_op_num_threads = os.cpu_count()
|
| 11 |
+
MODEL_SESS = ort.InferenceSession(
|
| 12 |
+
"cartoonizer.onnx", _sess_options, providers=["CPUExecutionProvider"]
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def preprocess_image(image: Image) -> np.ndarray:
|
| 17 |
+
image = np.array(image)
|
| 18 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 19 |
+
|
| 20 |
+
h, w, c = np.shape(image)
|
| 21 |
+
if min(h, w) > 720:
|
| 22 |
+
if h > w:
|
| 23 |
+
h, w = int(720 * h / w), 720
|
| 24 |
+
else:
|
| 25 |
+
h, w = 720, int(720 * w / h)
|
| 26 |
+
image = cv2.resize(image, (w, h), interpolation=cv2.INTER_AREA)
|
| 27 |
+
h, w = (h // 8) * 8, (w // 8) * 8
|
| 28 |
+
image = image[:h, :w, :]
|
| 29 |
+
image = image.astype(np.float32) / 127.5 - 1
|
| 30 |
+
return np.expand_dims(image, axis=0)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def inference(image: np.ndarray) -> Image:
|
| 34 |
+
image = preprocess_image(image)
|
| 35 |
+
results = MODEL_SESS.run(None, {"input_photo:0": image})
|
| 36 |
+
output = (np.squeeze(results[0]) + 1.0) * 127.5
|
| 37 |
+
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 38 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 39 |
+
return Image.fromarray(output)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
title = "Generate cartoonized images"
|
| 43 |
+
article = "Demo of CartoonGAN model (https://systemerrorwang.github.io/White-box-Cartoonization/). \nDemo image is from https://unsplash.com/photos/f0SgAs27BYI."
|
| 44 |
+
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
inference,
|
| 47 |
+
inputs=gr.inputs.Image(type="pil", label="Input Image"),
|
| 48 |
+
outputs="image",
|
| 49 |
+
title=title,
|
| 50 |
+
article=article,
|
| 51 |
+
allow_flagging="never",
|
| 52 |
+
examples=[["mountain.jpeg"]],
|
| 53 |
+
)
|
| 54 |
+
iface.launch()
|
cartoonizer.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c035adb7ce7590680fca3d82941ed5d1f6bcb25a6f61ac3fea06dcfb656c325a
|
| 3 |
+
size 5886605
|
mountain.jpeg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
onnxruntime
|
| 2 |
+
numpy
|
| 3 |
+
opencv-python
|