File size: 2,188 Bytes
1fe4dbb
 
 
 
 
 
 
d758bd2
1fe4dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d758bd2
1fe4dbb
 
 
76d8c20
1fe4dbb
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import random
import json
from PIL import Image, ImageDraw

# Open the JSON file
with open('image1.json', 'r') as file:
    # Load the JSON data from the file
    data = json.load(file)


with gr.Blocks() as demo:
    section_labels = [
        "pantalon",
        "pan droit",
        "pan gauche",
        "pan droit",
        "jointure",
        "ceinture",
        "bracelet",
        "bracelet",
        "etiquette",
        "poches",
        "plis",
    ]

    num_segments = 11
    width =3024
    height =4032
    with gr.Row():
        img_input = gr.Image()
        img_output = gr.AnnotatedImage().style(
            color_map={
                "plis": "#8CBF3D",
                "poches": "#D24A9A",
                "pan gauche": "#C94C4E",
                "etiquette": "#2FAD9E",
                "jointure": "#4D62F8",
                "ceinture": "#A154B0",
                "bracelet": "#F472B6",
                "bracelet": "#7ECE8E",
                "pan droit": "#ED8243",
                "pan droit": "#ED8243",
                "pantalon": "#3FBA90",
                }
        )

    # section_btn = gr.Button("Détecter les composants du pantalon")

    def getmask(polygon):
        img = Image.new('L', (width, height), 0)
        ImageDraw.Draw(img).polygon(polygon, outline=1, fill=1)
        mask = np.array(img)
        return mask

    def section(img):
        sections = []
        for b in range(num_segments):
            polygon = data['annotations'][b-1]['segmentation'][0]
            mask = getmask(polygon)
            sections.append((mask, section_labels[b-1]))
        return (img, sections)

    # section_btn.click(section, [img_input], img_output)
    # section_btn.click(section, [img_input, num_boxes, num_segments], img_output)


    def select_section(evt: gr.SelectData):
        return section_labels[evt.index]
    
    gr.Examples(
        examples=["20230210_110954 _1_.png"],
        inputs=img_input,
        outputs=img_output,
        fn=section,
        label="Exemple de pantalon (cliquer)",
        cache_examples=True,
    )

    img_output.select(select_section)


demo.launch(debug=True)