Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import json
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
+
|
7 |
+
# Open the JSON file
|
8 |
+
with open('/content/image1.json', 'r') as file:
|
9 |
+
# Load the JSON data from the file
|
10 |
+
data = json.load(file)
|
11 |
+
|
12 |
+
|
13 |
+
with gr.Blocks() as demo:
|
14 |
+
section_labels = [
|
15 |
+
"pantalon",
|
16 |
+
"pan droit",
|
17 |
+
"pan gauche",
|
18 |
+
"pan droit",
|
19 |
+
"jointure",
|
20 |
+
"ceinture",
|
21 |
+
"bracelet",
|
22 |
+
"bracelet",
|
23 |
+
"etiquette",
|
24 |
+
"poches",
|
25 |
+
"plis",
|
26 |
+
]
|
27 |
+
|
28 |
+
num_segments = 11
|
29 |
+
width =3024
|
30 |
+
height =4032
|
31 |
+
with gr.Row():
|
32 |
+
img_input = gr.Image()
|
33 |
+
img_output = gr.AnnotatedImage().style(
|
34 |
+
color_map={
|
35 |
+
"plis": "#8CBF3D",
|
36 |
+
"poches": "#D24A9A",
|
37 |
+
"pan gauche": "#C94C4E",
|
38 |
+
"etiquette": "#2FAD9E",
|
39 |
+
"jointure": "#4D62F8",
|
40 |
+
"ceinture": "#A154B0",
|
41 |
+
"bracelet": "#F472B6",
|
42 |
+
"bracelet": "#7ECE8E",
|
43 |
+
"pan droit": "#ED8243",
|
44 |
+
"pan droit": "#ED8243",
|
45 |
+
"pantalon": "#3FBA90",
|
46 |
+
}
|
47 |
+
)
|
48 |
+
|
49 |
+
# section_btn = gr.Button("Détecter les composants du pantalon")
|
50 |
+
|
51 |
+
def getmask(polygon):
|
52 |
+
img = Image.new('L', (width, height), 0)
|
53 |
+
ImageDraw.Draw(img).polygon(polygon, outline=1, fill=1)
|
54 |
+
mask = np.array(img)
|
55 |
+
return mask
|
56 |
+
|
57 |
+
def section(img):
|
58 |
+
sections = []
|
59 |
+
for b in range(num_segments):
|
60 |
+
polygon = data['annotations'][b-1]['segmentation'][0]
|
61 |
+
mask = getmask(polygon)
|
62 |
+
sections.append((mask, section_labels[b-1]))
|
63 |
+
return (img, sections)
|
64 |
+
|
65 |
+
# section_btn.click(section, [img_input], img_output)
|
66 |
+
# section_btn.click(section, [img_input, num_boxes, num_segments], img_output)
|
67 |
+
|
68 |
+
|
69 |
+
def select_section(evt: gr.SelectData):
|
70 |
+
return section_labels[evt.index]
|
71 |
+
|
72 |
+
gr.Examples(
|
73 |
+
examples=["/content/20230210_110954 _1_.png"],
|
74 |
+
inputs=img_input,
|
75 |
+
outputs=img_output,
|
76 |
+
fn=section,
|
77 |
+
cache_examples=True,
|
78 |
+
)
|
79 |
+
|
80 |
+
img_output.select(select_section)
|
81 |
+
|
82 |
+
|
83 |
+
demo.launch(debug=True)
|
84 |
+
|
85 |
+
|