Spaces:
Running
Running
Commit
·
53b8c3a
1
Parent(s):
8aa6d79
encoder
Browse files- app.py +8 -1
- requirements.txt +1 -0
- utils.py +76 -0
app.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from utils import encoder, decoder
|
| 3 |
+
|
| 4 |
+
source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
|
| 5 |
+
encoded_img = gr.Image()
|
| 6 |
|
| 7 |
def greet(name):
|
| 8 |
return "Hello " + name + "!!"
|
| 9 |
|
| 10 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 11 |
+
iface = gr.Interface(fn=encoder, inputs=[source_img,
|
| 12 |
+
gr.Slider(label='noise', minimum = 0, maximum = 1, step = .05, value = .95)],
|
| 13 |
+
outputs=encoded_img)
|
| 14 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scipy
|
utils.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import scipy
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
VALUE = 512
|
| 6 |
+
|
| 7 |
+
def resize(value,img):
|
| 8 |
+
img = Image.open(img)
|
| 9 |
+
img = img.resize((value,value), Image.Resampling.LANCZOS)
|
| 10 |
+
return img
|
| 11 |
+
|
| 12 |
+
def get_mask(img,p):
|
| 13 |
+
w,h=img.size
|
| 14 |
+
return np.random.choice(a=[False, True], size=(w, h), p=[p, 1-p])
|
| 15 |
+
|
| 16 |
+
def generate_points(mask):
|
| 17 |
+
(w,h) = mask.shape
|
| 18 |
+
noise_points = []
|
| 19 |
+
color_points = []
|
| 20 |
+
for x in range(w):
|
| 21 |
+
for y in range(h):
|
| 22 |
+
if mask[x,y]:
|
| 23 |
+
color_points.append(np.array([x,y]))
|
| 24 |
+
else:
|
| 25 |
+
noise_points.append(np.array([x,y]))
|
| 26 |
+
return color_points, noise_points
|
| 27 |
+
|
| 28 |
+
def encoder_cp(img,color_points):
|
| 29 |
+
w,h=img.size
|
| 30 |
+
img2=Image.new('RGB',(w,h))
|
| 31 |
+
for p in color_points:
|
| 32 |
+
t = img.getpixel((p[0],p[1]))
|
| 33 |
+
img2.putpixel((p[0],p[1]),(t[0],t[1],t[2]))
|
| 34 |
+
return img2
|
| 35 |
+
|
| 36 |
+
def encoder(img,p):
|
| 37 |
+
img = resize(img,VALUE)
|
| 38 |
+
mask = get_mask(img,p)
|
| 39 |
+
c_p, n_p = generate_points(mask)
|
| 40 |
+
return encoder_cp(img, c_p)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_points(img):
|
| 44 |
+
w,h=img.size
|
| 45 |
+
noise_points = []
|
| 46 |
+
color_points = []
|
| 47 |
+
for x in range(w):
|
| 48 |
+
for y in range(h):
|
| 49 |
+
t = img.getpixel((x,y))
|
| 50 |
+
if np.sum(t[:3]) > 0 :
|
| 51 |
+
color_points.append(np.array([x,y]))
|
| 52 |
+
else:
|
| 53 |
+
noise_points.append(np.array([x,y]))
|
| 54 |
+
return color_points, noise_points
|
| 55 |
+
|
| 56 |
+
def restore(img, k, color_points, noise_points):
|
| 57 |
+
kdtree = scipy.spatial.KDTree(color_points)
|
| 58 |
+
for p in noise_points:
|
| 59 |
+
_, knn_p = kdtree.query(p, k)
|
| 60 |
+
r_m = []
|
| 61 |
+
v_m = []
|
| 62 |
+
b_m = []
|
| 63 |
+
for np in [color_points[j] for j in knn_p]:
|
| 64 |
+
t = img.getpixel((np[0],np[1]))
|
| 65 |
+
r_m.append(t[0])
|
| 66 |
+
v_m.append(t[1])
|
| 67 |
+
b_m.append(t[2])
|
| 68 |
+
r_m = int(sum(r_m)/k)
|
| 69 |
+
v_m = int(sum(v_m)/k)
|
| 70 |
+
b_m = int(sum(b_m)/k)
|
| 71 |
+
img.putpixel((p[0],p[1]),(r_m,v_m,b_m))
|
| 72 |
+
return img
|
| 73 |
+
|
| 74 |
+
def decoder(img,k):
|
| 75 |
+
c_p, n_p = get_points(img)
|
| 76 |
+
return restore(img,k,c_p,n_p)
|