saba000farahani commited on
Commit
e8c4882
·
verified ·
1 Parent(s): 8534b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -1,35 +1,53 @@
 
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]))
@@ -47,16 +65,16 @@ def apply_mask_rcnn(image):
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")
 
1
+ import os
2
  import gradio as gr
3
  import numpy as np
 
 
4
  import tensorflow as tf
5
+ import cv2
6
+
7
+ # Set environment variable to avoid floating-point errors
8
+ os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
9
 
10
+ # Define the Mask R-CNN model architecture
11
+ def build_mask_rcnn_model():
12
+ input_layer = tf.keras.layers.Input(shape=(224, 224, 3)) # Adjust input shape to match your model
13
+ # Example architecture, you should modify it to match your actual Mask R-CNN model
14
+ x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(input_layer)
15
+ x = tf.keras.layers.MaxPooling2D((2, 2))(x)
16
+ x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
17
+ x = tf.keras.layers.MaxPooling2D((2, 2))(x)
18
+ x = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
19
+ x = tf.keras.layers.UpSampling2D((2, 2))(x)
20
+ x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
21
+ x = tf.keras.layers.UpSampling2D((2, 2))(x)
22
+ output_layer = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(x)
23
+
24
+ model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
25
+ return model
26
+
27
+ # Build the model and load weights
28
+ model = build_mask_rcnn_model()
29
+
30
+ # Load the Mask R-CNN model weights
31
+ model_path = os.path.join('toolkit', 'condmodel_100.h5') # Update with correct path
32
+ model.load_weights(model_path)
33
+ print("Mask R-CNN model loaded successfully with weights.")
34
 
35
+ # Function to apply Mask R-CNN for image segmentation
36
  def apply_mask_rcnn(image):
 
 
 
 
 
37
  try:
38
  # Convert image to RGB (in case of RGBA or grayscale)
39
  if image.shape[2] == 4: # Convert RGBA to RGB
40
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
41
 
42
+ # Resize the image to match the model input size
43
+ resized_image = cv2.resize(image, (224, 224)) # Adjust based on the input shape of your model
44
  input_image = np.expand_dims(resized_image, axis=0)
45
 
46
  # Use Mask R-CNN to predict the mask
47
+ prediction = model.predict(input_image)
48
 
49
+ # Extract mask (assumed to be the first output)
50
+ mask = np.squeeze(prediction[0])
 
51
 
52
  # Resize mask back to the original image size
53
  mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
 
65
  print(f"Error in segmentation: {e}")
66
  return image # Return original image if segmentation fails
67
 
68
+ # Gradio interface definition
69
  inputs = gr.Image(source="upload", tool="editor", type="numpy", label="Upload an image")
70
  outputs = gr.Image(type="numpy", label="Segmented Image")
71
 
72
+ # Gradio app layout
73
  with gr.Blocks() as demo:
74
  gr.Markdown("<h1 style='text-align: center;'>Image Segmentation with Mask R-CNN</h1>")
75
  gr.Markdown("Upload an image to see segmentation results using the Mask R-CNN model.")
76
 
77
+ # Input and output layout
78
  with gr.Row():
79
  with gr.Column():
80
  gr.Markdown("### Upload an Image")