实现labelme批量json_to_dataset方法

实现labelme批量json_to_dataset方法labelme可以帮助我们快速的实现Mask-RCNN中数据集json文件的生成,然而还需要我们进一步的将json转成dataset,可以直接在cmd中执行labelme_json_to_dataset.exeC:\Users\Administrator\Desktop\total\1.json(路径),但是这个过程需要我们一个json文件的生成,过程很慢。一、打开abelm…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

        labelme可以帮助我们快速的实现Mask-RCNN中数据集json文件的生成,然而还需要我们进一步的将json转成dataset,可以直接在cmd中执行labelme_json_to_dataset.exe C:\Users\Administrator\Desktop\total\1.json(路径),但是这个过程需要我们一个json文件的生成,过程很慢。

一、打开abelme安装目录

在lableme安装目录下有G:\Anaconda\Lib\site-packages\labelme\cli目录,可以看到json_to_dataset.py文件

实现labelme批量json_to_dataset方法

这里面提供将json转成dataset的代码,所以我们只需要在这个基础上更改即可。

二、代码实现

复制json_to_dataset.py文件,代码更改:

import argparse
import json
import os
import os.path as osp
import warnings
 
import PIL.Image
import yaml
 
from labelme import utils
import base64
 
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()
 
    json_file = args.json_file
    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)
 
    count = os.listdir(json_file) 
    for i in range(0, len(count)):
        path = os.path.join(json_file, count[i])
        if os.path.isfile(path):
            data = json.load(open(path))
            
            if data['imageData']:
                imageData = data['imageData']
            else:
                imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
                with open(imagePath, 'rb') as f:
                    imageData = f.read()
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)
            label_name_to_value = {'_background_': 0}
            for shape in data['shapes']:
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value
            
            # label_values must be dense
            label_values, label_names = [], []
            for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
                label_values.append(lv)
                label_names.append(ln)
            assert label_values == list(range(len(label_values)))
            
            lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
            
            captions = ['{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()]
            lbl_viz = utils.draw_label(lbl, img, captions)
            
            out_dir = osp.basename(count[i]).replace('.', '_')
            out_dir = osp.join(osp.dirname(count[i]), out_dir)
            if not osp.exists(out_dir):
                os.mkdir(out_dir)
 
            PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
            #PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
            utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
            PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
 
            with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
                for lbl_name in label_names:
                    f.write(lbl_name + '\n')
 
            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=label_names)
            with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)
 
            print('Saved to: %s' % out_dir)
if __name__ == '__main__':
    main()

然后替换之前json_to_dataset.py文件。

三、执行与查看

在cmd中cd到label_json_to_dataset.py路径下,然后输入

实现labelme批量json_to_dataset方法

路径只需要输入到文件夹即可,不需要具体指定json文件。

然后在安装目录下的Scripts路径下可以查看到批量保存的json文件夹。

如果有兴趣,可以看一下python3.7.0+win10安装labelme实现批量操作的。https://blog.csdn.net/yql_617540298/article/details/111041776

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195004.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 怎么复制网页不能复制的文字_网页文字无法复制

    怎么复制网页不能复制的文字_网页文字无法复制环景:win10专业版火狐浏览器92.01问题描述:有些网站上文字不能复制解决方案:1.打开XX文库网页2.按F12点击consle控制台,输入document.body.innerText3.找到要复制的文字复制即可

    2022年10月13日
    2
  • [POJ 2976]Dropping tests(0-1分数规划)

    [POJ 2976]Dropping tests(0-1分数规划)

    2022年2月1日
    50
  • node读取文件操作

    node读取文件操作导言:nodejs中所有与文件相关的操作都在fs模块中,而读写操作又是我们会经常用到的操作,nodejs的fs模块针对读操作为我们提供了readFile,read,createReadStream三个方法,针对写操作为我们提供了writeFile,write,createWriteStream三个方法,下面分析一下它们的区别:一、readFile和writeFile1、readFile…

    2022年5月29日
    49
  • ubuntu 微信开发者工具_微信web开发者工具官方下载

    ubuntu 微信开发者工具_微信web开发者工具官方下载下载地址:开发者工具下载解压到/optsudomkdir/opt/wxdt&&sudotar-zxvfwechat-devtools-1.03.2006090.tar.gz-C/opt/wxdtsudoln-s/opt/wxdt/bin/wechat-devtools/usr/bin/wd创建桌面图标文件vim~/.local/share/applications/wedt.desktop写入[DesktopEntry]Encoding=UT

    2025年7月1日
    1
  • C语言背包问题

    C语言背包问题0/1背包问题一个旅行者有一个最多能装MM公斤的背包,现在有nn件物品,它们的重量分别是W1,W2,…,WnW1,W2,…,Wn,它们的价值分别为C1,C2,…,CnC1,C2,…,Cn,求旅行者能获得最大总价值。【输入】第一行:两个整数,MM(背包容量,M<=200M<=200)和NN(物品数量,N<=30N<=30);第2..N+12..N+1行:每行二个整数Wi,CiWi,Ci,表示每个物品的重量和价值。【输出】仅一行…

    2022年7月14日
    12
  • Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)[通俗易懂]

    Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)[通俗易懂]from:http://blog.coolaj86.com/articles/getting-started-with-amazon-ec2-1-year-free-aws-vps-web-hosting.htmlAmazonWebServicesGoogle”AmazonWebServiceFreeTier”http://aws.amazon.com/…

    2022年10月8日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号