python labelme,jsonカスタムラベルデータを読み込む
18155 ワード
2,コード実装,まずファイルをロードし,ファイルパス,座標をテキストファイルに保存する
jsonファイルのデータ
# -*- coding: utf-8 -*-
import json, os
from PIL import Image
label_path = r"C:\Users\RoseC\Desktop\picture"
save_path = "save_img"
with open("./label.txt", "w+") as f: #
for filename in os.listdir(label_path):
#
x1 ,y1 = 0, 0 # x: ,y:
x2, y2 = 0, 0 #
mark_x1, mark_y1 = 0, 0 #
mark_x2, mark_y2 = 0, 0 #
mark_x3, mark_y3 = 0, 0 #
mark_x4, mark_y4 = 0, 0 #
if filename.endswith(".json"):
json_path = os.path.join(label_path, filename)
data = json.load(open(json_path, 'r'))
img_name = data['imagePath'] #
for obj in data['shapes']:
if obj['label'] == "box": #
x1 = int(obj['points'][0][0])
y1 = int(obj['points'][0][1])
x2 = int(obj['points'][1][0])
y2 = int(obj['points'][1][1])
if obj['label'] == "left_up": #
mark_x1 = int(obj['points'][0][0])
mark_y1 = int(obj['points'][0][1])
if obj['label'] == "right_up": #
mark_x2 = int(obj['points'][0][0])
mark_y2 = int(obj['points'][0][1])
if obj['label'] == "left_down": #
mark_x3 = int(obj['points'][0][0])
mark_y3 = int(obj['points'][0][1])
if obj['label'] == "right_down": #
mark_x4 = int(obj['points'][0][0])
mark_y4 = int(obj['points'][0][1])
#
img = Image.open(os.path.join(label_path, img_name))
img_save_path = os.path.join(save_path, img_name)
img.save(img_save_path)
#
line = "{0} {1} {2} {3} {4} " \
"{5} {6} {7} {8} " \
"{9} {10} {11} {12}
".format(img_save_path, x1, y1, x2, y2,
mark_x1, mark_y1, mark_x2, mark_y2,
mark_x3, mark_y3, mark_x4, mark_y4)
f.write(line)
jsonファイルのデータ
{
"version": "3.16.7",
"flags": {},
"shapes": [
{
"label": "box",
"line_color": null,
"fill_color": null,
"points": [
[
121.16666666666669,
89.75
],
[
1230.5416666666667,
815.7916666666667
]
],
"shape_type": "rectangle",
"flags": {}
},
{
"label": "left_up",
"line_color": null,
"fill_color": null,
"points": [
[
184.70833333333337,
127.25
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "right_up",
"line_color": null,
"fill_color": null,
"points": [
[
1175.3333333333335,
152.25
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "left_down",
"line_color": null,
"fill_color": null,
"points": [
[
159.70833333333337,
752.25
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "right_down",
"line_color": null,
"fill_color": null,
"points": [
[
1174.2916666666667,
768.9166666666667
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "height_middle_left",
"line_color": null,
"fill_color": null,
"points": [
[
161.79166666666669,
424.125
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "height_middle_right",
"line_color": null,
"fill_color": null,
"points": [
[
1185.75,
451.20833333333337
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "width_middle_up",
"line_color": null,
"fill_color": null,
"points": [
[
699.2916666666667,
132.45833333333334
]
],
"shape_type": "point",
"flags": {}
},
{
"label": "width_middle_down",
"line_color": null,
"fill_color": null,
"points": [
[
703.4583333333334,
774.125
]
],
"shape_type": "point",
"flags": {}
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "15665254962320480618.jpg",
"imageData": " , , , ",
"imageHeight": 1001,
"imageWidth": 1334
}