参考文章以下文章
https://blog.csdn.net/athrunsunny/article/details/122132518
https://zhuanlan.zhihu.com/p/547878330
图片标注工具labelme安装
conda install pyqt=5
conda install labelme
# 备注labelme用法可自行查找相关文章
图片数据处理脚本
bmp转换jpg格式
bmp2jpg.py
# encoding=utf-8
import os
import cv2
# 图片的路径
bmp_dir = "D:\\sourcepng"
jpg_dir = "D:\\target"
filelists = os.listdir(bmp_dir)
for i,file in enumerate(filelists):
# 读图,-1为不改变图片格式,0为灰度图
img = cv2.imread(os.path.join(bmp_dir,file),-1)
# newName = file.replace('.bmp','.jpg') #bmp转jpg
newName = file.replace('.png','.jpg') # 这里是测试png转换jpg
cv2.imwrite(os.path.join(jpg_dir,newName),img)
print('第%d张图:%s'%(i+1,newName))
labelme标注数据转换训练集数据脚本
label2yolo.py
import os
import numpy as np
import json
from glob import glob
import cv2
from sklearn.model_selection import train_test_split
from shutil import copyfile
import argparse
# Labelme坐标到YOLO V5坐标的转换
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
# 样本转换
def convertToYolo5(fileList, output_dir, labelme_path ,datatype):
# 创建指定样本的父目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 创建指定样本的images和labels子目录
yolo_images_dir = '{0}/images/{1}/'.format(output_dir,datatype)
yolo_labels_dir = '{0}/labels/{1}/'.format(output_dir,datatype)
if not os.path.ex