Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -36,7 +36,7 @@ def identify_image(image):
36
  score = top_5.values[i].item()
37
  results.append(f"{label}: {score:.2f}")
38
 
39
- return "\n".join(results)
40
 
41
  def analyze_image(image):
42
  overall_start_time = time.time()
@@ -62,15 +62,16 @@ def analyze_image(image):
62
  ax.set_ylim([0, 1])
63
  ax.axis('off')
64
 
65
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
66
- plt.savefig(temp_file, format='png', bbox_inches='tight', pad_inches=0)
67
- histogram_path = temp_file.name
68
  plt.close(fig)
69
  times['histogram'] = f"{time.time() - start_time:.3f} seconds"
70
 
71
  # 2. Edge detection
72
  start_time = time.time()
73
  edges = cv2.Canny(gray_image, 100, 200)
 
 
74
  times['edge_detection'] = f"{time.time() - start_time:.3f} seconds"
75
 
76
  # 3. Main color extraction (16 colors)
@@ -88,6 +89,8 @@ def analyze_image(image):
88
 
89
  # Create color palette
90
  color_palette = create_color_palette(colors)
 
 
91
  times['main_colors'] = f"{time.time() - start_time:.3f} seconds"
92
 
93
  # Calculate overall elapsed time
@@ -97,7 +100,7 @@ def analyze_image(image):
97
  # Prepare timing output
98
  timing_output = "\n".join([f"{k}: {v}" for k, v in times.items()])
99
 
100
- return top_5_classes, histogram_path, edges, color_palette, timing_output
101
 
102
  # Define Gradio interface
103
  interface = gr.Interface(
@@ -105,13 +108,14 @@ interface = gr.Interface(
105
  inputs=gr.Image(type="pil", label="Upload Image"),
106
  outputs=[
107
  gr.Textbox(label="Image Classification (Top 5)"),
108
- gr.Image(type="filepath", label="Histogram"),
109
- gr.Image(type="numpy", label="Edge Detection"),
110
- gr.Image(type="numpy", label="Main Colors Palette"),
111
  gr.Textbox(label="Processing Times")
112
  ],
113
  title="Advanced Image Analysis",
114
- description="Upload an image to analyze its classification, histogram, edges, and main colors."
 
115
  )
116
 
117
  # Launch the interface
 
36
  score = top_5.values[i].item()
37
  results.append(f"{label}: {score:.2f}")
38
 
39
+ return ", ".join(results)
40
 
41
  def analyze_image(image):
42
  overall_start_time = time.time()
 
62
  ax.set_ylim([0, 1])
63
  ax.axis('off')
64
 
65
+ histogram_path = "histogram.png"
66
+ plt.savefig(histogram_path, format='png', bbox_inches='tight', pad_inches=0)
 
67
  plt.close(fig)
68
  times['histogram'] = f"{time.time() - start_time:.3f} seconds"
69
 
70
  # 2. Edge detection
71
  start_time = time.time()
72
  edges = cv2.Canny(gray_image, 100, 200)
73
+ edges_path = "edges.png"
74
+ cv2.imwrite(edges_path, edges)
75
  times['edge_detection'] = f"{time.time() - start_time:.3f} seconds"
76
 
77
  # 3. Main color extraction (16 colors)
 
89
 
90
  # Create color palette
91
  color_palette = create_color_palette(colors)
92
+ palette_path = "palette.png"
93
+ cv2.imwrite(palette_path, color_palette)
94
  times['main_colors'] = f"{time.time() - start_time:.3f} seconds"
95
 
96
  # Calculate overall elapsed time
 
100
  # Prepare timing output
101
  timing_output = "\n".join([f"{k}: {v}" for k, v in times.items()])
102
 
103
+ return top_5_classes, histogram_path, edges_path, palette_path, timing_output
104
 
105
  # Define Gradio interface
106
  interface = gr.Interface(
 
108
  inputs=gr.Image(type="pil", label="Upload Image"),
109
  outputs=[
110
  gr.Textbox(label="Image Classification (Top 5)"),
111
+ gr.File(label="Histogram"),
112
+ gr.File(label="Edge Detection"),
113
+ gr.File(label="Main Colors Palette"),
114
  gr.Textbox(label="Processing Times")
115
  ],
116
  title="Advanced Image Analysis",
117
+ description="Upload an image to analyze its classification, histogram, edges, and main colors.",
118
+ allow_flagging="never"
119
  )
120
 
121
  # Launch the interface