目标检测yolov5检测火龙果
一、github官方网址
https://github.com/ultralytics/yolov5/tree/v6.1
二、labelme标记数据集:
(1)、进入虚拟环境
(2)、pip install labelme
(3)、labelme打开
(4)、注意选择自动保存
注:labelme 支持jpg等格式,不支持HEIC格式,图片格式转化网址(一次只支持转化10张图片):HEIC转JPG - 免费在线将HEIC文件转换成JPG (cdkm.com)
三、json转为txt
labelme保存的是json文件,需要将json文件转化为txt文件,用于label标签
可用python代码批量转化json文件:
import json import os # 标签名称,labelme做了几个标签这里就填几个 name2id = {'fruit': 0} def convert(img_size, box): dw = 1. / (img_size[0]) dh = 1. / (img_size[1]) x = (box[0] + box[2]) / 2.0 - 1 y = (box[1] + box[3]) / 2.0 - 1 w = box[2] - box[0] h = box[3] - box[1] x = x * dw w = w * dw y = y * dh h = h * dh return (x, y, w, h) def decode_json(json_floder_path, json_name): txt_name = 'E:\\***\\***\\目标检测\\labels\\' + json_name[0:-5] + '.txt' # 存放txt文件夹的绝对路径 txt_file = open(txt_name, 'w') json_path = os.path.join(json_floder_path, json_name) data = json.load(open(json_path, 'r', encoding='gb2312')) img_w = data['imageWidth'] img_h = data['imageHeight'] for i in data['shapes']: label_name = i['label'] if (i['shape_type'] == 'rectangle'): x1 = int(i['points'][0][0]) y1 = int(i['points'][0][1]) x2 = int(i['points'][1][0]) y2 = int(i['points'][1][1]) bb = (x1, y1, x2, y2) bbox = convert((img_w, img_h), bb) txt_file.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n') if __name__ == "__main__": # 存放json文件夹的绝对路径 json_floder_path = 'E:\\***\\***\\目标检测\\20220808json' json_names = os.listdir(json_floder_path) for json_name in json_names: decode_json(json_floder_path, json_name)
四、训练集train和验证集valid
train或者valid中保存的images和labels中的文件名是一一对应的
 ![]()

 

五、代码修改
(1)yolov5-6.1/utils/dataset.py
num_workers=0
(2)yolov5-6.1\data\fruittest.yaml 修改训练数据
fruittest.yaml代码:
train: ../train/images val: ../valid/images # Classes nc: 1 # number of classes names: ['fruit'] # class names
六、开始训练
python train.py --data fruittest.yaml --cfg yolov5s.yaml --weights '' --batch-size 32  --epoch 300
七、预测
python detect.py --source data\vedio --weights runs\train\exp5\weights\best.pt  --data data\fruittest.yaml 
纯绿色火龙果检测结果:https://live.csdn.net/v/230691
八、问题与反思
(1)、问题描述:AttributeError: 'NoneType' object has no attribute '_free_weak_ref'
解决办法:修改yolov5-6.1/utils/dataset.py中num_workers
num_workers=0
九、详细内容详见:
https://blog.csdn.net/qq_42051389/article/details/126248773?spm=1001.2014.3001.5502
 
                    
                
 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号