Giter Site home page Giter Site logo

yolo format! about nanodet HOT 6 CLOSED

rangilyu avatar rangilyu commented on May 18, 2024
yolo format!

from nanodet.

Comments (6)

wwdok avatar wwdok commented on May 18, 2024

i find a tool: https://github.com/surserrr/yolo_to_coco ,but not use yet.

from nanodet.

DCC-lzhy avatar DCC-lzhy commented on May 18, 2024

you can change it by yourself

from nanodet.

zengwb-lx avatar zengwb-lx commented on May 18, 2024

I wrote a script to convert coco to fit my own VOC data set

-- coding=utf-8 --

import json
import os
import cv2
import xml.etree.ElementTree as ET
import shutil

class2id = {'anchundanbopi1': 0, 'anchundanbopi': 0,
'aogan1': 1, 'aogan': 1,
'baigua': 2, 'baigua1': 2,
'boluo': 3, 'boluo1': 3,
}

从xml文件中提取bounding box信息, 格式为[[x_min, y_min, x_max, y_max, name]]

def parse_xml(xml_path):
tree = ET.parse(xml_path)
root = tree.getroot()
objs = root.findall('object')
coords = list()
for ix, obj in enumerate(objs):
name = obj.find('name').text
box = obj.find('bndbox')
x_min = int(box[0].text)
y_min = int(box[1].text)
x_max = int(box[2].text)
y_max = int(box[3].text)
coords.append([x_min, y_min, x_max, y_max, name])
return coords

def convert(root_path, source_xml_root_path, target_xml_root_path, phase='train', split=80000):
'''
root_path:
根路径,里面包含JPEGImages(图片文件夹),classes.txt(类别标签),以及annotations文件夹(如果没有则会自动创建,用于保存最后的json)
source_xml_root_path:
VOC xml文件存放的根目录
target_xml_root_path:
coco xml存放的根目录
phase:
状态:'train'或者'test'
split:
train和test图片的分界点数目
'''

dataset = {'categories': [], 'images': [], 'annotations': []}

# 打开类别标签
with open(os.path.join(root_path, 'classes.txt')) as f:
    classes = f.read().strip().split()

# 建立类别标签和数字id的对应关系
for i, cls in enumerate(classes, 1):
    dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'beverage'})  # mark
print(dataset['categories'])
# 读取images文件夹的图片名称
pics = [f for f in os.listdir(os.path.join(root_path, 'images'))]

# 判断是建立训练集还是验证集
if phase == 'train':
    pics = [line for i, line in enumerate(pics) if i <= split]
elif phase == 'val':
    pics = [line for i, line in enumerate(pics) if i > split]

print('---------------- start convert ---------------')
bnd_id = 1  # 初始为1
for i, pic in enumerate(pics):
    # print('pic  '+str(i+1)+'/'+str(len(pics)))
    xml_path = os.path.join(source_xml_root_path, pic[:-4] + '.xml')
    pic_path = os.path.join(root_path, 'images/' + pic)
    # 用opencv读取图片,得到图像的宽和高
    print('1')
    try:
        im = cv2.imread(pic_path)
    except IOError:
        print(pic_path)

    height, width, _ = im.shape
    # 添加图像的信息到dataset中
    dataset['images'].append({'file_name': pic,
                              'id': i,
                              'width': width,
                              'height': height})
    try:
        coords = parse_xml(xml_path)
    except:
        print(pic[:-4] + '.xml not exists~')
        continue
    for coord in coords:
        # x_min
        x1 = int(coord[0]) - 1
        x1 = max(x1, 0)
        # y_min
        y1 = int(coord[1]) - 1
        y1 = max(y1, 0)
        # x_max
        x2 = int(coord[2])
        # y_max
        y2 = int(coord[3])
        assert x1 < x2
        assert y1 < y2
        # name
        name = coord[4]
        # cls_id = classes.index(name) + 1  # 从1开始
        cls_id = class2id[name] + 1
        width = max(0, x2 - x1)
        height = max(0, y2 - y1)
        dataset['annotations'].append({
            'area': width * height,
            'bbox': [x1, y1, width, height],
            'category_id': int(cls_id),
            'id': bnd_id,
            'image_id': i,
            'iscrowd': 0,
            # mask, 矩形是从左上角点按顺时针的四个顶点
            'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
        })
        bnd_id += 1

# 保存结果的文件夹
folder = os.path.join(target_xml_root_path, 'annotations')
if os.path.exists(folder):
    shutil.rmtree(folder)
os.makedirs(folder)
json_name = os.path.join(target_xml_root_path, 'annotations/instances_{}2014.json'.format(phase))
with open(json_name, 'w') as f:
    json.dump(dataset, f)

if name == 'main':
convert(root_path='./fruit_dataset/coco_fruit/',
source_xml_root_path='.coco_fruit/Annotations',
target_xml_root_path='./coco_fruit/coco_data')

