Spaces:
Sleeping
Sleeping
File size: 4,642 Bytes
7842e83 6cc6b6b 7842e83 6cc6b6b eba227e 6cc6b6b eba227e 7842e83 872f59a 418578d 7842e83 418578d 0c5b5d7 6cc6b6b 418578d 0c5b5d7 418578d 538bf82 418578d 538bf82 418578d 538bf82 6cc6b6b 6934201 eba227e 6cc6b6b 418578d eba227e 7842e83 872f59a 7842e83 872f59a 7842e83 418578d 538bf82 7842e83 6cc6b6b 7842e83 418578d 7842e83 418578d 7842e83 d7a7630 418578d d7a7630 7842e83 6cc6b6b 7842e83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#######################################################################################
#
# MIT License
#
# Copyright (c) [2025] [leonelhs@gmail.com]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#######################################################################################
#
# This project is one of several repositories exploring image segmentation techniques.
# All related projects and interactive demos can be found at:
# https://huggingface.co/spaces/leonelhs/removators
# huggingface: https://huggingface.co/spaces/leonelhs/segformer-tf-transformers
#
import os
from itertools import islice
import cv2
import numpy as np
import onnxruntime as ort
import gradio as gr
from PIL import Image
from huggingface_hub import hf_hub_download
from pallete import colormap
from labels import cloth_labels, fashion_labels, ADE20k_labels
REPO_ID = "leonelhs/segmentators"
ADE20k_path = hf_hub_download(repo_id=REPO_ID, filename="segformer/segformer-b5-finetuned-ade-640-640.onnx")
fashion_path = hf_hub_download(repo_id=REPO_ID, filename="segformer/segformer-b3-fashion.onnx")
clothes_path = hf_hub_download(repo_id=REPO_ID, filename="segformer/segformer_b2_clothes.onnx")
sess_options = ort.SessionOptions()
sess_options.intra_op_num_threads = os.cpu_count()
session_ade20k = ort.InferenceSession(fashion_path, sess_options, providers=["CPUExecutionProvider"])
session_cloth = ort.InferenceSession(clothes_path, sess_options, providers=["CPUExecutionProvider"])
session_fashion = ort.InferenceSession(fashion_path, sess_options, providers=["CPUExecutionProvider"])
def predict(input_img, model="ADE20k"):
session = session_ade20k
labels = ADE20k_labels
if model == "Cloth":
session = session_cloth
labels = cloth_labels
elif model == "Fashion":
session = session_fashion
labels = fashion_labels
img = cv2.imread(input_img)
img = cv2.resize(img, (640, 640)).astype(np.float32)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_batch = np.expand_dims(img, axis=0)
img_batch = np.transpose(img_batch, (0, 3, 1, 2))
inputs = {'input': img_batch}
logits = session.run(None, inputs)[0]
logits = np.transpose(logits, (0, 2, 3, 1))
segmented_mask = np.argmax(logits, axis=-1)[0].astype("float32")
segmented_mask = cv2.resize(segmented_mask, (640, 640)).astype("uint8")
parts = []
unique_labels = np.unique(segmented_mask)
for label in unique_labels:
part = np.where(segmented_mask == label)
color_seg = np.full((640, 640, 3), 0, dtype=np.uint8)
color_seg[part[0], part[1], :] = colormap[label]
color_seg = cv2.cvtColor(color_seg, cv2.COLOR_BGR2GRAY)
parts.append((color_seg, labels[label]))
return Image.fromarray(img.astype("uint8")), parts
with gr.Blocks(title="SegFormer") as app:
navbar = gr.Navbar(visible=True, main_page_name="Workspace")
gr.Markdown("## SegFormer ONNX")
with gr.Row():
with gr.Column(scale=1):
inp = gr.Image(type="filepath", label="Upload Image")
mod = gr.Dropdown(choices=["ADE20k","Cloth","Fashion"], label="Model generator", value="ADE20k")
btn_predict = gr.Button("Parse")
with gr.Column(scale=2):
out = gr.AnnotatedImage(label="Image parsed annotated")
btn_predict.click(predict, inputs=[inp, mod], outputs=[out])
with app.route("About this", "/about"):
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() |