保存一行三列的可视化图像
(image, mask_all, gt_mask, thinking, question, save_dir="visualization_results")
| 3 | import matplotlib.pyplot as plt |
| 4 | |
| 5 | def visualize_result(image, mask_all, gt_mask, thinking, question, save_dir="visualization_results"): |
| 6 | """保存一行三列的可视化图像""" |
| 7 | os.makedirs(save_dir, exist_ok=True) |
| 8 | |
| 9 | # 创建一行三列的子图 |
| 10 | fig, axes = plt.subplots(1, 3, figsize=(15, 5)) |
| 11 | |
| 12 | # 第一列:原图 |
| 13 | axes[0].imshow(image) |
| 14 | axes[0].set_title('raw image', fontsize=14) |
| 15 | axes[0].axis('off') |
| 16 | |
| 17 | # 第二列:预测掩码叠加原图 |
| 18 | axes[1].imshow(image) |
| 19 | if mask_all.sum() > 0: |
| 20 | overlay = np.zeros_like(np.array(image)) |
| 21 | overlay[mask_all] = [255, 0, 0] # 红色 |
| 22 | axes[1].imshow(overlay, alpha=0.5) |
| 23 | axes[1].set_title('predicted mask', fontsize=14) |
| 24 | axes[1].axis('off') |
| 25 | |
| 26 | # 第三列:真实掩码叠加原图 |
| 27 | axes[2].imshow(image) |
| 28 | if gt_mask.sum() > 0: |
| 29 | overlay = np.zeros_like(np.array(image)) |
| 30 | overlay[gt_mask] = [0, 255, 0] # 绿色 |
| 31 | axes[2].imshow(overlay, alpha=0.5) |
| 32 | axes[2].set_title('ground truth mask', fontsize=14) |
| 33 | axes[2].axis('off') |
| 34 | |
| 35 | plt.tight_layout() |
| 36 | |
| 37 | # 保存图像 |
| 38 | filename = f"vis_{np.random.randint(10000, 99999)}.png" |
| 39 | save_path = os.path.join(save_dir, filename) |
| 40 | plt.savefig(save_path, dpi=150, bbox_inches='tight') |
| 41 | plt.close() |
| 42 | |
| 43 | # txt save thinking |
| 44 | with open(save_path.replace(".png", ".txt"), "w") as f: |
| 45 | f.write(f"Question: {question}\n") |
| 46 | f.write(f"Thinking: {thinking}") |
| 47 | |
| 48 | def visualize_result_with_bboxes_and_points(image, boxes, points, gt_bbox, thinking, question, save_dir="visualization_results"): |
| 49 | """保存一行三列的可视化图像""" |
nothing calls this directly
no outgoing calls
no test coverage detected