DHEIVER commited on
Commit
5e42969
·
1 Parent(s): bd447b9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
14
+ return image
15
+
16
+ def conv_block(input, num_filters):
17
+ conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(input)
18
+ conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(conv)
19
+ return conv
20
+
21
+ def encoder_block(input, num_filters):
22
+ conv = conv_block(input, num_filters)
23
+ pool = MaxPooling2D((2, 2))(conv)
24
+ return conv, pool
25
+
26
+ def decoder_block(input, skip_features, num_filters):
27
+ uconv = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same")(input)
28
+ con = concatenate([uconv, skip_features])
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)
36
+ s2, p2 = encoder_block(p1, 128)
37
+ s3, p3 = encoder_block(p2, 256)
38
+ s4, p4 = encoder_block(p3, 512)
39
+
40
+ b1 = conv_block(p4, 1024)
41
+
42
+ d1 = decoder_block(b1, s4, 512)
43
+ d2 = decoder_block(d1, s3, 256)
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]
63
+ mask_image = np.squeeze(mask_image, -1)
64
+ mask_image *= 255
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
+ examples=[["benign.png"], ["malignant .png"]],
90
+ title = '<h1 style="text-align: center;">Breast Cancer </h1>',
91
+
92
+ description = """
93
+ Explore essa incrível novidade no diagnóstico e tratamento do câncer de mama!
94
+ Apresentamos a demo de Segmentação de Imagem por Ultrassom de Câncer de Mama.
95
+ Faça o upload de uma imagem ou experimente um dos exemplos abaixo! 🙌
96
+ """,
97
+ theme="default",
98
+ layout="vertical",
99
+ verbose=True
100
+ ).launch(debug=True)