You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Detectron2 Visualizer时标注多边形位置错误求助

建筑检测标注可视化异常问题(Detectron2)

我正在基于2000×2000像素、分辨率20cm的航拍PNG图像开展建筑检测任务,使用Detectron2工具。

Detectron2提供的get_balloon_dicts函数用于注册数据集标注,visualizer模块负责可视化标注。官方教程中的气球示例流程运行成功,我在Anaconda环境中复现该示例时,标注JSON文件能在气球图像上正确可视化。

但将相同流程应用到我的建筑图像与标注JSON文件时,图像可正常显示,但标注无法完整可视化,仅在图像顶部出现部分标签。由于我的建筑标注JSON文件的格式、结构和属性与气球示例类似,预期应得到正常的可视化效果。

以下是我从气球示例复制并修改的数据集注册代码:

from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.structures import BoxMode
import cv2
import os
import numpy as np

def get_building_dicts(img_dir):
    json_file = os.path.join(img_dir, "via_region_data.json")
    with open(json_file) as f:
        imgs_anns = json.load(f)

    dataset_dicts = []
    for idx, v in enumerate(imgs_anns.values()):
        record = {}
        
        filename = os.path.join(img_dir, v["filename"])
        height, width = cv2.imread(filename).shape[:2]

        record["file_name"] = filename
        record["image_id"] = idx
        record["height"] = height
        record["width"] = width
      
        annos = v["regions"]
        objs = []
        for _, anno in annos.items():
            assert not anno["region_attributes"]
            anno = anno["shape_attributes"]
            px = anno["all_points_x"]
            py = anno["all_points_y"]
            poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
            poly = [p for x in poly for p in x]

            obj = {
                "bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
                "bbox_mode": BoxMode.XYXY_ABS,
                "segmentation": [poly],
                "category_id": 0,
            }
            objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts

for d in ["train", "val"]:
    DatasetCatalog.register("building_" + d, lambda d=d: get_building_dicts("wisconsin_dataset2020/" + d))
    MetadataCatalog.get("building_" + d).set(thing_classes=["building"])
building_metadata = MetadataCatalog.get("building_train")

标注与图像可视化代码:

import random
import matplotlib.pyplot as plt

dataset_dicts = get_building_dicts("wisconsin_dataset2020/train")
for d in random.sample(dataset_dicts, 1):
    img = cv2.imread(d["file_name"])
    print(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1], metadata=building_metadata, scale=1.0)
    out = visualizer.draw_dataset_dict(d)
    plt.figure(figsize=(20, 20))
    plt.imshow(out.get_image()[:, :, ::-1])
    plt.show()

我已将图像样本和标注JSON文件打包,用于问题排查。恳请提供解决方案或代码改进建议,帮助我实现标注完整可视化的预期效果。


环境信息

  • sys.platform: win32
  • Python: 3.8.16 | packaged by conda-forge | (default, Feb 1 2023, 15:53:35) [MSC v.1929 64 bit (AMD64)]
  • numpy: 1.24.3
  • detectron2: 0.6
  • DETECTRON2_ENV_MODULE: 无
  • PyTorch: 2.0.1 @L:\projects\pythonlover\conda_projects\envs\detectron2gpu\lib\site-packages\torch
  • PyTorch debug build: False
  • torch._C._GLIBCXX_USE_CXX11_ABI: False
  • GPU available: Yes
  • GPU 0: Quadro RTX 5000 (arch=7.5)
  • Driver version: 522.06
  • CUDA_HOME: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
  • Pillow: 9.4.0
  • torchvision: 0.15.2 @L:\projects\pythonlover\conda_projects\envs\detectron2gpu\lib\site-packages\torchvision
  • torchvision arch flags: L:\projects\pythonlover\conda_projects\envs\detectron2gpu\lib\site-packages\torchvision_C.pyd; cannot find cuobjdump
  • fvcore: 0.1.5.post20221221
  • iopath: 0.1.9
  • cv2: 4.7.0

PyTorch编译信息

  • C++ Version: 199711
  • MSVC 193431937
  • Intel(R) Math Kernel Library Version 2020.0.2 Product Build 20200624 for Intel(R) 64 architecture applications
  • Intel(R) MKL-DNN v2.7.3 (Git Hash 6dbeffbae1f23cbbeae17adb7b5b13f1f37c080e)
  • OpenMP 2019
  • LAPACK is enabled (usually provided by MKL)
  • CPU capability usage: AVX2
  • CUDA Runtime 11.8
  • NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_90,code=sm_90;-gencode;arch=compute_37,code=compute_37
  • CuDNN 8.7
  • Magma 2.5.4
  • Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.8, CUDNN_VERSION=8.7.0, CXX_COMPILER=C:/cb/pytorch_1000000000000/work/tmp_bin/sccache-cl.exe, CXX_FLAGS=/DWIN32 /D_WINDOWS /GR /EHsc /w /bigobj /FS -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOCUPTI -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_DISABLE_GPU_ASSERTS=OFF, TORCH_VERSION=2.0.1, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=OFF, USE_NNPACK=OFF, USE_OPENMP=ON, USE_ROCM=OFF,

内容的提问来源于stack exchange,提问作者Alfa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 20:35:54