Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_keras
|
2 |
+
import gradio as gr
|
3 |
+
from skimage.io import imsave
|
4 |
+
from skimage.color import lab2rgb
|
5 |
+
from tensorflow.keras.utils import img_to_array, load_img
|
6 |
+
|
7 |
+
repo_id="saruizn/trabajo"
|
8 |
+
|
9 |
+
model = from_pretrained_keras(repo_id)
|
10 |
+
|
11 |
+
def predict(img):
|
12 |
+
partes=[]
|
13 |
+
imsave("input.jpg",img)
|
14 |
+
imag = img_to_array(load_img("input.jpg"))
|
15 |
+
partes=divideImage(imag)
|
16 |
+
i=0
|
17 |
+
imagenes=[]
|
18 |
+
for imgp in partes:
|
19 |
+
nombre="result"+str(i)+".png"
|
20 |
+
imsave(nombre,imgp)
|
21 |
+
guardarImagen(nombre,i,imagenes)
|
22 |
+
i=i+1
|
23 |
+
imagenFinal=juntaImage(imagenes)
|
24 |
+
return imagenFinal
|
25 |
+
|
26 |
+
def guardarImagen(foto,i,imagenes):
|
27 |
+
color_me = []
|
28 |
+
img = img_to_array(load_img(foto))
|
29 |
+
img = resize(img ,(256,256))
|
30 |
+
color_me.append(img)
|
31 |
+
color_me = np.array(color_me, dtype=float)
|
32 |
+
color_me = rgb2lab(1.0/255*color_me)[:,:,:,0]
|
33 |
+
color_me = color_me.reshape(color_me.shape+(1,))
|
34 |
+
|
35 |
+
output = model.predict(color_me)
|
36 |
+
output = output * 128 * 8
|
37 |
+
|
38 |
+
|
39 |
+
result = np.zeros((256, 256, 3))
|
40 |
+
result[:,:,0] = color_me[0][:,:,0]
|
41 |
+
result[:,:,1:] = output[0]
|
42 |
+
imagenes.append(lab2rgb(result))
|
43 |
+
imsave("result"+str(i)+".png", lab2rgb(result))
|
44 |
+
|
45 |
+
def divideImage(img):
|
46 |
+
salida=[]
|
47 |
+
for i in range(0,3):
|
48 |
+
for j in range(0,5):
|
49 |
+
salida.append(img[i*256:i*256+256,j*256:j*256+256])
|
50 |
+
salida.append(img[i*256:i*256+256,1372-256:1372])
|
51 |
+
for j in range(0,5):
|
52 |
+
salida.append(img[929-256:929,j*256:j*256+256])
|
53 |
+
salida.append(img[929-256:929,1372-256:1372])
|
54 |
+
return salida
|
55 |
+
|
56 |
+
def juntaImage(X):
|
57 |
+
salida=np.zeros((929, 1372, 3))
|
58 |
+
im=0
|
59 |
+
for i in range(0,3):
|
60 |
+
for j in range(0,5):
|
61 |
+
salida[i*256:i*256+256,j*256:j*256+256]=X[im]
|
62 |
+
im=im+1
|
63 |
+
salida[i*256:i*256+256,1372-256:1372]=X[im]
|
64 |
+
im=im+1
|
65 |
+
for j in range(0,5):
|
66 |
+
salida[929-256:929,j*256:j*256+256]=X[im]
|
67 |
+
im=im+1
|
68 |
+
salida[929-256:929,1372-256:1372]=X[im]
|
69 |
+
return salida
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(929, 1372)), outputs=gr.outputs.Image(shape=(929, 1372))).launch(share=False)
|