Update app.py
Browse files
app.py
CHANGED
@@ -266,31 +266,40 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
266 |
)
|
267 |
|
268 |
plt.axis('off')
|
269 |
-
|
270 |
print("Saving final image...") # Debug print
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
|
296 |
def process_and_analyze(image):
|
|
|
266 |
)
|
267 |
|
268 |
plt.axis('off')
|
269 |
+
|
270 |
print("Saving final image...") # Debug print
|
271 |
+
try:
|
272 |
+
# Save to buffer
|
273 |
+
buf = io.BytesIO()
|
274 |
+
|
275 |
+
# Force figure to be in a format we can save
|
276 |
+
fig.canvas.draw()
|
277 |
+
|
278 |
+
# Get the image data from the figure
|
279 |
+
plot_data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
280 |
+
plot_data = plot_data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
281 |
+
|
282 |
+
# Convert to PIL Image
|
283 |
+
output_image = Image.fromarray(plot_data)
|
284 |
+
|
285 |
+
# Resize if needed
|
286 |
+
output_image = output_image.resize(original_size, Image.Resampling.LANCZOS)
|
287 |
+
|
288 |
+
# Save to final buffer
|
289 |
+
final_buf = io.BytesIO()
|
290 |
+
output_image.save(final_buf, format='PNG', dpi=original_dpi)
|
291 |
+
final_buf.seek(0)
|
292 |
+
|
293 |
+
# Cleanup
|
294 |
+
plt.close(fig)
|
295 |
+
|
296 |
+
return final_buf
|
297 |
+
|
298 |
+
except Exception as e:
|
299 |
+
print(f"Save error details: {str(e)}") # Debug print
|
300 |
+
print(f"Figure type: {type(fig)}")
|
301 |
+
print(f"Canvas type: {type(fig.canvas)}")
|
302 |
+
raise
|
303 |
|
304 |
|
305 |
def process_and_analyze(image):
|