ciasimbaya commited on
Commit
14511a6
1 Parent(s): 3456a74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -2
app.py CHANGED
@@ -5,11 +5,87 @@ import cv2
5
  from keras.models import Model
6
  from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate
7
 
8
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- gr.Interface(
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  inputs="image",
 
 
 
 
 
13
  examples=[["benign(10).png"], ["benign(109).png"]],
14
  title = '<h1 style="text-align: center;">Breast Cancer Ultrasound Image Segmentation!</h1>',
15
  description = """
 
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="Class 0 Percentage"),
87
+ gr.Number(label="Class 1 Percentage")
88
+ ],
89
  examples=[["benign(10).png"], ["benign(109).png"]],
90
  title = '<h1 style="text-align: center;">Breast Cancer Ultrasound Image Segmentation!</h1>',
91
  description = """