0 Star 1 Fork 0

gisfanmachel/mmdet-目标检测

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
balloon2coco.py 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
王龙 提交于 2024-06-06 22:41 . 提交代码
"""
#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
@Project :mmdetection-main
@File :balloon2coco.py
@IDE :PyCharm
@Author :chenxw
@Date :2023/10/20 14:54
@Descr:
"""
import os.path as osp
import mmcv
from mmengine.fileio import dump, load
from mmengine.utils import track_iter_progress
def convert_balloon_to_coco(ann_file, out_file, image_prefix):
data_infos = load(ann_file)
annotations = []
images = []
obj_count = 0
# for idx, v in enumerate(track_iter_progress(data_infos.values())):
for idx, v in enumerate(data_infos.values()):
filename = v['filename']
img_path = osp.join(image_prefix, filename)
height, width = mmcv.imread(img_path).shape[:2]
images.append(
dict(id=idx, file_name=filename, height=height, width=width))
for _, obj in v['regions'].items():
assert not obj['region_attributes']
obj = obj['shape_attributes']
px = obj['all_points_x']
py = obj['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]
x_min, y_min, x_max, y_max = (min(px), min(py), max(px), max(py))
data_anno = dict(
image_id=idx,
id=obj_count,
category_id=0,
bbox=[x_min, y_min, x_max - x_min, y_max - y_min],
area=(x_max - x_min) * (y_max - y_min),
segmentation=[poly],
iscrowd=0)
annotations.append(data_anno)
obj_count += 1
coco_format_json = dict(
images=images,
annotations=annotations,
categories=[{
'id': 0,
'name': 'balloon'
}])
dump(coco_format_json, out_file)
if __name__ == '__main__':
convert_balloon_to_coco(ann_file='data/balloon/train/via_region_data.json',
out_file='data/balloon/train/annotation_coco.json',
image_prefix='data/balloon/train')
convert_balloon_to_coco(ann_file='data/balloon/val/via_region_data.json',
out_file='data/balloon/val/annotation_coco.json',
image_prefix='data/balloon/val')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gisfanmachel/mmdet-object-detection.git
git@gitee.com:gisfanmachel/mmdet-object-detection.git
gisfanmachel
mmdet-object-detection
mmdet-目标检测
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385