首先需要安装openvino、paddlepaddle、labelimg等模块,python版本需要小于3.8(可以通过虚拟环境创建),
classes.txt:
dog
cat
在这个位置运行gen.py
gen.py:
import os
def train_val(labels_path, data_path, ratio=0.3):
nomask_num = 0
mask_num = 0
image_dir = "\\".join(data_path.split("\\")[-3:]) + "\\images"
txt_files = os.listdir(labels_path)
f_train = open("train.txt", "w")
f_val = open("val.txt", "w")
m = 0
n = 0
for txt in txt_files:
f_txt = open(os.path.join(labels_path, txt), 'r')
if f_txt.read()[0] == "0":
nomask_num += 1
else:
mask_num += 1
f_txt.close()
for txt in txt_files:
f_txt = open(os.path.join(labels_path, txt), 'r')
if f_txt.read()[0] == "0":
n += 1
if n >= int(nomask_num * ratio):
f_train.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
else:
f_val.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
else:
m += 1
if m >= int(mask_num * ratio):
f_train.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
else:
f_val.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
f_txt.close()
f_train.close()
f_val.close()
if __name__ == "__main__":
data_path = os.path.join(os.getcwd(), "m_animal")
labels_path = os.path.join(data_path, "labels")
train_val(labels_path=labels_path, data_path=data_path, ratio=0.3)
运行后产生train.txt、val.txt,在yolo2coco目录用控制台运行
python yolov5_2_coco.py --dir_path dataset/YOLOV5
yolov5_2_coco.py:
import argparse
from pathlib import Path
import json
import shutil
import cv2
def read_txt(txt_path):
with open(str(txt_path), 'r', encoding='utf-8') as f:
data = f.readlines()
data = list(map(lambda x: x.rstrip('\n'), data))
return data
def mkdir(dir_path):
Path(dir_path).mkdir(parents=True, exist_ok=True)
class YOLOV5ToCOCO(object):
def __init__(self, dir_path):
self.src_data = Path(dir_path)
self.src = self.src_data.parent
self.train_txt_path = self.src_data / 'train.txt'
self.val_txt_path = self.src_data / 'val.txt'
# 构建COCO格式目录
self.dst = Path(self.src) / f"{
Path(self.src_data).name}_COCO_format"
self.coco_train = "train2017"
self.coco_val = "val2017"
self.coco_annotation = "annotations"
self.coco_train_json = self.dst / self.coco_annotation \
/ f'instances_{
self.coco_train}.json'
self.coco_val_json = self.dst / self.coco_annotation \
/ f'instances_{
self.coco_val}.json'
mkdir(self.dst)
mkdir(self.dst / self.coco_train)
mkdir(self.dst / self.coco_val)
mkdir(self.dst / self.coco_annotation)
# 构建json内容结构
self.type = 'instances'
self.categories = []
self.annotation_id = 1
# 读取类别数
self._get_category()
self