Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -191,12 +191,11 @@ 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 |
-
#
|
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,7 +249,7 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
250 |
)
|
251 |
ax.add_patch(rect)
|
252 |
|
253 |
-
#
|
254 |
plt.text(
|
255 |
box[0], box[1] - base_fontsize,
|
256 |
f'{max_score:.2f}',
|
@@ -259,26 +258,16 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
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 |
-
|
272 |
plt.axis('off')
|
273 |
|
274 |
print("Saving final image...")
|
275 |
try:
|
276 |
buf = io.BytesIO()
|
277 |
fig.savefig(buf,
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
buf.seek(0)
|
283 |
|
284 |
# Open as PIL Image
|
@@ -288,13 +277,7 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
288 |
if output_image.mode != 'RGB':
|
289 |
output_image = output_image.convert('RGB')
|
290 |
|
291 |
-
#
|
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)
|
|
|
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 |
+
# Use original image dimensions for figure size
|
|
|
|
|
|
|
|
|
195 |
dpi = 300
|
196 |
+
width, height = image.size
|
197 |
+
figsize = (width / dpi, height / dpi)
|
198 |
+
|
199 |
fig = plt.figure(figsize=figsize, dpi=dpi)
|
200 |
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
201 |
fig.add_axes(ax)
|
|
|
249 |
)
|
250 |
ax.add_patch(rect)
|
251 |
|
252 |
+
# Only add the probability score
|
253 |
plt.text(
|
254 |
box[0], box[1] - base_fontsize,
|
255 |
f'{max_score:.2f}',
|
|
|
258 |
fontweight='bold'
|
259 |
)
|
260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
plt.axis('off')
|
262 |
|
263 |
print("Saving final image...")
|
264 |
try:
|
265 |
buf = io.BytesIO()
|
266 |
fig.savefig(buf,
|
267 |
+
format='png',
|
268 |
+
dpi=dpi,
|
269 |
+
bbox_inches='tight',
|
270 |
+
pad_inches=0)
|
271 |
buf.seek(0)
|
272 |
|
273 |
# Open as PIL Image
|
|
|
277 |
if output_image.mode != 'RGB':
|
278 |
output_image = output_image.convert('RGB')
|
279 |
|
280 |
+
# Save to final buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
final_buf = io.BytesIO()
|
282 |
output_image.save(final_buf, format='PNG', dpi=original_dpi)
|
283 |
final_buf.seek(0)
|