Spaces:
Sleeping
Sleeping
File size: 796 Bytes
2590409 3228ab0 2590409 3228ab0 2590409 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import matplotlib.pyplot as plt
import io
import base64
def plot_detection(image, detection_result):
plt.figure(figsize=(10, 10))
plt.imshow(image)
ax = plt.gca()
for score, label, box in zip(detection_result["scores"], detection_result["labels"], detection_result["boxes"]):
x1, y1, x2, y2 = box
width = x2 - x1
height = y2 - y1
rect = plt.Rectangle((x1, y1), width, height, fill=False, color='red', linewidth=2)
ax.add_patch(rect)
ax.text(x1, y1, f'{label}: {score:.2f}',
bbox=dict(facecolor='white', alpha=0.8))
plt.axis('off')
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
img_str = base64.b64encode(buf.getvalue()).decode()
return f"data:image/png;base64,{img_str}"
|