Spaces:
Sleeping
Sleeping
| 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}" | |