PaddleOCR 文本检测数据集COCO转换脚本

此篇文章介绍了如何将PaddleOCR支持的COCO格式文本检测数据集转换为Paddle支持的特定格式,包括读取COCOJSON,解析标注信息并将其写入新的文本标签文件中。
摘要由CSDN通过智能技术生成

PaddleOCR 文本检测数据集COCO格式转换脚本

PaddleOCR支持格式:

" 图像文件名                    json.dumps编码的图像标注信息"
ch4_test_images/img_61.jpg    [{"transcription": "MASA", "points": [[310, 104], [416, 141], [418, 216], [312, 179]]}, {...}]
ch4_test_images/img_24.jpg	[{"transcription": "CONVERSE", "points": [[402, 466], [539, 432], [543, 468], [406, 502]]}]
...
import json
def coco2paddle(json_path, output_path):
    image_dir = "images"
    with open(json_path, 'r') as file:
        data = json.load(file)
        images = data["images"]
        annotations = data['annotations']
        labels = {}
        for ann in annotations:
            id = ann["image_id"]
            bbox = ann["bbox"]
            points = tlwh2points(bbox)
            label = {
                "transcription": "",
                "points": points
            }
            if id not in labels.keys():
                labels[id] = []
            labels[id].append(label)
        images_dict = {}
        for image in images:
            images_dict[image["id"]] = image["file_name"]
        dataset = {}
        for id in labels.keys():
            image_path = image_dir +"/"+images_dict[id]
            with open(output_path, "a") as f:
                f.write(f"{image_path}\t{labels[id]}\n")
            
def tlwh2points(tlwh):
    
    p0 = tlwh[0:2]
    p1 = [tlwh[0] + tlwh[2], tlwh[1]]
    p2 = [tlwh[0] + tlwh[2], tlwh[1] + tlwh[3]]
    p3 = [tlwh[0], tlwh[1] + tlwh[3]]
    return [p0, p1, p2, p3]
        
    
if __name__=="__main__":
    json_path = "/workspace/Paddle/datasets/idcard_test/annotations/instances_Train.json"
    coco2paddle(json_path, "/workspace/Paddle/datasets/idcard_test/annotations/train_label.txt")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值