saba000farahani commited on
Commit
fef10af
·
verified ·
1 Parent(s): fc6210a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -3,33 +3,27 @@ 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
@@ -39,15 +33,17 @@ def apply_mask_rcnn(image):
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]))
 
3
  import numpy as np
4
  import tensorflow as tf
5
  import cv2
6
+ from mrcnn import model as modellib
7
+ from mrcnn.config import Config
8
 
9
  # Set environment variable to avoid floating-point errors
10
  os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
11
 
12
+ # Define Mask R-CNN configuration
13
+ class InferenceConfig(Config):
14
+ NAME = "mask_rcnn"
15
+ NUM_CLASSES = 1 + 80 # Update according to your dataset
16
+ GPU_COUNT = 1
17
+ IMAGES_PER_GPU = 1
18
+
19
+ config = InferenceConfig()
 
 
 
 
 
 
 
 
20
 
21
+ # Rebuild the Mask R-CNN model
22
+ model = modellib.MaskRCNN(mode="inference", config=config, model_dir=os.getcwd())
23
 
24
  # Load the Mask R-CNN model weights
25
+ model_path = os.path.join('toolkit', 'condmodel_100.h5')
26
+ model.load_weights(model_path, by_name=True)
27
  print("Mask R-CNN model loaded successfully with weights.")
28
 
29
  # Function to apply Mask R-CNN for image segmentation
 
33
  if image.shape[2] == 4: # Convert RGBA to RGB
34
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
35
 
36
+ # Resize the image to match the model input size (for inference)
37
+ resized_image = cv2.resize(image, (1024, 1024)) # Adjust based on the input shape of your model
38
  input_image = np.expand_dims(resized_image, axis=0)
39
 
40
+ # Use Mask R-CNN to predict
41
+ result = model.detect(input_image, verbose=0)
42
+ r = result[0]
43
 
44
+ # Create a mask for the detected objects
45
+ mask = r['masks']
46
+ mask = np.sum(mask, axis=-1) # Combine masks for all objects
47
 
48
  # Resize mask back to the original image size
49
  mask = cv2.resize(mask, (image.shape[1], image.shape[0]))