PPOCRLabel标注的txt格式如何转换成labelme能修改的json格式
作者:雪地(>^ω^<) 时间:2021-08-31 06:01:45
PPOCRLabel标注的txt格式转换成labelme能修改的json格式
PPOCR是个好东西
默认的训练文件是用自带的PPOCRLabel标注的,有时候需要用labelme来修改一些东西,比如后期用到的字段提取,就需要加一个参数,PPOCRLabel就没法做了,只能转成labelme,并利用group_id这个参数来赋值。
所以写了下面这个脚本
Label.txt每一行对应一个图片标注,该脚本作用是将PPOCRLabel的标注文件Label.txt转换为一个一个的json文件
并存储至json1目录下:
# -*- coding:utf-8 -*-
# 开发人员 : csu·攀-_-||
# 开发时间 : 2021/9/7 0007 10:30
# 文件名称 : pplable2json.py
# 开发工具 : PyCharm
# 功能描述 : PPOCRLabel标注的txt格式转换成labelme能修改的json格式
import os
import cv2
import json
import base64
#转base64
def image_to_base64(image_np):
image = cv2.imencode('.jpg', image_np)[1]
image_code = str(base64.b64encode(image))[2:-1]
return image_code
def pp2json(path,savepath):
with open(os.path.join(path,'Label.txt'),'r',encoding='utf-8') as f:
for line in f.readlines():
line = line.strip('\n') # 去掉列表中每一个元素的换行符
imgname = line.split('\t')[0].split('/')[1]
img = cv2.imread(os.path.join(path,imgname))
H,W,C = img.shape
items = json.loads(line.split('\t')[1])
item_shapes = []
for item in items:
item_shapes.append({
"label":item['transcription'],
"points":item['points'],
"group_id": 0,
"shape_type": "polygon",
"flags": {}
})
new_dict = {
"version": "4.5.9",
"flags": {},
"shapes": item_shapes,
"imagePath": imgname,
"imageData": image_to_base64(img),
"imageHeight": H,
"imageWidth": W
}
with open(os.path.join(savepath,imgname.split('.')[0] + '.json'), "w",encoding='utf-8') as fw:
print(imgname,' succeed!')
json.dump(new_dict, fw)
if __name__ == '__main__':
path = r'E:\Projects\OCR-release-2.1\tools\infer\imgs\train_data'
savepath = r'json1'
pp2json(path,savepath)
PPOCRLabel标注的Label.txt文件,所有的图片标注都在一个txt里头,一个图片一行,文件名和标注内容之间用制表符’\t’隔开。
labelme标注的json文件格式
来源:https://blog.csdn.net/qq_36563273/article/details/120822514
标签:PPOCRLabel,txt格式,labelme,json格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
有用的SQL语句(删除重复记录,收缩日志)
2008-03-04 16:59:00
Linux下用Python脚本监控目录变化代码分享
2021-03-30 20:15:59
python爬取youtube视频的示例代码
2021-10-02 16:25:26
![](https://img.aspxhome.com/file/2023/9/92499_0s.png)
python双向链表原理与实现方法详解
2021-03-06 05:29:39
python绘制直线的方法
2022-10-08 20:01:52
![](https://img.aspxhome.com/file/2023/5/93565_0s.jpg)
Numpy对于NaN值的判断方法
2022-12-15 15:08:21
![](https://img.aspxhome.com/file/2023/2/75282_0s.png)
Go gRPC服务客户端流式RPC教程
2023-07-16 06:08:55
python实现批量命名照片
2021-11-03 18:07:42
![](https://img.aspxhome.com/file/2023/3/77043_0s.png)
python如何将图片生成视频MP4
2023-10-19 09:25:37
![](https://img.aspxhome.com/file/2023/3/104843_0s.jpg)
Python迭代和迭代器详解
2023-11-20 08:52:28
Python函数高级(命名空间、作用域、装饰器)
2022-03-15 23:31:44
![](https://img.aspxhome.com/file/2023/1/75211_0s.png)
Anaconda 查看、创建、管理和使用python环境的方法
2023-11-29 02:34:48
![](https://img.aspxhome.com/file/2023/6/93486_0s.png)
python中执行shell的两种方法总结
2023-03-12 15:20:39
解决Django cors跨域问题
2021-07-02 08:42:36
python encode和decode的妙用
2021-01-08 01:34:20
python实现ftp文件传输功能
2023-04-21 13:20:16
![](https://img.aspxhome.com/file/2023/0/87900_0s.jpg)
Python中的单例模式与反射机制详解
2023-11-24 00:55:54
Python中True(真)和False(假)判断详解
2021-12-16 11:22:36
ASP隐藏真实文件的下载功能实现代码
2011-04-14 11:12:00
Python shapefile转GeoJson的2种方式实例
2023-02-20 01:07:52
![](https://img.aspxhome.com/file/2023/7/71427_0s.jpg)