from nanodet.

leozicai avatar leozicai commented on May 18, 2024

https://github.com/yukkyo/voc2coco , this repository is good, and I also made a little adjustment to it in its issue

from nanodet.

eeric avatar eeric commented on May 18, 2024

labels: txt-->xml-->json
This project support to Nanodet project to make official labels.
https://github.com/eeric/yolo2voc2coco

successfully, as following:
[root][12-01 02:42:11]INFO:val|Epoch2/70|Iter123(4940/4952)| lr:1.40e-01| loss_qfl:0.6564| loss_bbox:0.9067| loss_dfl:0.3167|
[root][12-01 02:42:11]INFO:val|Epoch2/70|Iter123(4942/4952)| lr:1.40e-01| loss_qfl:0.3882| loss_bbox:0.7180| loss_dfl:0.2757|
[root][12-01 02:42:12]INFO:val|Epoch2/70|Iter123(4944/4952)| lr:1.40e-01| loss_qfl:0.2997| loss_bbox:0.7028| loss_dfl:0.2815|
[root][12-01 02:42:12]INFO:val|Epoch2/70|Iter123(4946/4952)| lr:1.40e-01| loss_qfl:0.4795| loss_bbox:0.5486| loss_dfl:0.2645|
[root][12-01 02:42:13]INFO:val|Epoch2/70|Iter123(4948/4952)| lr:1.40e-01| loss_qfl:0.4833| loss_bbox:0.8868| loss_dfl:0.2684|
[root][12-01 02:42:14]INFO:val|Epoch2/70|Iter123(4950/4952)| lr:1.40e-01| loss_qfl:0.5885| loss_bbox:0.7382| loss_dfl:0.3275|
Loading and preparing results...
DONE (t=3.01s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type bbox
Loading and preparing results...
DONE (t=2.72s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type bbox
DONE (t=46.62s).
Accumulating evaluation results...
DONE (t=13.37s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.036
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.074
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.031
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.002
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.019
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.061
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.097

from nanodet.

jerryWTMH avatar jerryWTMH commented on May 18, 2024

I also wrote a script to convert yolo(txt) to coco(json)
The only thing you need to do is that change the content of the line with #####
Hope to help the one who needs it.
https://github.com/jerryWTMH/yolo2coco

import os
import numpy as np
import json
import cv2

id_counter = 0 # To record the id
FILE_PATH = 'put your image folder path here!' #####
out = {'annotations': [], 
           'categories': [{"id": 1, "name": "cricoid_cartilage", "supercategory": ""}, {"id": 2, "name": "thyroid_cartilage", "supercategory": ""}], ##### change the categories to match your dataset!
           'images': [],
           'info': {"contributor": "", "year": "", "version": "", "url": "", "description": "", "date_created": ""},
           'licenses': {"id": 0, "name": "", "url": ""}
           }

def annotations_data(whole_path , image_id):
    # id, bbox, iscrowd, image_id, category_id
    global id_counter
    txt = open(whole_path,'r')
    for line in txt.readlines(): # if txt.readlines is null, this for loop would not run
        data = line.strip()
        data = data.split() 
        # convert the center into the top-left point!
        data[1] = float(data[1])* 800 - 0.5 * float(data[3])* 800 ##### change the 800 to your raw image width
        data[2] = float(data[2])* 600 - 0.5 * float(data[4])* 600 ##### change the 600 to your raw image height
        data[3] = float(data[3])* 800 ##### change the 800 to your raw image width
        data[4] = float(data[4])* 600 ##### change the 600 to your raw image height
        bbox = [data[1],data[2],data[3],data[4]]
        ann = {'id': id_counter,
            'bbox': bbox,
            'area': data[3] * data[4],
            'iscrowd': 0,
            'image_id': int(image_id),
            'category_id': int(data[0]) + 1            
        }
        out['annotations'].append(ann)
        id_counter = id_counter + 1 

def images_data(file_name):
    #id, height, width, file_name
    id = file_name.split('.')[0]
    file_name = id + '.jpg' ##### change '.jpg' to other image formats if the format of your image is not .jpg
    imgs = {'id': int(id),
            'height': 600, ##### change the 600 to your raw image height
            'width': 800, ##### change the 800 to your raw image width
            'file_name': file_name,
            "coco_url": "", 
            "flickr_url": "", 
            "date_captured": 0, 
            "license": 0
    }
    out['images'].append(imgs)
           
    
            

if __name__ == '__main__':
    files = os.listdir(FILE_PATH)
    files.sort()
    for file in files:
        whole_path = os.path.join(FILE_PATH,file)
        annotations_data(whole_path, file.split('.')[0])
        images_data(file)

    
    with open('instances_merge_train.json', 'w') as outfile: ##### change the str to the json file name you want
      json.dump(out, outfile, separators=(',', ':'))

from nanodet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.