Use labelme software to make data sets in batches (and divide the data sets into training sets)

First, how to make a leaflet:

1. First activate the labelme environment

conda activate label#我label安装到label环境中,label是环境的名称

2. Find the script under the labelme installation path and find the directory where labelme_json_to_dataset.exe is located.

cd F:\Anacondapy3.5\envs\label\Scripts  

3. Enter

python labelme_json_to_dataset.exe C:\Users\Terry\Desktop\1123123\\11011000001320000005-1-20201012133705-23892182849.json
C:\Users\Terry\Desktop\1123123\\11011000001320000005-1-20201012133705-23892182849.json

The above is the path of the file. Do not understand the absolute path relative path, please see my other blog post.
Insert picture description here

Insert picture description here
We opened there is no yaml file, only 4 files.
Solve the problem of no yaml:
https://blog.csdn.net/winter616/article/details/104426111/

Batch production:

Use the script method
1 to create a py file (mine is 1234.py) and
enter the following:

import os
path = 'C:/Users/Terry/Desktop/shuichi/zhai\\'  # path是你存放json的路径
json_file = os.listdir(path)
for file in json_file:
    os.system("python F:/Anacondapy3.5/envs/label/Scripts/labelme_json_to_dataset.exe %s"%(path + file))

2. On the command line, open the labelme environment

3 Open the directory where 1234.py is located. Then enter on the command line

python  1234.py

Insert picture description here

Insert picture description here

Move the picture to the specified folder

import os
import cv2 as cv
path="C:/Users/Terry/Desktop/shuichi/zhai"
if not os.path.exists(os.path.join(path,"IMAGE_picture")):
    os.mkdir(path+"./IMAGE_picture")#./IMAGE_picture如果是这样则会创建一个zhai文件夹下的mulu
    os.mkdir(path+"./LABEL_picture")

IMAGE_picture_path=os.path.join(path,"IMAGE_picture")
LABEL_picture_PATH=os.path.join(path,"LABEL_picture")
imgs_name = os.listdir(path)
print(imgs_name)
STR=0
for imgname in imgs_name:

    STR=str(STR)
    imgs_path=os.path.join(path,imgname)
    img_path = os.path.join(imgs_path, "img.png")
    label_path=os.path.join(imgs_path, "label.png")
    label=cv.imread(label_path)
    image=cv.imread(img_path)
    cv.imwrite(IMAGE_picture_path+"./image"+STR+".png",image)
    cv.imwrite(LABEL_picture_PATH + "./label" +STR +".png", label)
    STR = int(STR)
    STR=STR+1

"""
    print(label_path)
    src = cv.imread(label_path)
    cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
    cv.imshow('input_image', src)
    cv.waitKey(0)
"""

Make the training set data set:
put the two generated files in a folder, here is 13123
Insert picture description here

import codecs
import os
import random
import shutil
from PIL import Image

train_ratio = 4.0 / 5
all_file_dir = 'C:/Users/Terry/Desktop/13123'
class_list = [c for c in os.listdir(all_file_dir) if
              os.path.isdir(os.path.join(all_file_dir, c)) and not c.endswith('Set') and not c.startswith('.')]

# 用于返回文件夹的名称
class_list.sort()  # 排序
print(class_list)

##############################创建文件夹######################################
train_image_dir = os.path.join(all_file_dir, "trainImageSet")  # 这一行是路径的拼接trainImageSet放在all_file_dir路径下
if not os.path.exists(train_image_dir):  # 查看文件是否存在
    os.makedirs(train_image_dir)
train_image_img_dir = os.path.join(train_image_dir, "image")  # 这一行是路径的拼接trainImageSet放在all_file_dir路径下
train_image_label_dir = os.path.join(train_image_dir, "label")
if not (os.path.exists(train_image_img_dir) or os.path.exists(train_image_label_dir)):  # 查看文件是否存在
    os.makedirs(train_image_img_dir)
    os.makedirs(train_image_label_dir)

eval_image_dir = os.path.join(all_file_dir, "evalImageSet")
if not os.path.exists(eval_image_dir):
    os.makedirs(eval_image_dir)

eval_image_label_dir = os.path.join(eval_image_dir, "label")
eval_image_img_dir = os.path.join(eval_image_dir, "image")
if not (os.path.exists(eval_image_label_dir) or os.path.exists(eval_image_img_dir)):
    os.makedirs(eval_image_label_dir)
    os.makedirs(eval_image_img_dir)


#######创建train  ,eval文件分别在trainImageSet,evalImageSet目录下###########################
train_file = codecs.open(os.path.join(train_image_dir, "train.txt"), 'w')  # 打开文件,w为只读
eval_file = codecs.open(os.path.join(eval_image_dir, "eval.txt"), 'w')


################移动图片#####################################
image_path_pre = os.path.join(all_file_dir, class_list[0])#保存原图像的路径
image_list = [c for c in os.listdir(os.path.join(all_file_dir,class_list[0]))]##所有图片的名字
image_list.sort() ##排序
label_path_pre = os.path.join(all_file_dir, class_list[1])#保存原label的路径
label_list = [c for c in os.listdir(os.path.join(all_file_dir,class_list[1]))]##所有label的名字
label_list.sort() #排序
if (len(label_list)==len(image_list)):
    list = [(image_list[i]+" "+label_list[i]) for i in range(0,len(label_list))]

for file in list:
    B=file.split()[0]

    if random.uniform(0, 1) <= train_ratio:
        shutil.copyfile(os.path.join(image_path_pre, file.split()[0]),
                        os.path.join(train_image_img_dir, file.split()[0]))  # 复制一个文件到另一个文件中
        shutil.copyfile(os.path.join(label_path_pre, file.split()[1]),
                        os.path.join(train_image_label_dir, file.split()[1]))

        train_file.write("{0}  {1}\n".format(os.path.join("trainImageSet\image", file.split()[0]), os.path.join("trainImageSet\label", file.split()[1])))
    else:
        shutil.copyfile(os.path.join(image_path_pre, file.split()[0]),
                        os.path.join(eval_image_img_dir, file.split()[0]))

        shutil.copyfile(os.path.join(label_path_pre, file.split()[1]),
                        os.path.join(eval_image_label_dir, file.split()[1]))
        eval_file.write("{0}  {1}\n".format(os.path.join("evalImageSet\image", file.split()[0]), os.path.join("evalImageSet\label", file.split()[1])))


train_file.close()
eval_file.close()

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/ALZFterry/article/details/109336054