栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

dbnet ICPAR2015数据集json格式制作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

dbnet ICPAR2015数据集json格式制作

dbnet json格式制作
  • 说明
    • json格式
    • 代码

说明

dbnet代码选自https://github.com/WenmuZhou/DBNet.pytorch,训练时数据集信息以json导入,但是官方ICPAR2015数据集下载数据是txt格式因此会出现错误:FileNotFoundError: [Errno 2] No such file or directory: ‘datasetstrain.json’ 因此进行修改

json格式

从该博主找到json数据格式http://t.csdn.cn/ZJRog。写一个脚本实现制作该json数据集

代码

代码改编自http://t.csdn.cn/cxApa

#coding:utf-8

import json
import os

def writeToJson(filePath,data):
    fb = open(filePath,'w',encoding='utf-8')
    # json.dumps(data).decode('unicode-escape')
    fb.write(json.dumps(data,indent=2,ensure_ascii=False)) # ,encoding='utf-8'
    fb.close()

def txt2json(imagePath,txtPath,savePath):
    txtName = os.listdir(txtPath)
    imgName = os.listdir(imagePath)
    content = {}
    datalist=[]
    for i in txtName:
        with open(txtPath+'/'+i,'r', encoding='UTF-8-sig') as f:
            content1={}
            line  =  f.readlines() #忽略/n
            #print(int(i[-5:-4]) - 1)
            content1['img_name'] = imgName[int(i[-5:-4]) - 1]
            txt=[]
            for j in range(len(line)):
                content2={}
                list = line[j].splitlines()
                list = list[0].split(',')
                #print([int(list[0]),int(list[1])])
                xy=[]
                for k in [0,2,4,6]:
                    xy.append([int(list[k]),int(list[k+1])])
                content2['polygon']=xy
                content2['text']=list[8]
                if list[8]=='###':
                    content2['illegibility']=True
                else:
                    content2['illegibility']=False
                content2['language']="Latin"
                #print(content2)
                txt.append(content2)
                content2['chars']=[
                    {
                        "polygon": [],
                        "char": "",
                        "illegibility": False,
                        "language": "Latin"
                    }
                ]
            content1['annotations'] = txt
        datalist.append(content1)
    content['data_root']="datasets/train/img"
    content['data_list']=datalist
    #print(content)
    writeToJson(savePath, content)




if __name__=='__main__':
    imagePath=r'C:UsersAdministratorDesktopDBNet.pytorch-masterdatasetstrainimg'
    txtPath=r'C:UsersAdministratorDesktopDBNet.pytorch-masterdatasetstraingt'
    savePath=r'C:UsersAdministratorDesktopDBNet.pytorch-masterdatasetstrain.json'
    txt2json(imagePath,txtPath,savePath)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号