Om Prakash Singh commited on
Commit
d251eff
1 Parent(s): 9ca7827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -32,7 +32,7 @@ import gradio as gr
32
  # Your code using these packages can follow here
33
 
34
 
35
- def remove_background_deeplab(image_path):
36
  # Load the TensorFlow Lite model
37
  interpreter = tf.lite.Interpreter(model_path="2.tflite")
38
  interpreter.allocate_tensors()
@@ -40,13 +40,12 @@ def remove_background_deeplab(image_path):
40
  input_details = interpreter.get_input_details()
41
  output_details = interpreter.get_output_details()
42
 
43
- # Load and preprocess the image
44
- image = cv2.imread(image_path)
45
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
46
 
47
  # Resize the image to match the expected input size of the model
48
  input_size = (257, 257)
49
- image_resized = cv2.resize(image_rgb, input_size)
50
 
51
  # Normalize the input image
52
  input_tensor = (image_resized / 127.5 - 1.0).astype(np.float32)
@@ -62,15 +61,16 @@ def remove_background_deeplab(image_path):
62
  mask = np.argmax(predictions, axis=-1)[0]
63
 
64
  # Resize the binary mask to match the shape of the image
65
- binary_mask = cv2.resize(np.where(mask == 15, 1, 0).astype(np.uint8), (image.shape[1], image.shape[0]))
66
 
67
  # Multiply the image with the binary mask to get the result
68
- result = image * binary_mask[:, :, np.newaxis]
 
 
 
 
 
69
 
70
- # Display the result or save it
71
- cv2.imshow('Result', result)
72
- cv2.waitKey(0)
73
- cv2.destroyAllWindows()
74
 
75
  # Example usage
76
  # remove_background_deeplab('images (2).jpeg')
 
32
  # Your code using these packages can follow here
33
 
34
 
35
+ def remove_background_deeplab(image):
36
  # Load the TensorFlow Lite model
37
  interpreter = tf.lite.Interpreter(model_path="2.tflite")
38
  interpreter.allocate_tensors()
 
40
  input_details = interpreter.get_input_details()
41
  output_details = interpreter.get_output_details()
42
 
43
+ # Convert the Gradio Image object to a NumPy array
44
+ image_np = np.array(image)
 
45
 
46
  # Resize the image to match the expected input size of the model
47
  input_size = (257, 257)
48
+ image_resized = cv2.resize(image_np, input_size)
49
 
50
  # Normalize the input image
51
  input_tensor = (image_resized / 127.5 - 1.0).astype(np.float32)
 
61
  mask = np.argmax(predictions, axis=-1)[0]
62
 
63
  # Resize the binary mask to match the shape of the image
64
+ binary_mask = cv2.resize(np.where(mask == 15, 1, 0).astype(np.uint8), (image_np.shape[1], image_np.shape[0]))
65
 
66
  # Multiply the image with the binary mask to get the result
67
+ result = image_np * binary_mask[:, :, np.newaxis]
68
+
69
+ # Convert the result back to Gradio Image object
70
+ result_image = gr.Image(result)
71
+
72
+ return result_image
73
 
 
 
 
 
74
 
75
  # Example usage
76
  # remove_background_deeplab('images (2).jpeg')