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}"