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")