#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Regenerate debug_clusters.png with the new split bubbles.json """ import json import cv2 import numpy as np def quad_bbox(quad): """Convert quad to bounding box""" xs = [p[0] for p in quad] ys = [p[1] for p in quad] return (min(xs), min(ys), max(xs), max(ys)) def save_debug_clusters_from_json( image_path="004.png", bubbles_path="bubbles.json", out_path="debug_clusters.png" ): img = cv2.imread(image_path) if img is None: print(f"❌ Cannot load image: {image_path}") return # Load bubbles.json with open(bubbles_path, "r", encoding="utf-8") as f: bubbles_data = json.load(f) # Draw all quad polygons in white (erasing original text) for bid_str, bubble_info in bubbles_data.items(): for quad in bubble_info.get("quads", []): pts = np.array(quad, dtype=np.int32) cv2.fillPoly(img, [pts], (255, 255, 255)) cv2.polylines(img, [pts], True, (180, 180, 180), 1) # Draw bounding boxes with labels for bid_str, bubble_info in bubbles_data.items(): bid = int(bid_str) x = bubble_info["x"] y = bubble_info["y"] w = bubble_info["w"] h = bubble_info["h"] x2 = x + w y2 = y + h cv2.rectangle(img, (x, y), (x2, y2), (0, 220, 0), 2) cv2.putText(img, f"BOX#{bid}", (x + 2, max(15, y + 16)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 220, 0), 2) cv2.imwrite(out_path, img) print(f"✅ Saved: {out_path}") if __name__ == "__main__": save_debug_clusters_from_json()