**
1. 简介
** 平时标注数据时,例如检测框、分割等,使用labelme工具。labelme标注后会生成一个json文件。例如用于目标检测,由于需要标注的数据较多。前期可手动标注一部分数据,然后训练一个检测模型。通过该检测模型对剩余未标注的图片进行检测,生成对应图片的目标框的值。然后使用这些值,自己生成对应图片的json文件,这样就可以减少标注的工作量,剩下的就是对这些自动标注的数据进行质检就可以了。
这个过程中,有一个比较关键的问题就是,json文件中有每个图片的base64编码。
2. 图片转换为base64格式
from base64 import b64encode
from json import dumps
image_path = 'E:\\datas\\test\\0000_01.jpg'
with open(image_path, 'rb') as jpg_file:
byte_content = jpg_file.read()
base64_bytes = b64encode(byte_content)
base64_string = base64_bytes.decode('utf-8')
dict_data = {}
dict_data['name'] = image_path
dict_data['imageData'] = base64_string
json_data = dumps(dict_data, indent=2)
with open('test.json', 'w') as json_file:
json_file.write(json_data)
3. 制作labelme标注生成的json文件
import json
from json import dumps
import base64
root_dir = 'E:\\datas\\test\\'
def image_to_base64(image_path):
with open(image_path, 'rb') as jpg_file:
byte_content = jpg_file.read()
base64_bytes = base64.b64encode(byte_content)
base64_string = base64_bytes.decode('utf-8')
return base64_string
def get_label(txt_file):
f_txt = open(txt_file, 'r')
label = f_txt.read().splitlines()
point = []
label_list = label[0].strip().split(' ')
filename = label_list[0]
if len(label_list) == 5:
x1 = int(label_list[1])
y1 = int(label_list[2])
x2 = int(label_list[3])
y2 = int(label_list[4])
point.append(x1)
point.append(y1)
point.append(x2)
point.append(y2)
return filename, point
if __name__ == '__main__':
jpgName, pt = get_label(root_dir + '0000_01.txt')
base64_str = image_to_base64(root_dir + '0000_01.jpg')
json_label = {
"version": "5.0.1",
"flags": {},
"shapes": [
{
"label": "test",
"points": [
[
pt[0],
pt[1]
],
[
pt[2],
pt[3]
]
],
"group_id": None,
"shape_type": "rectangle",
"flags": {}
}
],
"imagePath": jpgName,
"imageData": base64_str,
"imageHeight": 1600,
"imageWidth": 2048
}
json_data = dumps(json_label, indent=2)
with open(root_dir + "0000_01.json", "w") as fp:
fp.write(json_data)
5. base64转为图片
import base64
import json
json_path = 'E:\\datas\\test\\0000_01.json'
with open(json_path, "r") as json_file:
raw_data = json.load(json_file)
image_base64_string = raw_data["imageData"]
image_data = base64.b64decode(image_base64_string)
with open('test.jpg', 'wb') as jpg_file:
jpg_file.write(image_data)
|