栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【程序】JSON文件:使用opencv对标注框bbox的单独可视化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【程序】JSON文件:使用opencv对标注框bbox的单独可视化

方法1: 用plt.imshow()显示

import json
import os, cv2
import matplotlib.pyplot as plt

train_json = './WIDER_train/aaa/trainset.json'
train_path = ''

def visualization_bbox1(num_image, json_path,img_path):# 需要画的第num副图片, 对应的json路径和图片路径
    with open(json_path) as annos:
        annotation_json = json.load(annos)

    print('the annotation_json num_key is:',len(annotation_json))  # 统计json文件的关键字长度
    print('the annotation_json key is:', annotation_json.keys()) # 读出json文件的关键字
    print('the annotation_json num_images is:', len(annotation_json['images'])) # json文件中包含的图片数量

    image_name = annotation_json['images'][num_image - 1]['file_name']  # 读取图片名
    id = annotation_json['images'][num_image - 1]['id']  # 读取图片id

    image_path = os.path.join(img_path, str(image_name).zfill(5)) # 拼接图像路径
    print(image_path)
    image = cv2.imread(image_path, 1)  # 保持原始格式的方式读取图像
    num_bbox = 0  # 统计一幅图片中bbox的数量

    for i in range(len(annotation_json['annotations'][::])):
        if  annotation_json['annotations'][i-1]['image_id'] == id:
            num_bbox = num_bbox + 1
            x, y, w, h = annotation_json['annotations'][i-1]['bbox']  # 读取边框
            image = cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 255), 2)

    print('The unm_bbox of the display image is:', num_bbox)

    # 显示方式1:用plt.imshow()显示
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) #绘制图像,将CV的BGR换成RGB
    plt.show() #显示图像



if __name__ == "__main__":
   visualization_bbox1(0, train_json, train_path)

方法2:用cv2.imshow()显示

import json
import os, cv2
import matplotlib.pyplot as plt

train_json = './WIDER_train/aaa/trainset.json'
train_path = ''

def visualization_bbox1(num_image, json_path,img_path):# 需要画的第num副图片, 对应的json路径和图片路径
    with open(json_path) as annos:
        annotation_json = json.load(annos)

    print('the annotation_json num_key is:',len(annotation_json))  # 统计json文件的关键字长度
    print('the annotation_json key is:', annotation_json.keys()) # 读出json文件的关键字
    print('the annotation_json num_images is:', len(annotation_json['images'])) # json文件中包含的图片数量

    image_name = annotation_json['images'][num_image - 1]['file_name']  # 读取图片名
    id = annotation_json['images'][num_image - 1]['id']  # 读取图片id

    image_path = os.path.join(img_path, str(image_name).zfill(5)) # 拼接图像路径
    print(image_path)
    image = cv2.imread(image_path, 1)  # 保持原始格式的方式读取图像
    num_bbox = 0  # 统计一幅图片中bbox的数量

    for i in range(len(annotation_json['annotations'][::])):
        if  annotation_json['annotations'][i-1]['image_id'] == id:
            num_bbox = num_bbox + 1
            x, y, w, h = annotation_json['annotations'][i-1]['bbox']  # 读取边框
            image = cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 255), 2)

    print('The unm_bbox of the display image is:', num_bbox)


    # 显示方式2:用cv2.imshow()显示
    cv2.namedWindow(image_name, 0)  # 创建窗口
    cv2.resizeWindow(image_name, 1000, 1000) # 创建500*500的窗口
    cv2.imshow(image_name, image)
    cv2.waitKey(0)

if __name__ == "__main__":
   visualization_bbox1(0, train_json, train_path)

json:

