Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fastai
|
2 |
+
from fastai.vision import *
|
3 |
+
from fastai.utils.mem import *
|
4 |
+
from fastai.vision import open_image, load_learner, image, torch
|
5 |
+
import numpy as np
|
6 |
+
import urllib.request
|
7 |
+
import PIL.Image
|
8 |
+
from io import BytesIO
|
9 |
+
import torchvision.transforms as T
|
10 |
+
from PIL import Image
|
11 |
+
import requests
|
12 |
+
from io import BytesIO
|
13 |
+
import fastai
|
14 |
+
from fastai.vision import *
|
15 |
+
from fastai.utils.mem import *
|
16 |
+
from fastai.vision import open_image, load_learner, image, torch
|
17 |
+
import numpy as np
|
18 |
+
import urllib.request
|
19 |
+
import PIL.Image
|
20 |
+
from PIL import Image
|
21 |
+
from io import BytesIO
|
22 |
+
import torchvision.transforms as T
|
23 |
+
|
24 |
+
class FeatureLoss(nn.Module):
|
25 |
+
def __init__(self, m_feat, layer_ids, layer_wgts):
|
26 |
+
super().__init__()
|
27 |
+
self.m_feat = m_feat
|
28 |
+
self.loss_features = [self.m_feat[i] for i in layer_ids]
|
29 |
+
self.hooks = hook_outputs(self.loss_features, detach=False)
|
30 |
+
self.wgts = layer_wgts
|
31 |
+
self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
|
32 |
+
] + [f'gram_{i}' for i in range(len(layer_ids))]
|
33 |
+
|
34 |
+
def make_features(self, x, clone=False):
|
35 |
+
self.m_feat(x)
|
36 |
+
return [(o.clone() if clone else o) for o in self.hooks.stored]
|
37 |
+
|
38 |
+
def forward(self, input, target):
|
39 |
+
out_feat = self.make_features(target, clone=True)
|
40 |
+
in_feat = self.make_features(input)
|
41 |
+
self.feat_losses = [base_loss(input,target)]
|
42 |
+
self.feat_losses += [base_loss(f_in, f_out)*w
|
43 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
44 |
+
self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out))*w**2 * 5e3
|
45 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
46 |
+
self.metrics = dict(zip(self.metric_names, self.feat_losses))
|
47 |
+
return sum(self.feat_losses)
|
48 |
+
|
49 |
+
def __del__(self): self.hooks.remove()
|
50 |
+
|
51 |
+
def add_margin(pil_img, top, right, bottom, left, color):
|
52 |
+
width, height = pil_img.size
|
53 |
+
new_width = width + right + left
|
54 |
+
new_height = height + top + bottom
|
55 |
+
result = Image.new(pil_img.mode, (new_width, new_height), color)
|
56 |
+
result.paste(pil_img, (left, top))
|
57 |
+
return result
|
58 |
+
|
59 |
+
|
60 |
+
MODEL_URL = "https://www.dropbox.com/s/04suaimdpru76h3/ArtLine_920.pkl?dl=1 "
|
61 |
+
urllib.request.urlretrieve(MODEL_URL, "ArtLine_920.pkl")
|
62 |
+
path = Path(".")
|
63 |
+
print(os.listdir('.'))
|
64 |
+
learn=load_learner(path, 'ArtLine_920.pkl')
|
65 |
+
|
66 |
+
|
67 |
+
import gradio as gr
|
68 |
+
import cv2
|
69 |
+
|
70 |
+
|
71 |
+
def get_filename(prefix="sketch"):
|
72 |
+
from datetime import datetime
|
73 |
+
from pytz import timezone
|
74 |
+
return datetime.now(timezone('Asia/Seoul')).strftime('sketch__%Y-%m-%d %H:%M:%S.jpg')
|
75 |
+
|
76 |
+
def predict(img):
|
77 |
+
img = PIL.Image.fromarray(img)
|
78 |
+
img = add_margin(img, 250, 250, 250, 250, (255, 255, 255))
|
79 |
+
img = np.array(img)
|
80 |
+
|
81 |
+
h, w = img.shape[:-1]
|
82 |
+
cv2.imwrite("test.jpg", img)
|
83 |
+
img_test = open_image("test.jpg")
|
84 |
+
|
85 |
+
p,img_hr,b = learn.predict(img_test)
|
86 |
+
|
87 |
+
res = (img_hr / img_hr.max()).numpy()
|
88 |
+
res = res[0] # take only first channel as result
|
89 |
+
res = cv2.resize(res, (w,h))
|
90 |
+
|
91 |
+
output_file = get_filename()
|
92 |
+
|
93 |
+
cv2.imwrite(output_file, (res * 255).astype(np.uint8), [cv2.IMWRITE_JPEG_QUALITY, 50])
|
94 |
+
|
95 |
+
return res, output_file
|
96 |
+
|
97 |
+
gr.Interface(predict,
|
98 |
+
inputs="image",
|
99 |
+
outputs=[gr.Image(label="Sketch Image"), gr.File(label="Result File")],
|
100 |
+
title="Image-to-sketch",
|
101 |
+
description="Transfer any image into BW cartoon-style sketch!").launch(share=True)
|