COCO转YoloV5数据集,适用于YoloV5、ScaledYoloV4

简介: COCO转YoloV5数据集,适用于YoloV5、ScaledYoloV4
import json
import glob
import os
import shutil
from pathlib import Path
import numpy as np
from tqdm import tqdm
def make_folders(path='../out/'):
    # Create folders
    if os.path.exists(path):
        shutil.rmtree(path)  # delete output folder
    os.makedirs(path)  # make new output folder
    os.makedirs(path + os.sep + 'labels')  # make new labels folder
    os.makedirs(path + os.sep + 'images')  # make new labels folder
    return path
def convert_coco_json(json_dir='../coco/annotations/'):
    dir = make_folders(path='out/')  # output directory
    jsons = glob.glob(json_dir + '*.json')
    coco80 = coco91_to_coco80_class()
    # Import json
    for json_file in sorted(jsons):
        fn = 'out/labels/%s/' % Path(json_file).stem.replace('instances_', '')  # folder name
        os.mkdir(fn)
        with open(json_file) as f:
            data = json.load(f)
        # Create image dict
        images = {'%g' % x['id']: x for x in data['images']}
        # Write labels file
        for x in tqdm(data['annotations'], desc='Annotations %s' % json_file):
            if x['iscrowd']:
                continue
            img = images['%g' % x['image_id']]
            h, w, f = img['height'], img['width'], img['file_name']
            # The Labelbox bounding box format is [top left x, top left y, width, height]
            box = np.array(x['bbox'], dtype=np.float64)
            box[:2] += box[2:] / 2  # xy top-left corner to center
            box[[0, 2]] /= w  # normalize x
            box[[1, 3]] /= h  # normalize y
            if (box[2] > 0.) and (box[3] > 0.):  # if w > 0 and h > 0
                with open(fn + Path(f).stem + '.txt', 'a') as file:
                    file.write('%g %.6f %.6f %.6f %.6f\n' % (coco80[x['category_id'] - 1], *box))
def coco91_to_coco80_class():  # converts 80-index (val2014) to 91-index (paper)
    # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
    # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n')
    # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n')
    # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)]  # darknet to coco
    # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)]  # coco to darknet
    x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, None, 24, 25, None,
         None, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, None, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
         51, 52, 53, 54, 55, 56, 57, 58, 59, None, 60, None, None, 61, None, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
         None, 73, 74, 75, 76, 77, 78, 79, None]
    return x
convert_coco_json()


目录
相关文章
|
存储 缓存 分布式计算
Spark的Driver和Executor
Spark的Driver和Executor
1477 0
|
1月前
|
机器学习/深度学习 人工智能 边缘计算
2026 CAD图纸AI智能识别:如何实现图例自动统计与参数一体化管控?
本文详解2026年CAD图纸智能识别技术:融合多模态AI与工程大模型,实现秒级图例识别(准确率>95%)、多格式解析、结构化数据流转、参数统一管控及智能校核对比,助力设计提效、杜绝返工。(239字)
316 0
|
2月前
|
人工智能 搜索推荐 程序员
最差实习生翻身:零代码用AI做健身App,6个月做到月入1.4万美金
本文是「OPC一人公司通关手册」第31篇,真实拆解一位零代码金融毕业生6个月打造AI健身App的案例:单人+AI编程助手,月入1.4万美金。核心在于——用AI Agent替代真人教练、年费硬付费墙($24.99/年)、创始人IP自拍短视频获客。不灌鸡汤,只讲可复制的方法论:找真场景、AI开敲、价值定价、自己成渠道。(239字)
|
3月前
|
存储 弹性计算 人工智能
2026年阿里云服务器推荐:适合个人和普通企业用户的云服务器推荐,最低38元起
本文基于2026年阿里云最新活动信息,按个人用户、普通企业用户、集团型企业级用户三类群体的核心需求,梳理从38元/年轻量应用服务器、99元/年经济型e实例到第九代高性能计算实例的全梯度选型方案,同步汇总三大核心活动入口与优惠权益,帮助不同规模的用户精准匹配高性价比云服务器,避开选型误区,把上云预算落到实处。
|
5月前
|
人工智能 资源调度 开发工具
从 fork 到发布:用 Qoder 重构了一个图床插件
一款 VSCode 图床插件,源码仓库 404、代码陈旧、依赖冗余——但刚好拿来试试 Qoder。先建 Wiki 摸清老代码的坑,再用 Spec 模式驱动重构,中间踩了 Region 格式的坑,最后一句指令搞定 OSS 配置、一行命令打包发布。一次从头到尾的 vibe coding记录
431 0
从 fork 到发布:用 Qoder 重构了一个图床插件
|
存储 算法 API
唯品会智能分仓API技术解析:基于收货地址自动匹配最近仓库
唯品会智能分仓API通过地理编码与Haversine距离算法,自动将订单匹配至最近仓库,提升配送效率、降低成本。本文详解其技术原理、实现步骤与应用优势,助力开发者构建高效物流系统。(239字)
|
机器学习/深度学习 JSON API
淘宝图片搜索接口(Taobao.item_search_img)
淘宝图片搜索接口(Taobao.item_search_img)允许开发者通过上传商品图片或提供图片地址,获取相似的淘宝商品列表。该接口基于深度学习和计算机视觉技术,支持注册账号、申请权限、构造请求参数、调用接口、解析响应数据等步骤。适用于电商平台购物体验提升、商家商品优化与推广、商品推荐系统和图片版权保护等场景。
1989 4
|
存储 JavaScript Oracle
探究 Java 应用的启动速度优化
在高性能的背后,Java 的启动性能差也令人印象深刻,大家印象中的 Java 笨重缓慢的印象也大多来源于此。高性能和快启动速度似乎有一些相悖,本文将和大家一起探究两者是否可以兼得。
2531 134
探究 Java 应用的启动速度优化
|
机器学习/深度学习 编解码 算法
BEVFormer-accelerate:基于EasyCV加速BEVFormer
BEVFormer是一种纯视觉的自动驾驶感知算法,通过融合环视相机图像的空间和时序特征显式的生成具有强表征能力的BEV特征,并应用于下游3D检测、分割等任务,取得了SOTA的结果。