Spaces:
Runtime error
Runtime error
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) | |