Yohan Runhaar commited on
Commit
e91cfe6
Β·
1 Parent(s): 69ae580

Replace with pie chart

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -83,22 +83,30 @@ def compute_class_areas(predictions, image_shape):
83
  return class_areas
84
 
85
 
86
- def generate_coverage_graph(class_areas):
87
  """
88
- Generates a graph for class coverage percentages.
89
  """
 
 
 
 
 
 
90
  plt.figure(figsize=(8, 6))
91
- classes = list(class_areas.keys())
92
- coverage = list(class_areas.values())
93
- plt.bar(classes, coverage, color="skyblue")
94
- plt.xlabel("Class")
95
- plt.ylabel("Coverage Percentage")
96
- plt.title("Class Coverage as % of Total Image")
97
- plt.ylim(0, 100) # Always show the Y-axis up to 100%
98
- graph_path = "class_coverage.png"
99
- plt.savefig(graph_path)
 
 
100
  plt.close()
101
- return graph_path
102
 
103
 
104
  def coral_ai_inference(image, model_name):
@@ -115,12 +123,12 @@ def coral_ai_inference(image, model_name):
115
 
116
  # Generate class coverage
117
  class_areas = compute_class_areas(results[0], image.shape[:2])
118
- graph_path = generate_coverage_graph(class_areas)
119
 
120
  # Render the prediction
121
  rendered_image = results[0].plot()
122
 
123
- return rendered_image, graph_path
124
 
125
 
126
  # Dynamically generate Gradio examples
@@ -143,7 +151,7 @@ inputs = [
143
 
144
  outputs = [
145
  gr.Image(type="numpy", label="Segmented Image"),
146
- gr.Image(type="filepath", label="Class Coverage Graph"),
147
  ]
148
 
149
  # Icons with links
 
83
  return class_areas
84
 
85
 
86
+ def generate_coverage_pie_chart(class_areas):
87
  """
88
+ Generates a pie chart for class coverage percentages.
89
  """
90
+ total_percentage = sum(class_areas.values())
91
+ other_percentage = max(0, 100 - total_percentage)
92
+
93
+ labels = list(class_areas.keys()) + (["Other"] if other_percentage > 0 else [])
94
+ sizes = list(class_areas.values()) + ([other_percentage] if other_percentage > 0 else [])
95
+
96
  plt.figure(figsize=(8, 6))
97
+ plt.pie(
98
+ sizes,
99
+ labels=[f"{label} ({size:.1f}%)" for label, size in zip(labels, sizes)],
100
+ autopct="%1.1f%%",
101
+ startangle=90,
102
+ colors=plt.cm.tab20.colors
103
+ )
104
+ plt.axis("equal")
105
+ plt.title("Class Coverage Distribution")
106
+ chart_path = "class_coverage_pie.png"
107
+ plt.savefig(chart_path)
108
  plt.close()
109
+ return chart_path
110
 
111
 
112
  def coral_ai_inference(image, model_name):
 
123
 
124
  # Generate class coverage
125
  class_areas = compute_class_areas(results[0], image.shape[:2])
126
+ pie_chart_path = generate_coverage_pie_chart(class_areas)
127
 
128
  # Render the prediction
129
  rendered_image = results[0].plot()
130
 
131
+ return rendered_image, pie_chart_path
132
 
133
 
134
  # Dynamically generate Gradio examples
 
151
 
152
  outputs = [
153
  gr.Image(type="numpy", label="Segmented Image"),
154
+ gr.Image(type="filepath", label="Class Coverage Pie Chart"),
155
  ]
156
 
157
  # Icons with links