Spaces:
Runtime error
Runtime error
alexrods
commited on
Commit
•
05e606d
1
Parent(s):
238b730
add st interface
Browse files
app.py
CHANGED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
from huggingface_hub import from_pretrained_keras
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
st.header("Segmentacion de partes del cuerpo humano")
|
9 |
+
|
10 |
+
st.markdown("Sube una imagen o selecciona un ejemplo para segmentar las distintas partes del cuerpo humano")
|
11 |
+
|
12 |
+
file_imagen = st.file_uploader("Sube aqui tu imagen", type=["png", "jpg", "jpeg"])
|
13 |
+
|
14 |
+
model = from_pretrained_keras("keras-io/deeplabv3p-resnet50")
|
15 |
+
|
16 |
+
colormap = np.array([[0,0,0], [31,119,180], [44,160,44], [44, 127, 125], [52, 225, 143],
|
17 |
+
[217, 222, 163], [254, 128, 37], [130, 162, 128], [121, 7, 166], [136, 183, 248],
|
18 |
+
[85, 1, 76], [22, 23, 62], [159, 50, 15], [101, 93, 152], [252, 229, 92],
|
19 |
+
[167, 173, 17], [218, 252, 252], [238, 126, 197], [116, 157, 140], [214, 220, 252]], dtype=np.uint8)
|
20 |
+
|
21 |
+
img_size = 512
|
22 |
+
|
23 |
+
def read_image(image):
|
24 |
+
image = tf.convert_to_tensor(image)
|
25 |
+
image.set_shape([None, None, 3])
|
26 |
+
image = tf.image.resize(images=image, size=[img_size, img_size])
|
27 |
+
image = image / 127.5 - 1
|
28 |
+
return image
|
29 |
+
|
30 |
+
def infer(model, image_tensor):
|
31 |
+
predictions = model.predict(np.expand_dims((image_tensor), axis=0))
|
32 |
+
predictions = np.squeeze(predictions)
|
33 |
+
predictions = np.argmax(predictions, axis=2)
|
34 |
+
return predictions
|
35 |
+
|
36 |
+
def decode_segmentation_masks(mask, colormap, n_classes):
|
37 |
+
r = np.zeros_like(mask).astype(np.uint8)
|
38 |
+
g = np.zeros_like(mask).astype(np.uint8)
|
39 |
+
b = np.zeros_like(mask).astype(np.uint8)
|
40 |
+
for l in range(0, n_classes):
|
41 |
+
idx = mask == l
|
42 |
+
r[idx] = colormap[l, 0]
|
43 |
+
g[idx] = colormap[l, 1]
|
44 |
+
b[idx] = colormap[l, 2]
|
45 |
+
rgb = np.stack([r, g, b], axis=2)
|
46 |
+
return rgb
|
47 |
+
|
48 |
+
def get_overlay(image, colored_mask):
|
49 |
+
image = tf.keras.preprocessing.image.array_to_img(image)
|
50 |
+
image = np.array(image).astype(np.uint8)
|
51 |
+
overlay = cv2.addWeighted(image, 0.35, colored_mask, 0.65, 0)
|
52 |
+
return overlay
|
53 |
+
|
54 |
+
def segmentation(input_image):
|
55 |
+
image_tensor = read_image(input_image)
|
56 |
+
prediction_mask = infer(image_tensor=image_tensor, model=model)
|
57 |
+
prediction_colormap = decode_segmentation_masks(prediction_mask, colormap, 20)
|
58 |
+
overlay = get_overlay(image_tensor, prediction_colormap)
|
59 |
+
return (overlay, prediction_colormap)
|
60 |
+
|
61 |
+
i = gr.inputs.Image()
|
62 |
+
o = [gr.outputs.Image('pil'), gr.outputs.Image('pil')]
|
63 |
+
|
64 |
+
examples = [["example_image_2.jpeg"], ["example_image_2.jpg"], ["example_image_3.jpeg"]]
|
65 |
+
|
66 |
+
col1, col2, col3 = st.columns(3)
|
67 |
+
with col1:
|
68 |
+
ex1 = Image.open(examples[0])
|
69 |
+
st.image(ex1, width=200)
|
70 |
+
if st.button("Corre ejemplo 1"):
|
71 |
+
file_imagen = examples[0]
|
72 |
+
|
73 |
+
with col2:
|
74 |
+
ex2 = Image.open(examples[1])
|
75 |
+
st.image(ex2, width=200)
|
76 |
+
if st.button("Corre ejemplo 1"):
|
77 |
+
file_imagen = examples[1]S
|
78 |
+
|
79 |
+
with col3:
|
80 |
+
ex3 = Image.open(examples[2])
|
81 |
+
st.image(ex3, width=200)
|
82 |
+
if st.button("Corre ejemplo 1"):
|
83 |
+
file_imagen = examples[2]
|
84 |
+
|
85 |
+
article = "<div style='text-align: center;'><a href='https://keras.io/examples/vision/deeplabv3_plus/' target='_blank'>Keras example by Praveen Kaushik</a></div>"
|
86 |
+
# gr.Interface(segmentation, i, o, examples=examples, allow_flagging=False, analytics_enabled=False,
|
87 |
+
# title=title, description=description, article=article).launch(enable_queue=True)
|