Spaces:
Runtime error
Runtime error
File size: 721 Bytes
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 |
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"]):
x, y, w, h = box
rect = plt.Rectangle((x, y), w-x, h-y, fill=False, color='red')
ax.add_patch(rect)
ax.text(x, y, 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}"
|