import json import re import argparse from pathlib import Path from PIL import Image, ImageDraw GRID_SIZE = 1000 def load_json(path): with open(path, "r") as f: return json.load(f) def extract_ref_boxes(prompt): matches = re.findall(r"", prompt) return [[int(x1), int(y1), int(x2), int(y2)] for x1, y1, x2, y2 in matches] def grid_to_pixel(boxes, img_w, img_h): """将 1000-grid 坐标转为像素坐标""" return [[ int(x1 * img_w / GRID_SIZE), int(y1 * img_h / GRID_SIZE), int(x2 * img_w / GRID_SIZE), int(y2 * img_h / GRID_SIZE), ] for x1, y1, x2, y2 in boxes] def draw_scaled_boxes(draw, boxes, off_x, off_y, scale, color, width=3): for x1, y1, x2, y2 in boxes: draw.rectangle([ off_x + x1 * scale, off_y + y1 * scale, off_x + x2 * scale, off_y + y2 * scale, ], outline=color, width=width) def visualize_single(data_path, output_path, max_images=None, cols=4): data = load_json(data_path) if max_images: data = data[:max_images] n = len(data) rows = (n + cols - 1) // cols images = [] for item in data: img = Image.open(item["image"]).convert("RGB") images.append(img) max_w = max(im.width for im in images) max_h = max(im.height for im in images) header_h = 60 cell_pad = 8 canvas_w = cols * (max_w + cell_pad) + cell_pad canvas_h = rows * (max_h + cell_pad) + cell_pad + header_h canvas = Image.new("RGB", (canvas_w, canvas_h), (240, 240, 240)) draw = ImageDraw.Draw(canvas) legend_x = cell_pad legend_y = cell_pad draw.rectangle([legend_x, legend_y, legend_x + 20, legend_y + 12], fill=None, outline=(0, 200, 0), width=3) draw.text((legend_x + 26, legend_y - 1), "GT", fill=(0, 0, 0)) draw.rectangle([legend_x + 80, legend_y, legend_x + 100, legend_y + 12], fill=None, outline=(220, 20, 20), width=3) draw.text((legend_x + 106, legend_y - 1), "Pred", fill=(0, 0, 0)) draw.rectangle([legend_x + 160, legend_y, legend_x + 180, legend_y + 12], fill=None, outline=(0, 120, 220), width=3) draw.text((legend_x + 186, legend_y - 1), "Ref", fill=(0, 0, 0)) draw.text((legend_x + 240, legend_y - 1), f"Total: {n} images", fill=(0, 0, 0)) for idx, (item, img) in enumerate(zip(data, images)): r = idx // cols c = idx % cols ox = cell_pad + c * (max_w + cell_pad) oy = header_h + cell_pad + r * (max_h + cell_pad) scale = min(max_w / img.width, max_h / img.height) new_w = int(img.width * scale) new_h = int(img.height * scale) img_resized = img.resize((new_w, new_h)) off_x = ox + (max_w - new_w) // 2 off_y = oy + (max_h - new_h) // 2 canvas.paste(img_resized, (off_x, off_y)) gt_boxes = grid_to_pixel(item.get("gt_bboxes", []), img.width, img.height) pred_boxes = grid_to_pixel(item.get("pred_bboxes", []), img.width, img.height) ref_boxes = grid_to_pixel(extract_ref_boxes(item.get("prompt", "")), img.width, img.height) draw_scaled_boxes(draw, ref_boxes, off_x, off_y, scale, (0, 120, 220), width=2) draw_scaled_boxes(draw, gt_boxes, off_x, off_y, scale, (0, 200, 0), width=3) draw_scaled_boxes(draw, pred_boxes, off_x, off_y, scale, (220, 20, 20), width=3) fname = Path(item["image"]).name stats = f"GT:{len(gt_boxes)} Pred:{len(pred_boxes)}" draw.text((ox + 4, oy + max_h - 18), f"{fname} {stats}", fill=(0, 0, 0)) canvas.save(output_path) print(f"保存到: {output_path}") def main(): parser = argparse.ArgumentParser(description="可视化 GT 和 Pred bboxes") parser.add_argument("--input", "-i", required=True, help="简化结果 JSON 路径") parser.add_argument("--output", "-o", default="contact_sheet.jpg", help="输出图片路径") parser.add_argument("--max", "-n", type=int, default=0, help="最多可视化几张 (0=全部)") parser.add_argument("--cols", type=int, default=4, help="每行列数") args = parser.parse_args() visualize_single( args.input, args.output, max_images=args.max if args.max > 0 else None, cols=args.cols, ) if __name__ == "__main__": main()