Om Prakash Singh commited on
Commit
bc7f524
1 Parent(s): 73cbb2d

Upload 6 files

Browse files
Files changed (6) hide show
  1. 2.tflite +3 -0
  2. app.py +48 -0
  3. images (1).jpeg +0 -0
  4. images (2).jpeg +0 -0
  5. images (3).jpeg +0 -0
  6. images.jpeg +0 -0
2.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9711334db2b01d5894feb8ed0f5cb3e97d125b8d229f8d8692f625801818f5ef
3
+ size 2780051
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import gradio as gr
5
+
6
+ def remove_background_deeplab(image_path):
7
+ # Load the TensorFlow Lite model
8
+ interpreter = tf.lite.Interpreter(model_path="2.tflite")
9
+ interpreter.allocate_tensors()
10
+
11
+ input_details = interpreter.get_input_details()
12
+ output_details = interpreter.get_output_details()
13
+
14
+ # Load and preprocess the image
15
+ image = cv2.imread(image_path)
16
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
17
+
18
+ # Resize the image to match the expected input size of the model
19
+ input_size = (257, 257)
20
+ image_resized = cv2.resize(image_rgb, input_size)
21
+
22
+ # Normalize the input image
23
+ input_tensor = (image_resized / 127.5 - 1.0).astype(np.float32)
24
+
25
+ # Set the input tensor to the model
26
+ interpreter.set_tensor(input_details[0]['index'], np.expand_dims(input_tensor, axis=0))
27
+
28
+ # Run inference
29
+ interpreter.invoke()
30
+
31
+ # Get the segmented mask
32
+ predictions = interpreter.get_tensor(output_details[0]['index'])
33
+ mask = np.argmax(predictions, axis=-1)[0]
34
+
35
+ # Resize the binary mask to match the shape of the image
36
+ binary_mask = cv2.resize(np.where(mask == 15, 1, 0).astype(np.uint8), (image.shape[1], image.shape[0]))
37
+
38
+ # Multiply the image with the binary mask to get the result
39
+ result = image * binary_mask[:, :, np.newaxis]
40
+
41
+ # Display the result or save it
42
+ cv2.imshow('Result', result)
43
+ cv2.waitKey(0)
44
+ cv2.destroyAllWindows()
45
+
46
+ # Example usage
47
+ # remove_background_deeplab('images (2).jpeg')
48
+ gr.Interface(fn=remove_background_deeplab,inputs=gr.Image(label='Drop an Image or Open Camera to Classify'),outputs=gr.Image()).launch()
images (1).jpeg ADDED
images (2).jpeg ADDED
images (3).jpeg ADDED
images.jpeg ADDED