一、打开labelme工具进行数据标注
二、数据格式转换
2.1 标签转换
运行Anaconda Prompt,进入json标签文件所在目录,逐个转换,分布运行
cpp
labelme_json_to_dataset 0000.json
labelme_json_to_dataset 0001.json
labelme_json_to_dataset 0002.json
labelme_json_to_dataset 0003.json
labelme_json_to_dataset 0004.json
labelme_json_to_dataset 0005.json
结束后,会得到这些文件夹
看看里面是什么内容。。
2.2 生成二值图标签和train.py
-
创建一个data_json_train空文件夹
-
在data_json_train下,创建images、clips、gt_image、gt_binary_image、gt_instance_image 共5个文件夹
-
在images下创建data空文件夹
-
在data下创建annotations空文件夹
-
将2.1节的生成的文件复制到annotations文件夹下
-
在data_json_train下,创建data.py,代码如下:
cpp
# data.py
import cv2
from skimage import measure, color
from skimage.measure import regionprops
import numpy as np
import os
import copy
# 优化的地方:应该用文件的名字来命名新的文件,而不是简单的进行 +1 操作就进行对图片文件的命名
# BUG 已解决
def skimageFilter(gray):
binary_warped = copy.copy(gray)
binary_warped[binary_warped > 0.1] = 255
gray = (np.dstack((gray, gray, gray)) * 255).astype('uint8')
labels = measure.label(gray[:, :, 0], connectivity=1)
dst = color.label2rgb(labels, bg_label=0, bg_color=(0, 0, 0))
gray = cv2.cvtColor(np.uint8(dst * 255), cv2.COLOR_RGB2GRAY)
return binary_warped, gray
def moveImageTodir(path, targetPath, name):
if os.path.isdir(path):
image_name = "gt_image/" + str(name) + ".png"
binary_name = "gt_binary_image/" + str(name) + ".png"
instance_name = "gt_instance_image/" + str(name) + ".png"
train_rows = image_name + " " + binary_name + " " + instance_name + "\n"
origin_img = cv2.imread(path + "/img.png")
origin_img = cv2.resize(origin_img, (1280, 720))
cv2.imwrite(targetPath + "/" + image_name, origin_img)
img = cv2.imread(path + '/label.png')
img = cv2.resize(img, (1280, 720))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
binary_warped, instance = skimageFilter(gray)
cv2.imwrite(targetPath + "/" + binary_name, binary_warped)
cv2.imwrite(targetPath + "/" + instance_name, instance)
print("success create data name is : ", train_rows)
return train_rows
return None
if __name__ == "__main__":
print('--------------开始执行----------------')
#count = 1
with open("./train.txt", 'w+') as file:
for images_dir in os.listdir("./images"):
dir_name = os.path.join("./images", images_dir + "/annotations")
for annotations_dir in os.listdir(dir_name):
json_dir = os.path.join(dir_name, annotations_dir)
if os.path.isdir(json_dir):
# train_rows = moveImageTodir(json_dir, "./", str(count).zfill(4))
# 利用 json 文件夹的名字来对生成的图片进行命名,比较好对应
train_rows = moveImageTodir(json_dir, "./", json_dir.split('\\')[-1])
file.write(train_rows)
#count += 1
- 运行data.py,会生成train.txt,以及gt_image,gt_binary_image,gt_instance_image下会生成转换后的标签
看下gt_binary_image下的东西。。
由于标注的数据少,所有val.txt复制train.txtx的内容。
使用train.txt,gt_image,gt_binary_image,gt_instance_image
就可以进行训练了
大功告成!