Jeysshon
commited on
Commit
·
6bc2d32
1
Parent(s):
69b0499
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
from PIL import Image
|
|
|
|
|
4 |
from keras.models import Model
|
5 |
from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate
|
6 |
|
7 |
-
|
|
|
|
|
8 |
image = image.resize((size, size))
|
9 |
image = image.convert("L")
|
10 |
image = np.array(image) / 255.0
|
@@ -26,7 +29,7 @@ def decoder_block(input, skip_features, num_filters):
|
|
26 |
conv = conv_block(con, num_filters)
|
27 |
return conv
|
28 |
|
29 |
-
def build_model(input_shape
|
30 |
input_layer = Input(input_shape)
|
31 |
|
32 |
s1, p1 = encoder_block(input_layer, 64)
|
@@ -41,13 +44,19 @@ def build_model(input_shape, num_output_channels):
|
|
41 |
d3 = decoder_block(d2, s2, 128)
|
42 |
d4 = decoder_block(d3, s1, 64)
|
43 |
|
44 |
-
output_layer = Conv2D(
|
45 |
model = Model(input_layer, output_layer, name="U-Net")
|
46 |
model.load_weights('modelo.h5')
|
47 |
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
def
|
50 |
-
image = preprocess_image(image, size)
|
51 |
image = np.expand_dims(image, 0)
|
52 |
output = model.predict(image, verbose=0)
|
53 |
mask_image = output[0]
|
@@ -56,33 +65,59 @@ def image_segmentation(image, size=128, num_output_channels=1):
|
|
56 |
mask_image = mask_image.astype(np.uint8)
|
57 |
mask_image = Image.fromarray(mask_image).convert("L")
|
58 |
|
|
|
59 |
positive_pixels = np.count_nonzero(mask_image)
|
60 |
total_pixels = mask_image.size[0] * mask_image.size[1]
|
61 |
percentage = (positive_pixels / total_pixels) * 100
|
62 |
|
|
|
63 |
class_0_percentage = 100 - percentage
|
64 |
class_1_percentage = percentage
|
65 |
|
66 |
return mask_image, class_0_percentage, class_1_percentage
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
gr.Interface(
|
73 |
fn=image_segmentation,
|
74 |
inputs="image",
|
75 |
outputs=[
|
76 |
-
gr.Image(type="pil", label="Cáncer de
|
77 |
gr.Number(label="Benigno"),
|
78 |
gr.Number(label="Maligno")
|
79 |
],
|
80 |
-
title='<h1 style="text-align: center;">Cáncer de
|
81 |
description="""
|
82 |
-
|
83 |
""",
|
|
|
|
|
|
|
|
|
|
|
84 |
theme="default",
|
85 |
layout="vertical",
|
86 |
verbose=True
|
87 |
).launch(debug=True)
|
88 |
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
from keras.models import Model
|
6 |
from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate
|
7 |
|
8 |
+
size = 128
|
9 |
+
|
10 |
+
def preprocess_image(image, size=128):
|
11 |
image = image.resize((size, size))
|
12 |
image = image.convert("L")
|
13 |
image = np.array(image) / 255.0
|
|
|
29 |
conv = conv_block(con, num_filters)
|
30 |
return conv
|
31 |
|
32 |
+
def build_model(input_shape):
|
33 |
input_layer = Input(input_shape)
|
34 |
|
35 |
s1, p1 = encoder_block(input_layer, 64)
|
|
|
44 |
d3 = decoder_block(d2, s2, 128)
|
45 |
d4 = decoder_block(d3, s1, 64)
|
46 |
|
47 |
+
output_layer = Conv2D(1, 1, padding="same", activation="sigmoid")(d4)
|
48 |
model = Model(input_layer, output_layer, name="U-Net")
|
49 |
model.load_weights('modelo.h5')
|
50 |
return model
|
51 |
+
|
52 |
+
def preprocess_image(image, size=128):
|
53 |
+
image = cv2.resize(image, (size, size))
|
54 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
55 |
+
image = image / 255.
|
56 |
+
return image
|
57 |
|
58 |
+
def segment(image):
|
59 |
+
image = preprocess_image(image, size=size)
|
60 |
image = np.expand_dims(image, 0)
|
61 |
output = model.predict(image, verbose=0)
|
62 |
mask_image = output[0]
|
|
|
65 |
mask_image = mask_image.astype(np.uint8)
|
66 |
mask_image = Image.fromarray(mask_image).convert("L")
|
67 |
|
68 |
+
#Porcentaje de 0
|
69 |
positive_pixels = np.count_nonzero(mask_image)
|
70 |
total_pixels = mask_image.size[0] * mask_image.size[1]
|
71 |
percentage = (positive_pixels / total_pixels) * 100
|
72 |
|
73 |
+
# Calcular los porcentajes de 0 y 1
|
74 |
class_0_percentage = 100 - percentage
|
75 |
class_1_percentage = percentage
|
76 |
|
77 |
return mask_image, class_0_percentage, class_1_percentage
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
+
model = build_model(input_shape=(size, size, 1))
|
81 |
+
gr.Interface(
|
82 |
+
fn=segment,
|
83 |
+
inputs="image",
|
84 |
+
outputs=[
|
85 |
+
gr.Image(type="pil", label="Breast Cancer Mask"),
|
86 |
+
gr.Number(label="Benigno"),
|
87 |
+
gr.Number(label="Maligno")
|
88 |
+
],
|
89 |
+
title = '<h1 style="text-align: center;"> Cancer ultrasonido de Cancer de Mama </h1>',
|
90 |
+
|
91 |
+
description = """
|
92 |
+
Presentamos la demostración de Segmentación de Imágenes por Ultrasonido de Cáncer de Mama.
|
93 |
+
""",
|
94 |
+
theme="default",
|
95 |
+
layout="vertical",
|
96 |
+
verbose=True
|
97 |
+
).launch(debug=True)
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
model = build_model(input_shape=(size, size, 1))
|
101 |
gr.Interface(
|
102 |
fn=image_segmentation,
|
103 |
inputs="image",
|
104 |
outputs=[
|
105 |
+
gr.Image(type="pil", label="Máscara de Cáncer de Mama"),
|
106 |
gr.Number(label="Benigno"),
|
107 |
gr.Number(label="Maligno")
|
108 |
],
|
109 |
+
title='<h1 style="text-align: center;">Segmentación de Ultrasonidos de Cáncer de Mama</h1>',
|
110 |
description="""
|
111 |
+
Presentamos la demostración de Segmentación de Imágenes por Ultrasonido de Cáncer de Mama.
|
112 |
""",
|
113 |
+
examples=[
|
114 |
+
['benign(10).png'],
|
115 |
+
['benign(109).png'],
|
116 |
+
['malignant.png']
|
117 |
+
],
|
118 |
theme="default",
|
119 |
layout="vertical",
|
120 |
verbose=True
|
121 |
).launch(debug=True)
|
122 |
|
123 |
+
|