saba000farahani commited on
Commit
1d2e742
·
verified ·
1 Parent(s): 6b1afed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import os
5
+ import tensorflow as tf
6
+ from tensorflow.keras.models import load_model
7
+
8
+ # Load the Mask R-CNN model
9
+ model_path = os.path.join('toolkit', 'condmodel_100.h5') # Path to your model
10
+ mask_rcnn_model = load_model(model_path)
11
+
12
+ def apply_mask_rcnn(image):
13
+ """
14
+ Function to apply the Mask R-CNN model and return the segmented image.
15
+ :param image: Input image in numpy array format
16
+ :return: Image with segmentation mask overlaid
17
+ """
18
+ try:
19
+ # Convert image to RGB (in case of RGBA or grayscale)
20
+ if image.shape[2] == 4: # Convert RGBA to RGB
21
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
22
+
23
+ # Resize the image to the input size of the model
24
+ resized_image = cv2.resize(image, (224, 224)) # Adjust according to model input size
25
+ input_image = np.expand_dims(resized_image, axis=0)
26
+
27
+ # Use Mask R-CNN to predict the mask
28
+ prediction = mask_rcnn_model.predict(input_image)
29
+
30
+ # Assuming the first output is the mask, you may need to adjust based on your model's structure
31
+ mask = prediction[0]
32
+ mask = np.squeeze(mask) # Remove any unnecessary dimensions
33
+
34
+ # Resize mask back to the original image size
35
+ mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
36
+
37
+ # Create a segmentation overlay on the original image
38
+ mask_overlay = np.zeros_like(image)
39
+ mask_overlay[mask > 0.5] = [0, 255, 0] # Green mask
40
+
41
+ # Combine the original image with the mask
42
+ segmented_image = cv2.addWeighted(image, 1, mask_overlay, 0.5, 0)
43
+
44
+ return segmented_image
45
+
46
+ except Exception as e:
47
+ print(f"Error in segmentation: {e}")
48
+ return image # Return original image if segmentation fails
49
+
50
+ # Update Gradio interface for image input/output
51
+ inputs = gr.Image(source="upload", tool="editor", type="numpy", label="Upload an image")
52
+ outputs = gr.Image(type="numpy", label="Segmented Image")
53
+
54
+ # Gradio interface definition
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("<h1 style='text-align: center;'>Image Segmentation with Mask R-CNN</h1>")
57
+ gr.Markdown("Upload an image to see segmentation results using the Mask R-CNN model.")
58
+
59
+ # Input and output components
60
+ with gr.Row():
61
+ with gr.Column():
62
+ gr.Markdown("### Upload an Image")
63
+ inputs.render() # Render the input (image upload)
64
+
65
+ # Submit button
66
+ gr.Button("Submit").click(fn=apply_mask_rcnn, inputs=inputs, outputs=outputs)
67
+ gr.Button("Clear").click(fn=lambda: None)
68
+
69
+ with gr.Column():
70
+ gr.Markdown("### Segmented Image Output")
71
+ outputs.render() # Render the output (segmented image)
72
+
73
+ # Launch the Gradio app
74
+ demo.launch()