reab5555 commited on
Commit
5fbcf60
·
verified ·
1 Parent(s): 755e5aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
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
- buf = io.BytesIO()
272
- plt.savefig(buf,
273
- format='png',
274
- dpi=dpi,
275
- bbox_inches='tight',
276
- pad_inches=0,
277
- metadata={'dpi': original_dpi})
278
- buf.seek(0)
279
- plt.close()
280
-
281
- output_image = Image.open(buf)
282
- output_image = output_image.resize(original_size, Image.Resampling.LANCZOS)
283
-
284
- final_buf = io.BytesIO()
285
- output_image.save(final_buf, format='PNG', dpi=original_dpi)
286
- final_buf.seek(0)
287
-
288
- return final_buf
289
-
290
- except Exception as e:
291
- print(f"Process image detection error: {str(e)}") # Debug print
292
- print(f"Error occurred at line {e.__traceback__.tb_lineno}") # Debug print
293
- raise
 
 
 
 
 
 
 
 
 
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):