reab5555 commited on
Commit
2425fe6
·
verified ·
1 Parent(s): e1b87b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -174,8 +174,8 @@ def process_image_detection(image, target_label, surprise_rating):
174
  original_size = image.size
175
  print(f"Image size: {original_size}")
176
 
177
- # Calculate relative font size based on image dimensions
178
- base_fontsize = min(original_size) / 40
179
 
180
  print("Loading models...")
181
  owlv2_processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16")
@@ -191,8 +191,12 @@ def process_image_detection(image, target_label, surprise_rating):
191
  target_sizes = torch.tensor([image.size[::-1]]).to(device)
192
  results = owlv2_processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]
193
 
 
 
 
 
 
194
  dpi = 300
195
- figsize = (original_size[0] / dpi, original_size[1] / dpi)
196
  fig = plt.figure(figsize=figsize, dpi=dpi)
197
  ax = plt.Axes(fig, [0., 0., 1., 1.])
198
  fig.add_axes(ax)
@@ -246,22 +250,22 @@ def process_image_detection(image, target_label, surprise_rating):
246
  )
247
  ax.add_patch(rect)
248
 
 
249
  plt.text(
250
  box[0], box[1] - base_fontsize,
251
  f'{max_score:.2f}',
252
  color='red',
253
  fontsize=base_fontsize,
254
- fontweight='bold',
255
- bbox=dict(facecolor='white', alpha=0.7, edgecolor='none', pad=2)
256
  )
257
 
 
258
  plt.text(
259
  box[2] + base_fontsize / 2, box[1],
260
  f'Unexpected (Rating: {surprise_rating}/5)\n{target_label}',
261
  color='red',
262
  fontsize=base_fontsize,
263
  fontweight='bold',
264
- bbox=dict(facecolor='white', alpha=0.7, edgecolor='none', pad=2),
265
  verticalalignment='bottom'
266
  )
267
 
@@ -269,7 +273,6 @@ def process_image_detection(image, target_label, surprise_rating):
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',
@@ -285,16 +288,17 @@ def process_image_detection(image, target_label, surprise_rating):
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
 
@@ -302,8 +306,6 @@ def process_image_detection(image, target_label, surprise_rating):
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
  except Exception as e:
 
174
  original_size = image.size
175
  print(f"Image size: {original_size}")
176
 
177
+ # Calculate relative font size
178
+ base_fontsize = min(original_size) / 80
179
 
180
  print("Loading models...")
181
  owlv2_processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16")
 
191
  target_sizes = torch.tensor([image.size[::-1]]).to(device)
192
  results = owlv2_processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]
193
 
194
+ # Calculate aspect ratio and figure size
195
+ height, width = image.size[1], image.size[0]
196
+ aspect_ratio = width / height
197
+ figsize = (6 * aspect_ratio, 6)
198
+
199
  dpi = 300
 
200
  fig = plt.figure(figsize=figsize, dpi=dpi)
201
  ax = plt.Axes(fig, [0., 0., 1., 1.])
202
  fig.add_axes(ax)
 
250
  )
251
  ax.add_patch(rect)
252
 
253
+ # Add confidence score without background
254
  plt.text(
255
  box[0], box[1] - base_fontsize,
256
  f'{max_score:.2f}',
257
  color='red',
258
  fontsize=base_fontsize,
259
+ fontweight='bold'
 
260
  )
261
 
262
+ # Add label and rating without background
263
  plt.text(
264
  box[2] + base_fontsize / 2, box[1],
265
  f'Unexpected (Rating: {surprise_rating}/5)\n{target_label}',
266
  color='red',
267
  fontsize=base_fontsize,
268
  fontweight='bold',
 
269
  verticalalignment='bottom'
270
  )
271
 
 
273
 
274
  print("Saving final image...")
275
  try:
 
276
  buf = io.BytesIO()
277
  fig.savefig(buf,
278
  format='png',
 
288
  if output_image.mode != 'RGB':
289
  output_image = output_image.convert('RGB')
290
 
291
+ # Calculate new size preserving aspect ratio
292
+ target_width = original_size[0]
293
+ target_height = int(target_width / aspect_ratio)
294
+
295
+ # Resize maintaining aspect ratio
296
+ output_image = output_image.resize((target_width, target_height), Image.Resampling.LANCZOS)
297
 
 
298
  final_buf = io.BytesIO()
299
  output_image.save(final_buf, format='PNG', dpi=original_dpi)
300
  final_buf.seek(0)
301
 
 
302
  plt.close(fig)
303
  buf.close()
304
 
 
306
 
307
  except Exception as e:
308
  print(f"Save error details: {str(e)}")
 
 
309
  raise
310
 
311
  except Exception as e: