您好,登录后才能下订单哦!
在计算机视觉领域,图像标注是一个非常重要的步骤,尤其是在目标检测、图像分割等任务中。PPOCRLabel 和 labelme 是两种常用的图像标注工具,它们分别使用不同的文件格式来存储标注信息。PPOCRLabel 使用的是 txt 格式,而 labelme 使用的是 json 格式。本文将详细介绍如何将 PPOCRLabel 标注的 txt 格式转换为 labelme 能修改的 json 格式。
PPOCRLabel 是一个基于 PaddleOCR 的图像标注工具,主要用于 OCR(光学字符识别)任务的图像标注。它支持对图像中的文本区域进行标注,并将标注信息保存为 txt 文件。每个 txt 文件对应一张图像,文件中每一行代表一个标注的文本区域,包含文本区域的坐标信息和文本内容。
labelme 是一个通用的图像标注工具,支持多种标注任务,如目标检测、图像分割等。它使用 json 文件来存储标注信息,每个 json 文件对应一张图像,文件中包含了图像的基本信息以及标注的形状、类别等信息。
PPOCRLabel 的 txt 文件格式如下:
x1,y1,x2,y2,x3,y3,x4,y4,text
x1,y1,x2,y2,x3,y3,x4,y4,text
...
其中,x1,y1
到 x4,y4
是文本区域的四个顶点的坐标,text
是该文本区域的内容。
例如,一个典型的 PPOCRLabel 标注文件可能如下所示:
100,200,150,200,150,250,100,250,Hello
300,400,350,400,350,450,300,450,World
labelme 的 json 文件格式如下:
{
"version": "4.5.6",
"flags": {},
"shapes": [
{
"label": "text",
"points": [
[x1, y1],
[x2, y2],
[x3, y3],
[x4, y4]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"imagePath": "image.jpg",
"imageData": null,
"imageHeight": 600,
"imageWidth": 800
}
其中,shapes
字段包含了所有的标注信息,每个标注对象包含 label
(类别)、points
(顶点坐标)、shape_type
(形状类型)等信息。
要将 PPOCRLabel 的 txt 格式转换为 labelme 的 json 格式,我们需要完成以下几个步骤:
首先,我们需要读取 PPOCRLabel 的 txt 文件,并解析出每个文本区域的坐标和文本内容。可以使用 Python 的文件操作来实现:
def read_ppocrlabel_txt(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
annotations = []
for line in lines:
parts = line.strip().split(',')
coords = list(map(int, parts[:8]))
text = parts[8]
annotations.append({'coords': coords, 'text': text})
return annotations
接下来,我们需要将解析出的坐标和文本内容转换为 labelme 的 json 格式。可以使用 Python 的字典和列表操作来实现:
def convert_to_labelme_json(annotations, image_path, image_height, image_width):
shapes = []
for ann in annotations:
coords = ann['coords']
text = ann['text']
points = [
[coords[0], coords[1]],
[coords[2], coords[3]],
[coords[4], coords[5]],
[coords[6], coords[7]]
]
shape = {
"label": text,
"points": points,
"group_id": None,
"shape_type": "polygon",
"flags": {}
}
shapes.append(shape)
labelme_json = {
"version": "4.5.6",
"flags": {},
"shapes": shapes,
"imagePath": image_path,
"imageData": None,
"imageHeight": image_height,
"imageWidth": image_width
}
return labelme_json
最后,我们需要将转换后的 json 数据保存为文件。可以使用 Python 的 json
模块来实现:
import json
def save_labelme_json(labelme_json, output_path):
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(labelme_json, f, ensure_ascii=False, indent=2)
将上述步骤整合起来,完整的代码如下:
import json
def read_ppocrlabel_txt(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
annotations = []
for line in lines:
parts = line.strip().split(',')
coords = list(map(int, parts[:8]))
text = parts[8]
annotations.append({'coords': coords, 'text': text})
return annotations
def convert_to_labelme_json(annotations, image_path, image_height, image_width):
shapes = []
for ann in annotations:
coords = ann['coords']
text = ann['text']
points = [
[coords[0], coords[1]],
[coords[2], coords[3]],
[coords[4], coords[5]],
[coords[6], coords[7]]
]
shape = {
"label": text,
"points": points,
"group_id": None,
"shape_type": "polygon",
"flags": {}
}
shapes.append(shape)
labelme_json = {
"version": "4.5.6",
"flags": {},
"shapes": shapes,
"imagePath": image_path,
"imageData": None,
"imageHeight": image_height,
"imageWidth": image_width
}
return labelme_json
def save_labelme_json(labelme_json, output_path):
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(labelme_json, f, ensure_ascii=False, indent=2)
def main():
ppocrlabel_txt_path = 'example.txt'
image_path = 'example.jpg'
image_height = 600
image_width = 800
output_json_path = 'example.json'
annotations = read_ppocrlabel_txt(ppocrlabel_txt_path)
labelme_json = convert_to_labelme_json(annotations, image_path, image_height, image_width)
save_labelme_json(labelme_json, output_json_path)
if __name__ == '__main__':
main()
example.txt
。example.jpg
。image_height
和 image_width
为图像的实际高度和宽度。example.json
。本文详细介绍了如何将 PPOCRLabel 标注的 txt 格式转换为 labelme 能修改的 json 格式。通过解析 PPOCRLabel 的 txt 文件,并将其转换为 labelme 的 json 格式,我们可以方便地在 labelme 中进一步编辑和管理标注数据。希望本文对你在图像标注任务中有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。