|
|
import cv2
|
|
|
import numpy as np
|
|
|
import tensorflow as tf
|
|
|
import tensorflow_hub as hub
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
model = hub.load("https://tfhub.dev/tensorflow/deeplabv3/1")
|
|
|
|
|
|
|
|
|
def run_inference(model, image):
|
|
|
image = tf.convert_to_tensor(image)
|
|
|
image = tf.image.convert_image_dtype(image, tf.uint8)
|
|
|
image = tf.image.resize(image, [513, 513])
|
|
|
image = tf.expand_dims(image, axis=0)
|
|
|
result = model(image)
|
|
|
return result['semantic_pred'][0]
|
|
|
|
|
|
|
|
|
def remove_background(image, mask):
|
|
|
mask = cv2.resize(mask.numpy(), (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
|
|
mask = mask == 15
|
|
|
result = np.where(mask[..., None], image, 0)
|
|
|
return result
|
|
|
|
|
|
|
|
|
def process_image(image):
|
|
|
|
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
|
|
mask = run_inference(model, image)
|
|
|
|
|
|
|
|
|
result = remove_background(image, mask)
|
|
|
|
|
|
|
|
|
result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=process_image,
|
|
|
inputs=gr.inputs.Image(source="webcam", tool="editor"),
|
|
|
outputs=gr.outputs.Image(type="auto"),
|
|
|
live=True)
|
|
|
|
|
|
|
|
|
iface.launch()
|
|
|
|