CircleStar commited on
Commit
ea0a3ef
Β·
verified Β·
1 Parent(s): 56e1278

Fix class legend: render as an image instead of HTML-in-Markdown, since the Space sanitizer strips inline style attributes and left the color swatches blank

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -40,26 +40,6 @@ COLORS_RGB = np.array([
40
  [220, 50, 50], [255, 165, 0], [160, 90, 30], [240, 230, 140],
41
  ], dtype=np.uint8)
42
 
43
- def rgb_to_hex(rgb):
44
- return '#{:02x}{:02x}{:02x}'.format(*rgb)
45
-
46
- def build_class_legend_md():
47
- """Legend table with real color swatches (matching COLORS_RGB exactly),
48
- instead of emoji β€” some emoji pairs (e.g. green circle / green heart)
49
- render too close in hue to tell classes apart."""
50
- rows = "\n".join(
51
- f'| {i} | {CLASSES[i]} | '
52
- f'<span style="display:inline-block;width:16px;height:16px;'
53
- f'background-color:{rgb_to_hex(COLORS_RGB[i])};border:1px solid #888;'
54
- f'border-radius:3px;vertical-align:middle;"></span> |'
55
- for i in range(1, 8)
56
- )
57
- return f"""## Classes d'occupation du sol
58
-
59
- | NΒ° | Classe | Couleur |
60
- |----|--------|---------|
61
- {rows}
62
- """
63
 
64
  BAND_FILES = [
65
  'band_1_uv.tif', 'band_2_blue.tif', 'band_3_green.tif', 'band_4_red.tif',
@@ -221,6 +201,22 @@ def add_class_legend(fig):
221
  fig.legend(handles=handles, loc='lower center', ncol=4, fontsize=8,
222
  frameon=False, bbox_to_anchor=(0.5, -0.02))
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  def fig_confusion_matrix(matrix, oa, title, cmap='Blues'):
225
  """Generic 7Γ—7 confusion matrix plot: predicted classes vs Ground Truth."""
226
  short = CLASS_SHORT
@@ -414,7 +410,8 @@ puis soumettez vos rΓ©ponses pour gΓ©nΓ©rer la carte et la matrice de confusion.
414
 
415
  # ── Tab 1: LΓ©gende des classes ────────────────────────────────────
416
  with gr.Tab("Classes d'occupation du sol"):
417
- gr.Markdown(build_class_legend_md())
 
418
  gr.Markdown("""
419
  ---
420
  **Conseils d'interprΓ©tation :**
 
40
  [220, 50, 50], [255, 165, 0], [160, 90, 30], [240, 230, 140],
41
  ], dtype=np.uint8)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  BAND_FILES = [
45
  'band_1_uv.tif', 'band_2_blue.tif', 'band_3_green.tif', 'band_4_red.tif',
 
201
  fig.legend(handles=handles, loc='lower center', ncol=4, fontsize=8,
202
  frameon=False, bbox_to_anchor=(0.5, -0.02))
203
 
204
+ def fig_class_legend():
205
+ """Standalone class-color legend for Tab 1, rendered as an image rather
206
+ than HTML-in-Markdown β€” the Space's Markdown sanitizer strips inline
207
+ `style` attributes, so colored <span> swatches showed up blank there."""
208
+ handles = [
209
+ Patch(facecolor=COLORS_RGB[i] / 255.0, edgecolor='#888', label=f"{i} β€” {CLASSES[i]}")
210
+ for i in range(1, 8)
211
+ ]
212
+ fig, ax = plt.subplots(figsize=(6.5, 3.2))
213
+ ax.axis('off')
214
+ ax.legend(handles=handles, loc='center', fontsize=11, frameon=False,
215
+ handlelength=1.6, handleheight=1.6, labelspacing=0.9,
216
+ borderaxespad=0)
217
+ plt.tight_layout()
218
+ return fig
219
+
220
  def fig_confusion_matrix(matrix, oa, title, cmap='Blues'):
221
  """Generic 7Γ—7 confusion matrix plot: predicted classes vs Ground Truth."""
222
  short = CLASS_SHORT
 
410
 
411
  # ── Tab 1: LΓ©gende des classes ────────────────────────────────────
412
  with gr.Tab("Classes d'occupation du sol"):
413
+ gr.Markdown("## Classes d'occupation du sol")
414
+ gr.Plot(value=fig_class_legend(), show_label=False, container=False)
415
  gr.Markdown("""
416
  ---
417
  **Conseils d'interprΓ©tation :**