|
import cv2 |
|
import numpy as np |
|
import tensorflow as tf |
|
import gradio as gr |
|
|
|
def remove_background_deeplab(image_path): |
|
|
|
interpreter = tf.lite.Interpreter(model_path="2.tflite") |
|
interpreter.allocate_tensors() |
|
|
|
input_details = interpreter.get_input_details() |
|
output_details = interpreter.get_output_details() |
|
|
|
|
|
image = cv2.imread(image_path) |
|
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
input_size = (257, 257) |
|
image_resized = cv2.resize(image_rgb, input_size) |
|
|
|
|
|
input_tensor = (image_resized / 127.5 - 1.0).astype(np.float32) |
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], np.expand_dims(input_tensor, axis=0)) |
|
|
|
|
|
interpreter.invoke() |
|
|
|
|
|
predictions = interpreter.get_tensor(output_details[0]['index']) |
|
mask = np.argmax(predictions, axis=-1)[0] |
|
|
|
|
|
binary_mask = cv2.resize(np.where(mask == 15, 1, 0).astype(np.uint8), (image.shape[1], image.shape[0])) |
|
|
|
|
|
result = image * binary_mask[:, :, np.newaxis] |
|
|
|
|
|
cv2.imshow('Result', result) |
|
cv2.waitKey(0) |
|
cv2.destroyAllWindows() |
|
|
|
|
|
|
|
gr.Interface(fn=remove_background_deeplab,inputs=gr.Image(label='Drop an Image or Open Camera to Classify'),outputs=gr.Image()).launch() |