1.安装labelme及使用
anaconda prompt下输入命令:
pip install labelme
下载成功后,anaconda prompt输入以下命令打开:
labelme
打开软件界面,按这个按钮就是标注了
标注完多边形后双击鼠标左键表示完成标注。
然后crtl+s保存
同目录下生成json文件:
用pycharm直接打开这个json文件:
可以看到label就是我们写的标签,points是我刚刚标的多边形的四个点。
2.解析json文件为txt文件
def read_json_save_to_txt():
import json
import os
from glob import glob
dir_json = r"F:\\Projects\\study_python\\images\\" # json文件的目录
jsons = glob(dir_json + "*.json", recursive=False) # 搜寻该目录下所有后缀名为.json的文件路径,改为**/*.json recursive=True为所有子目录的
all_files = glob(dir_json + "*.*", recursive=False)
images = list(set(all_files).difference(set(jsons))) # all_files中有而jsons中没有的,就是图片
with open(dir_json+'points_data.txt', "w") as txt:
for file in jsons:
with open(file, 'r') as load_f:
load_dict = json.load(load_f)
# print("load_dict:", load_dict)
label = load_dict["shapes"][0]["label"] # 读取json中的标签信息
points = load_dict["shapes"][0]["points"] # 读取json中的点的信息
points = [int(j) for i in points for j in i] # 所有的点化为整数
for i in images:
if file.split(".")[0] == i.split(".")[0]:
txt.writelines("{0},{1},{2}\n".format(i, " ".join(str(i) for i in points), label))
# print(i)
使用这个函数,只要指定变量dir_json为json文件的目录,运行此函数,就会在你的数据文件夹下生成一个txt文件:
从txt文件读取信息:
# 读取txt内容的代码
with open("images/points_data.txt", "r") as f:
files = f.readlines()
for i in files:
image_path = i.split(",")[0]
points = i.split(",")[1]
label = i.split(",")[2].strip()
print("image_path: {0} points: {1} label: {2}".format(image_path, points, label))