COCO数据集训练格式转换成YOLO格式

更新时间:2023-06-23 06:45:58 阅读: 评论:0

COCO数据集训练格式转换成YOLO格式
COCO转YOLO:
neway⽐如coco2017train或coco2017val数据集中标注的⽬标(类别)位置在 Annotations 中以 (x, y, width, height) 来进⾏表⽰,x,y表⽰bbox左上⾓位置,width, height表⽰bbox的宽和⾼。⽽YOLO训练或者进⾏验证的时候读取的标注格式是以 (xmin, ymin, xmax, ymax)来进⾏表⽰,xmin, ymin表⽰bbox左上⾓位置, xmax, ymax表⽰bbox右下⾓位置。
所以将COCO标注格式(.json)转换成YOLO格式(.txt)并存储,⽐如转换val数据集的格式,其代码如下:
```python
import json
triplekillfrom collections import defaultdict
pillow是什么意思"""hyper parameters"""
unpack
json_file_path = '/home/wsy/data/coco2014/annotations/instances_val2014.json' images_dir_path = '/home/wsy/data/coco2014/val2014/'
output_path = '/home/wsy/code/pytorch-YOLOv4/'
"""load json file"""
name_box_id = defaultdict(list)
id_name = dict()
with open(json_file_path, encoding='utf-8') as f:
data = json.load(f)美国宣布对俄新制裁
annotations = data['annotations']
for ant in annotations:
id = ant['image_id']
name = '/home/wsy/data/coco2014/val2014/COCO_val2014_%012d.jpg' % id    cat = ant['category_id']
if cat >= 1 and cat <= 11:
意思是什么意思
cat = cat - 1
elif cat >= 13 and cat <= 25:
screen
cat = cat - 2
elif cat >= 27 and cat <= 28:
cat = cat - 3
elif cat >= 31 and cat <= 44:
cat = cat - 5
elif cat >= 46 and cat <= 65:
cat = cat - 6
elif cat == 67:
cat = cat - 7
elif cat == 70:
cat = cat - 9
elif cat >= 72 and cat <= 82:
cat = cat - 10
elif cat >= 84 and cat <= 90:
cat = cat - 11
name_box_id[name].append([ant['bbox'], cat])泉州英语培训
"""write to txt"""
with open(output_path, 'w') as f:
for key in name_box_id.keys():
f.write(key)
box_infos = name_box_id[key]劝说英文
for info in box_infos:
x_min = int(info[0][0])
quarterdollary_min = int(info[0][1])
x_max = x_min + int(info[0][2])
y_max = y_min + int(info[0][3])
box_info = " %d,%d,%d,%d,%d" % (
x_min, y_min, x_max, y_max, int(info[1]))
f.write(box_info)
f.write('\n')

本文发布于:2023-06-23 06:45:58,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/154554.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:格式   宣布   数据   标注
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图