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")
GitHub 加速计划 / pa / PaddleOCR
41.53 K
7.59 K
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:3 个月前 )
7bbda2bc 6 天前
1d4e7a80 8 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