File size: 3,461 Bytes
6142843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#######################################################################################
#
# 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/human-parser
#
from itertools import islice

import gradio as gr
import numpy as np
from PIL import Image
from ultralytics import YOLO
from huggingface_hub import hf_hub_download

# model = YOLO("yolo11x-seg.pt")  # only to test official model

REPO_ID = "MnLgt/yolo-human-parse"
model_path = hf_hub_download(repo_id=REPO_ID, filename="yolo-human-parse-epoch-125.pt")
model = YOLO(model_path)

# use for show bounding boxes
def predict_box(image):
    sections = []
    results = model(image)[0]  # predict on an image
    for result in results.boxes:
        box = np.asarray(result.xyxy)[0]
        label = results.names[int(result.cls)]
        sections.append(((int(box[0]), int(box[1]), int(box[2]), int(box[3])), label))
    return image, sections


def predict(image):
    sections = []
    results = model(image)[0]  # predict on an image
    for box, mask in zip(results.boxes, results.masks):
        label = results.names[int(box.cls)]
        data = np.asarray(mask.data)
        sections.append((data, label))

    width = results.masks.shape[1]
    height = results.masks.shape[2]
    image = image.resize((height, width), Image.Resampling.BILINEAR)
    return image, sections

with gr.Blocks(title="Yolo human parser") as app:
    navbar = gr.Navbar(visible=True, main_page_name="Workspace")
    gr.Markdown("## Yolo human parser")
    with gr.Row():
        with gr.Column(scale=1):
            inp = gr.Image(type="pil", label="Upload Image")
            btn_predict = gr.Button("Segment")
        with gr.Column(scale=2):
            out = gr.AnnotatedImage(label="Segments annotated")

    btn_predict.click(predict, inputs=[inp], outputs=[out])

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()