|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from itertools import islice |
|
|
|
|
|
import cv2 |
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import onnxruntime as ort |
|
|
from PIL import Image |
|
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
REPO_ID = "leonelhs/removators" |
|
|
|
|
|
|
|
|
model_path = hf_hub_download(repo_id=REPO_ID, filename='isnet.onnx') |
|
|
|
|
|
session = ort.InferenceSession(model_path) |
|
|
|
|
|
def normalize(image, mean, std): |
|
|
"""Normalize a numpy image with mean and standard deviation.""" |
|
|
return (image / 255.0 - mean) / std |
|
|
|
|
|
def predict(image_path): |
|
|
input_size = (1024, 1024) |
|
|
|
|
|
img = cv2.imread(image_path, cv2.IMREAD_COLOR) |
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
|
|
|
if len(img.shape) == 2: |
|
|
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) |
|
|
|
|
|
|
|
|
img = img.astype(np.float32) |
|
|
im_normalized = normalize(img, mean=[0.5, 0.5, 0.5], std=[1.0, 1.0, 1.0]) |
|
|
|
|
|
|
|
|
img_resized = cv2.resize(im_normalized, input_size, interpolation=cv2.INTER_LINEAR) |
|
|
img_resized = np.transpose(img_resized, (2, 0, 1)) |
|
|
img_resized = np.expand_dims(img_resized, axis=0) |
|
|
|
|
|
|
|
|
img_resized = img_resized.astype(np.float32) |
|
|
ort_inputs = {session.get_inputs()[0].name: img_resized} |
|
|
prediction = session.run(None, ort_inputs) |
|
|
|
|
|
|
|
|
result = prediction[0][0] |
|
|
result = np.clip(result, 0, 1) |
|
|
result = (result * 255).astype(np.uint8) |
|
|
result = np.transpose(result, (1, 2, 0)) |
|
|
|
|
|
original_shape = img.shape[:2] |
|
|
return cv2.resize(result, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR) |
|
|
|
|
|
|
|
|
def cuts(image): |
|
|
mask = predict(image) |
|
|
mask = Image.fromarray(mask).convert('L') |
|
|
cutted = Image.open(image).convert("RGB") |
|
|
cutted.putalpha(mask) |
|
|
return [image, cutted], mask |
|
|
|
|
|
with gr.Blocks(title="DIS") as app: |
|
|
navbar = gr.Navbar(visible=True, main_page_name="Workspace") |
|
|
gr.Markdown("## Dichotomous Image Segmentation") |
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
inp_image = gr.Image(type="filepath", label="Upload Image") |
|
|
btn_predict = gr.Button(variant="primary", value="Remove background") |
|
|
with gr.Column(scale=2): |
|
|
with gr.Row(): |
|
|
preview = gr.ImageSlider(type="filepath", label="Comparer") |
|
|
|
|
|
btn_predict.click(cuts, inputs=[inp_image], outputs=[preview, inp_image]) |
|
|
|
|
|
with app.route("Readme", "/readme"): |
|
|
with open("README.md") as f: |
|
|
for line in islice(f, 12, None): |
|
|
gr.Markdown(line.strip()) |
|
|
|
|
|
|
|
|
app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True) |
|
|
app.queue() |
|
|
|