reab5555 commited on
Commit
aff928e
·
verified ·
1 Parent(s): 01b0a0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -39
app.py CHANGED
@@ -266,45 +266,45 @@ def process_image_detection(image, target_label, surprise_rating):
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:
 
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: