K-A-Uthman commited on
Commit
6a1aee3
·
verified ·
1 Parent(s): b5a3ac5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -60
app.py CHANGED
@@ -1,61 +1,60 @@
1
- import gradio as gr
2
- import numpy as np
3
- import keras
4
- import torch
5
- from PIL import Image
6
- from keras.preprocessing import image as keras_image
7
-
8
- # load the model
9
- def load_model(model_path):
10
- model = keras.models.load_model(model_path)
11
- return model
12
-
13
-
14
- # Function to preprocess the input image
15
- def preprocess_image(image):
16
- # Convert image to grayscale
17
- image = np.array(image)
18
- image = Image.fromarray(image).convert('L')
19
-
20
- # Resize the image
21
- image = image.resize((128, 128))
22
-
23
- # Convert to numpy array and normalize
24
- image = np.array(image)
25
- image = image / 255.0
26
-
27
- # Add batch dimension
28
- image = np.expand_dims(image, axis=-1)
29
-
30
- # Stack the grayscale image to make it a 2-channel image
31
- image = np.repeat(image, 2, axis=-1)
32
-
33
- # Add batch dimension
34
- image = np.expand_dims(image, axis=0)
35
-
36
- return image
37
-
38
- # Function to perform segmentation prediction
39
- def predict_segmentation(model, image):
40
- segmentation_map = model.predict(image)[0]
41
- threshold = 0.5
42
- segmented_image = (segmentation_map > threshold).astype(np.uint8)
43
- segmented_image = Image.fromarray(segmented_image * 255)
44
- return segmented_image
45
-
46
-
47
- # Define Gradio interface
48
- def gradio_interface(model_path):
49
- # Load the model
50
- model = load_model(model_path)
51
-
52
- # Define input and output components
53
- demo = gr.Interface(lambda image: predict_segmentation(model, preprocess_image(image)),
54
- inputs = "image" ,
55
- outputs= "image" ,
56
- title = "Brain Tumor Segmentation",
57
- description = "Upload an image of a brain scan, and the model will segment the brain tumor.")
58
-
59
- demo.launch(share=True)
60
-
61
  gradio_interface("model_x81_dcs65.h5")
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import keras
4
+ from PIL import Image
5
+ from keras.preprocessing import image as keras_image
6
+
7
+ # load the model
8
+ def load_model(model_path):
9
+ model = keras.models.load_model(model_path)
10
+ return model
11
+
12
+
13
+ # Function to preprocess the input image
14
+ def preprocess_image(image):
15
+ # Convert image to grayscale
16
+ image = np.array(image)
17
+ image = Image.fromarray(image).convert('L')
18
+
19
+ # Resize the image
20
+ image = image.resize((128, 128))
21
+
22
+ # Convert to numpy array and normalize
23
+ image = np.array(image)
24
+ image = image / 255.0
25
+
26
+ # Add batch dimension
27
+ image = np.expand_dims(image, axis=-1)
28
+
29
+ # Stack the grayscale image to make it a 2-channel image
30
+ image = np.repeat(image, 2, axis=-1)
31
+
32
+ # Add batch dimension
33
+ image = np.expand_dims(image, axis=0)
34
+
35
+ return image
36
+
37
+ # Function to perform segmentation prediction
38
+ def predict_segmentation(model, image):
39
+ segmentation_map = model.predict(image)[0]
40
+ threshold = 0.5
41
+ segmented_image = (segmentation_map > threshold).astype(np.uint8)
42
+ segmented_image = Image.fromarray(segmented_image * 255)
43
+ return segmented_image
44
+
45
+
46
+ # Define Gradio interface
47
+ def gradio_interface(model_path):
48
+ # Load the model
49
+ model = load_model(model_path)
50
+
51
+ # Define input and output components
52
+ demo = gr.Interface(lambda image: predict_segmentation(model, preprocess_image(image)),
53
+ inputs = "image" ,
54
+ outputs= "image" ,
55
+ title = "Brain Tumor Segmentation",
56
+ description = "Upload an image of a brain scan, and the model will segment the brain tumor.")
57
+
58
+ demo.launch(share=True)
59
+
 
60
  gradio_interface("model_x81_dcs65.h5")