{
    "images": [
        {
            "file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_53.jpg",
            "height": 683,
            "width": 1024,
            "id": 0
        }
    ],
    "annotations": [
        {
            "segmentation": [
                [
                    421.1,
                    133.2,
                    445.5,
                    125.5,
                    432.6,
                    145.4,
                    432.3,
                    159.9,
                    450.6,
                    153.4
                ]
            ],
            "area": 4356.76,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                411.5,
                100.1,
                59.6,
                73.1
            ],
            "category_id": 1,
            "id": 0,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    537.6,
                    120.5,
                    546.8,
                    119.1,
                    541.2,
                    125.2,
                    539.9,
                    130.7,
                    547.1,
                    129.5
                ]
            ],
            "area": 585.66,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                534.4,
                110.9,
                22.7,
                25.8
            ],
            "category_id": 1,
            "id": 1,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    104.5,
                    152.8,
                    116.5,
                    150.9,
                    109.9,
                    158.4,
                    107.3,
                    164.1,
                    117.7,
                    162.5
                ]
            ],
            "area": 953.2800000000001,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                99.3,
                139.2,
                28.8,
                33.1
            ],
            "category_id": 1,
            "id": 2,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    823.0,
                    102.6,
                    832.1,
                    102.6,
                    826.2,
                    107.1,
                    823.6,
                    112.3,
                    830.9,
                    112.2
                ]
            ],
            "area": 497.96000000000004,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                819.0,
                95.6,
                21.1,
                23.6
            ],
            "category_id": 1,
            "id": 3,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    955.1,
                    94.6,
                    971.3,
                    96.7,
                    961.0,
                    106.2,
                    954.8,
                    111.8,
                    966.8,
                    113.3
                ]
            ],
            "area": 1760.5900000000001,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                945.4,
                76.2,
                37.7,
                46.7
            ],
            "category_id": 1,
            "id": 4,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    597.0,
                    121.9,
                    603.2,
                    121.7,
                    600.3,
                    125.3,
                    598.1,
                    127.7,
                    602.5,
                    127.5
                ]
            ],
            "area": 195.35999999999999,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                593.3,
                115.6,
                13.2,
                14.8
            ],
            "category_id": 1,
            "id": 5,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    756.2,
                    115.3,
                    762.5,
                    115.1,
                    759.5,
                    118.2,
                    757.2,
                    121.5,
                    762.1,
                    121.3
                ]
            ],
            "area": 221.1,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                752.5,
                108.7,
                13.4,
                16.5
            ],
            "category_id": 1,
            "id": 6,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    358.1,
                    117.4,
                    372.8,
                    116.2,
                    370.0,
                    123.7,
                    361.8,
                    134.2,
                    373.6,
                    133.4
                ]
            ],
            "area": 1874.5200000000002,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                340.3,
                96.7,
                38.1,
                49.2
            ],
            "category_id": 1,
            "id": 7,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    379.5,
                    129.0,
                    389.0,
                    129.0,
                    384.7,
                    133.6,
                    380.9,
                    138.7,
                    387.5,
                    138.7
                ]
            ],
            "area": 552.16,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                373.1,
                116.6,
                20.3,
                27.2
            ],
            "category_id": 1,
            "id": 8,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    709.0,
                    126.4,
                    713.6,
                    126.1,
                    711.4,
                    128.5,
                    709.7,
                    130.8,
                    713.6,
                    130.6
                ]
            ],
            "area": 124.23,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                706.5,
                121.5,
                10.1,
                12.3
            ],
            "category_id": 1,
            "id": 9,
            "ignore": 0
        },
        {
            "segmentation": [
                [
                    572.8,
                    126.0,
                    581.4,
                    126.0,
                    576.7,
                    131.5,
                    574.6,
                    134.5,
                    580.3,
                    134.4
                ]
            ],
            "area": 371.05,
            "iscrowd": 0,
            "image_id": 0,
            "bbox": [
                569.0,
                116.7,
                18.1,
                20.5
            ],
            "category_id": 1,
            "id": 10,
            "ignore": 0
        }
    ],
    "categories": [
        {
            "name": "background",
            "id": 0
        },
        {
            "name": "face",
            "id": 1
        }
    ]
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号