Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -266,44 +266,45 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
| 266 |
)
|
| 267 |
|
| 268 |
plt.axis('off')
|
| 269 |
-
|
| 270 |
-
|
| 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 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
|
|
|
| 307 |
|
| 308 |
def process_and_analyze(image):
|
| 309 |
if image is None:
|
|
|
|
| 266 |
)
|
| 267 |
|
| 268 |
plt.axis('off')
|
| 269 |
+
|
| 270 |
+
print("Saving final image...")
|
| 271 |
+
try:
|
| 272 |
+
# Save directly to buffer using savefig
|
| 273 |
+
buf = io.BytesIO()
|
| 274 |
+
fig.savefig(buf,
|
| 275 |
+
format='png',
|
| 276 |
+
dpi=dpi,
|
| 277 |
+
bbox_inches='tight',
|
| 278 |
+
pad_inches=0)
|
| 279 |
+
buf.seek(0)
|
| 280 |
+
|
| 281 |
+
# Open as PIL Image
|
| 282 |
+
output_image = Image.open(buf)
|
| 283 |
+
|
| 284 |
+
# Convert to RGB if needed
|
| 285 |
+
if output_image.mode != 'RGB':
|
| 286 |
+
output_image = output_image.convert('RGB')
|
| 287 |
+
|
| 288 |
+
# Resize to original size if needed
|
| 289 |
+
if output_image.size != original_size:
|
| 290 |
+
output_image = output_image.resize(original_size, Image.Resampling.LANCZOS)
|
| 291 |
+
|
| 292 |
+
# Save to final buffer
|
| 293 |
+
final_buf = io.BytesIO()
|
| 294 |
+
output_image.save(final_buf, format='PNG', dpi=original_dpi)
|
| 295 |
+
final_buf.seek(0)
|
| 296 |
+
|
| 297 |
+
# Cleanup
|
| 298 |
+
plt.close(fig)
|
| 299 |
+
buf.close()
|
| 300 |
+
|
| 301 |
+
return final_buf
|
| 302 |
+
|
| 303 |
+
except Exception as e:
|
| 304 |
+
print(f"Save error details: {str(e)}")
|
| 305 |
+
print(f"Figure type: {type(fig)}")
|
| 306 |
+
print(f"Canvas type: {type(fig.canvas)}")
|
| 307 |
+
raise
|
| 308 |
|
| 309 |
def process_and_analyze(image):
|
| 310 |
if image is None:
|