Spaces:
Runtime error
Runtime error
added new title
Browse files- .history/app_20231208153741.py +77 -0
- .history/app_20231208160008.py +77 -0
- .history/app_20231208160016.py +77 -0
- app.py +1 -1
.history/app_20231208153741.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from torchvision import transforms
|
4 |
+
import torch
|
5 |
+
import random
|
6 |
+
import os
|
7 |
+
from models.structure.model import SketchKeras
|
8 |
+
from safetensors.torch import load_model
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
|
13 |
+
path_to_weights = os.path.join(
|
14 |
+
os.path.dirname(__file__), "models/weights/sketch_keras.st"
|
15 |
+
)
|
16 |
+
model = SketchKeras()
|
17 |
+
load_model(model, path_to_weights)
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
|
21 |
+
def preprocess(img):
|
22 |
+
h, w, c = img.shape
|
23 |
+
blurred = cv2.GaussianBlur(img, (0, 0), 3)
|
24 |
+
highpass = img.astype(int) - blurred.astype(int)
|
25 |
+
highpass = highpass.astype(float) / 128.0
|
26 |
+
highpass /= np.max(highpass)
|
27 |
+
|
28 |
+
ret = np.zeros((512, 512, 3), dtype=float)
|
29 |
+
ret[0:h, 0:w, 0:c] = highpass
|
30 |
+
return ret
|
31 |
+
|
32 |
+
|
33 |
+
def postprocess(pred, thresh=0.18, smooth=False):
|
34 |
+
assert thresh <= 1.0 and thresh >= 0.0
|
35 |
+
|
36 |
+
pred = np.amax(pred, 0)
|
37 |
+
pred[pred < thresh] = 0
|
38 |
+
pred = 1 - pred
|
39 |
+
pred *= 255
|
40 |
+
pred = np.clip(pred, 0, 255).astype(np.uint8)
|
41 |
+
if smooth:
|
42 |
+
pred = cv2.medianBlur(pred, 3)
|
43 |
+
return pred
|
44 |
+
|
45 |
+
|
46 |
+
def output_sketch(img):
|
47 |
+
# resize
|
48 |
+
height, width = float(img.shape[0]), float(img.shape[1])
|
49 |
+
if width > height:
|
50 |
+
new_width, new_height = (512, int(512 / width * height))
|
51 |
+
else:
|
52 |
+
new_width, new_height = (int(512 / height * width), 512)
|
53 |
+
img = cv2.resize(img, (new_width, new_height))
|
54 |
+
|
55 |
+
img = preprocess(img)
|
56 |
+
x = img.reshape(1, *img.shape).transpose(3, 0, 1, 2)
|
57 |
+
x = torch.tensor(x).float()
|
58 |
+
|
59 |
+
with torch.no_grad():
|
60 |
+
pred = model(x)
|
61 |
+
|
62 |
+
pred = pred.squeeze()
|
63 |
+
|
64 |
+
# postprocess
|
65 |
+
output = pred.cpu().detach().numpy()
|
66 |
+
output = postprocess(output, thresh=0.1, smooth=False)
|
67 |
+
output = output[:new_height, :new_width]
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
71 |
+
|
72 |
+
gr.Interface(
|
73 |
+
title="Turn Any Image Into a Sketch",
|
74 |
+
fn=output_sketch,
|
75 |
+
inputs=gr.Image(type="numpy"),
|
76 |
+
outputs=gr.Image(type="numpy"),
|
77 |
+
).launch()
|
.history/app_20231208160008.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from torchvision import transforms
|
4 |
+
import torch
|
5 |
+
import random
|
6 |
+
import os
|
7 |
+
from models.structure.model import SketchKeras
|
8 |
+
from safetensors.torch import load_model
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
|
13 |
+
path_to_weights = os.path.join(
|
14 |
+
os.path.dirname(__file__), "models/weights/sketch_keras.st"
|
15 |
+
)
|
16 |
+
model = SketchKeras()
|
17 |
+
load_model(model, path_to_weights)
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
|
21 |
+
def preprocess(img):
|
22 |
+
h, w, c = img.shape
|
23 |
+
blurred = cv2.GaussianBlur(img, (0, 0), 3)
|
24 |
+
highpass = img.astype(int) - blurred.astype(int)
|
25 |
+
highpass = highpass.astype(float) / 128.0
|
26 |
+
highpass /= np.max(highpass)
|
27 |
+
|
28 |
+
ret = np.zeros((512, 512, 3), dtype=float)
|
29 |
+
ret[0:h, 0:w, 0:c] = highpass
|
30 |
+
return ret
|
31 |
+
|
32 |
+
|
33 |
+
def postprocess(pred, thresh=0.18, smooth=False):
|
34 |
+
assert thresh <= 1.0 and thresh >= 0.0
|
35 |
+
|
36 |
+
pred = np.amax(pred, 0)
|
37 |
+
pred[pred < thresh] = 0
|
38 |
+
pred = 1 - pred
|
39 |
+
pred *= 255
|
40 |
+
pred = np.clip(pred, 0, 255).astype(np.uint8)
|
41 |
+
if smooth:
|
42 |
+
pred = cv2.medianBlur(pred, 3)
|
43 |
+
return pred
|
44 |
+
|
45 |
+
|
46 |
+
def output_sketch(img):
|
47 |
+
# resize
|
48 |
+
height, width = float(img.shape[0]), float(img.shape[1])
|
49 |
+
if width > height:
|
50 |
+
new_width, new_height = (512, int(512 / width * height))
|
51 |
+
else:
|
52 |
+
new_width, new_height = (int(512 / height * width), 512)
|
53 |
+
img = cv2.resize(img, (new_width, new_height))
|
54 |
+
|
55 |
+
img = preprocess(img)
|
56 |
+
x = img.reshape(1, *img.shape).transpose(3, 0, 1, 2)
|
57 |
+
x = torch.tensor(x).float()
|
58 |
+
|
59 |
+
with torch.no_grad():
|
60 |
+
pred = model(x)
|
61 |
+
|
62 |
+
pred = pred.squeeze()
|
63 |
+
|
64 |
+
# postprocess
|
65 |
+
output = pred.cpu().detach().numpy()
|
66 |
+
output = postprocess(output, thresh=0.1, smooth=False)
|
67 |
+
output = output[:new_height, :new_width]
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
71 |
+
|
72 |
+
gr.Interface(
|
73 |
+
title="Turn Any Image Into a Sketch with SketchKeras",
|
74 |
+
fn=output_sketch,
|
75 |
+
inputs=gr.Image(type="numpy"),
|
76 |
+
outputs=gr.Image(type="numpy"),
|
77 |
+
).launch()
|
.history/app_20231208160016.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from torchvision import transforms
|
4 |
+
import torch
|
5 |
+
import random
|
6 |
+
import os
|
7 |
+
from models.structure.model import SketchKeras
|
8 |
+
from safetensors.torch import load_model
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
|
13 |
+
path_to_weights = os.path.join(
|
14 |
+
os.path.dirname(__file__), "models/weights/sketch_keras.st"
|
15 |
+
)
|
16 |
+
model = SketchKeras()
|
17 |
+
load_model(model, path_to_weights)
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
|
21 |
+
def preprocess(img):
|
22 |
+
h, w, c = img.shape
|
23 |
+
blurred = cv2.GaussianBlur(img, (0, 0), 3)
|
24 |
+
highpass = img.astype(int) - blurred.astype(int)
|
25 |
+
highpass = highpass.astype(float) / 128.0
|
26 |
+
highpass /= np.max(highpass)
|
27 |
+
|
28 |
+
ret = np.zeros((512, 512, 3), dtype=float)
|
29 |
+
ret[0:h, 0:w, 0:c] = highpass
|
30 |
+
return ret
|
31 |
+
|
32 |
+
|
33 |
+
def postprocess(pred, thresh=0.18, smooth=False):
|
34 |
+
assert thresh <= 1.0 and thresh >= 0.0
|
35 |
+
|
36 |
+
pred = np.amax(pred, 0)
|
37 |
+
pred[pred < thresh] = 0
|
38 |
+
pred = 1 - pred
|
39 |
+
pred *= 255
|
40 |
+
pred = np.clip(pred, 0, 255).astype(np.uint8)
|
41 |
+
if smooth:
|
42 |
+
pred = cv2.medianBlur(pred, 3)
|
43 |
+
return pred
|
44 |
+
|
45 |
+
|
46 |
+
def output_sketch(img):
|
47 |
+
# resize
|
48 |
+
height, width = float(img.shape[0]), float(img.shape[1])
|
49 |
+
if width > height:
|
50 |
+
new_width, new_height = (512, int(512 / width * height))
|
51 |
+
else:
|
52 |
+
new_width, new_height = (int(512 / height * width), 512)
|
53 |
+
img = cv2.resize(img, (new_width, new_height))
|
54 |
+
|
55 |
+
img = preprocess(img)
|
56 |
+
x = img.reshape(1, *img.shape).transpose(3, 0, 1, 2)
|
57 |
+
x = torch.tensor(x).float()
|
58 |
+
|
59 |
+
with torch.no_grad():
|
60 |
+
pred = model(x)
|
61 |
+
|
62 |
+
pred = pred.squeeze()
|
63 |
+
|
64 |
+
# postprocess
|
65 |
+
output = pred.cpu().detach().numpy()
|
66 |
+
output = postprocess(output, thresh=0.1, smooth=False)
|
67 |
+
output = output[:new_height, :new_width]
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
71 |
+
|
72 |
+
gr.Interface(
|
73 |
+
title="Turn Any Image Into a Sketch with This App",
|
74 |
+
fn=output_sketch,
|
75 |
+
inputs=gr.Image(type="numpy"),
|
76 |
+
outputs=gr.Image(type="numpy"),
|
77 |
+
).launch()
|
app.py
CHANGED
@@ -70,7 +70,7 @@ def output_sketch(img):
|
|
70 |
|
71 |
|
72 |
gr.Interface(
|
73 |
-
title="Turn Any Image Into a Sketch",
|
74 |
fn=output_sketch,
|
75 |
inputs=gr.Image(type="numpy"),
|
76 |
outputs=gr.Image(type="numpy"),
|
|
|
70 |
|
71 |
|
72 |
gr.Interface(
|
73 |
+
title="Turn Any Image Into a Sketch with This App",
|
74 |
fn=output_sketch,
|
75 |
inputs=gr.Image(type="numpy"),
|
76 |
outputs=gr.Image(type="numpy"),
|