Python 官方文档:入门教程 => 点击学习
说明 labelme制作yolov5模型的数据集,超级详细,主要步骤: labelme安装和使用教程python实现JSON文件转txt文件格式Python实现对指定格式文件进行提取 找一批牛和马的数
labelme制作yolov5模型的数据集,超级详细,主要步骤:
找一批牛和马的数据集,用来做YOLOv5实现牛马检测识别任务,数据集格式如下:
(1)进入PyCharm在终端输入:
pip install labelme
(2)安装完labelme后,在终端输入labelme标注工具:
(3)点击Open dir找到数据集所放的位置,然后选择需要标注的数据集所在的文件夹:
(4)点击Edit,选择标注的方式(我选择的是矩形框标注(Create Rectangle)):
(5)框出目标物,单击鼠标左键弹出标签名称,输入标签值(此实验室是牛和马标注,我的标签值是cattle和horse),标签值填写好后点击ok
(6) 标注完一张后,进行保存和下一张继续标注,快捷键Ctrl+s保存,按键‘d’切换下一张图片:
(7)把所有图片标注完后大概是这个样子(标注结果json也放在了图片所在的目录中):
将labelme标注的结果json文件转换为yolov5模型需要的txt文件格式
import osimport numpy as npimport jsonfrom glob import globimport cv2from sklearn.model_selection import train_test_splitfrom os import getcwdclasses = ["cattle", "horse"]# 1.标签路径labelme_path = r"C:/Users/xxxx/Desktop/images/dataset/cattle/"isUseTest = True # 是否创建test集# 3.获取待处理文件files = glob(labelme_path + "*.json")files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]print(files)if isUseTest: trainval_files, test_files = train_test_split(files, test_size=0.1, random_state=55)else: trainval_files = files# splittrain_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)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)wd = getcwd()print(wd)def ChangeToYolo5(files, txt_Name): if not os.path.exists('tmp/'): os.makedirs('tmp/') list_file = open('tmp/%s.txt' % (txt_Name), 'w') for json_file_ in files: json_filename = labelme_path + json_file_ + ".json" imagePath = labelme_path + json_file_ + ".jpg" list_file.write('%s/%s\n' % (wd, imagePath)) out_file = open('%s/%s.txt' % (labelme_path, json_file_), 'w') json_file = json.load(open(json_filename, "r", encoding="utf-8")) height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape for multi in json_file["shapes"]: points = np.array(multi["points"]) xmin = min(points[:, 0]) if min(points[:, 0]) > 0 else 0 xmax = max(points[:, 0]) if max(points[:, 0]) > 0 else 0 ymin = min(points[:, 1]) if min(points[:, 1]) > 0 else 0 ymax = max(points[:, 1]) if max(points[:, 1]) > 0 else 0 label = multi["label"] if xmax <= xmin: pass elif ymax <= ymin: pass else: cls_id = classes.index(label) b = (float(xmin), float(xmax), float(ymin), float(ymax)) bb = convert((width, height), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') print(json_filename, xmin, ymin, xmax, ymax, cls_id)ChangeToYolo5(train_files, "train")ChangeToYolo5(val_files, "val")ChangeToYolo5(test_files, "test")
代码运行
代码需要修改的地方,如 下图两个标出的方框中,第一个方框为对应的两个标签值,标签值cattle(牛)对应的是0、horse(马)对应的是1。
第二个方框为json所在文件夹的存储路径。
运行结果:
json文件转换生成的txt文件保存到当前目录下,如下图:
除此之外,还在当前目录下生成如下文件:
上面第二步可以看出文件太乱了,所有文件包括图片、对图片的标注结果json的文件以及生成的txt格式的文件都在一个文件夹内,所以要把他们提取到指定文件夹里面,实现代码:
import osimport shutil#文件存放目录source_folder = r"C:/Users/xxx/Desktop/data/images/"#提取文件保存目录destination_folder = r"C:/Users/xxx/Desktop/data/train/labels/"# 自动创建输出目录if not os.path.exists(destination_folder): os.makedirs(destination_folder)# 遍历所有子文件夹for parent_folder, _, file_names in os.walk(source_folder): # 遍历当前子文件夹中的所有文件 for file_name in file_names: # 只处理图片文件 # if file_name.endswith(('jpg', 'jpeg', 'png', 'gif')):#提取jpg、jpeg等格式的文件到指定目录 if file_name.endswith(('.txt')):#提取json格式的文件到指定目录 # 构造源文件路径和目标文件路径 source_path = os.path.join(parent_folder, file_name) destination_path = os.path.join(destination_folder, file_name) # 复制文件到目标文件夹 shutil.copy(source_path, destination_path)
C:/Users/xxx/Desktop/data/train/images/
dataset |——test |——images |——train |——images |——labels |——val |——images |——labels
其中images里面是图片:
labels里面装的是标注的结果转换而来的txt文件:
值得注意的是,train文件或者val文件夹里面的images和labels里面的文件是一一对应的:
至此,yolov5的标准数据集制作完成。
如何训练复现yolov5模型请移步下一章。
来源地址:https://blog.csdn.net/weixin_45736855/article/details/129583272
--结束END--
本文标题: labelme制作yolov5模型的数据集
本文链接: https://www.lsjlt.com/news/415469.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0