0


OpenCV与AI深度学习 | 实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程)

本文来源公众号“OpenCV与AI深度学习”,仅用于学术分享,侵权删,干货满满。

原文链接:实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程)

0 导 读

  1. 本文将手把手教你用YoloV8训练自己的数据集并实现手势识别。

1 安装环境

【1】安装torch, torchvision对应版本,这里先下载好,直接安装

  1. pip install torch-1.13.1+cu116-cp38-cp38-win_amd64.whl
  2. pip install torchvision-0.14.1+cu116-cp38-cp38-win_amd64.whl

安装好后可以查看是否安装成功,上面安装的gpu版本,查看指令与结果:

  1. import torch
  2. print(torch.__version__)
  3. print(torch.cuda.is_available())

【2】安装ultralytics****

  1. pip install ultralytics

【3】下载YoloV8预训练模型:GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite

本文使用YOLOv8n,直接下载第一个即可

【4】运行demo测试安装是否成功:

  1. from ultralytics import YOLO
  2. # Load a model
  3. model = YOLO('yolov8n.pt') # pretrained YOLOv8n model
  4. # Run batched inference on a list of images
  5. results = model(['1.jpg', '2.jpg']) # return a list of Results objects
  6. # Process results list
  7. for result in results:
  8. boxes = result.boxes # Boxes object for bounding box outputs
  9. masks = result.masks # Masks object for segmentation masks outputs
  10. keypoints = result.keypoints # Keypoints object for pose outputs
  11. probs = result.probs # Probs object for classification outputs
  12. result.show() # display to screen
  13. result.save(filename='result.jpg') # save to disk

2 标注/制作数据集

【1】准备好待标注图片

  1. 可以自己写一个从摄像头存图的脚本保存一下**不同手势图**到本地,这里提供一个供参考:
  1. # -*- coding: utf-8 -*-
  2. import cv2
  3. cap = cv2.VideoCapture(0)
  4. flag = 0
  5. if(cap.isOpened()): #视频打开成功
  6. flag = 1
  7. else:
  8. flag = 0
  9. print('open cam failed!')
  10. if(flag==1):
  11. while(True):
  12. cv2.namedWindow("frame")
  13. ret,frame = cap.read()#读取一帧
  14. if ret==False: #读取帧失败
  15. break
  16. cv2.imshow("frame", frame)
  17. if cv2.waitKey(50)&0xFF ==27: #按下Esc键退出
  18. cv2.imwrite("1.jpg",frame)
  19. break
  20. cap.release()
  21. cv2.destroyAllWindows()

本文使用共3种手势1,2,5,三种手势各300张,大家可以根据实际情况增减样本数量。

【2】标注样本

  1. 标注工具使用labelimg即可,直接pip安装:
  1. pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple

安装完成后,命令行直接输入labelimg,回车即可打开labelimg,数据集类型切换成YOLO,然后依次完成标注即可。

【3】标注划分

  1. 标注好之后,使用下面的脚本划分训练集、验证集,注意设置正确的图片和txt路径:
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import random
  4. import shutil
  5. # 设置文件路径和划分比例
  6. root_path = "./voc_yolo/"
  7. image_dir = "./JPEGImages/"
  8. label_dir = "./Annotations/"
  9. train_ratio = 0.7
  10. val_ratio = 0.2
  11. test_ratio = 0.1
  12. # 创建训练集、验证集和测试集目录
  13. os.makedirs("images/train", exist_ok=True)
  14. os.makedirs("images/val", exist_ok=True)
  15. os.makedirs("images/test", exist_ok=True)
  16. os.makedirs("labels/train", exist_ok=True)
  17. os.makedirs("labels/val", exist_ok=True)
  18. os.makedirs("labels/test", exist_ok=True)
  19. # 获取所有图像文件名
  20. image_files = os.listdir(image_dir)
  21. total_images = len(image_files)
  22. random.shuffle(image_files)
  23. # 计算划分数量
  24. train_count = int(total_images * train_ratio)
  25. val_count = int(total_images * val_ratio)
  26. test_count = total_images - train_count - val_count
  27. # 划分训练集
  28. train_images = image_files[:train_count]
  29. for image_file in train_images:
  30. label_file = image_file[:image_file.rfind(".")] + ".txt"
  31. shutil.copy(os.path.join(image_dir, image_file), "images/train/")
  32. shutil.copy(os.path.join(label_dir, label_file), "labels/train/")
  33. # 划分验证集
  34. val_images = image_files[train_count:train_count+val_count]
  35. for image_file in val_images:
  36. label_file = image_file[:image_file.rfind(".")] + ".txt"
  37. shutil.copy(os.path.join(image_dir, image_file), "images/val/")
  38. shutil.copy(os.path.join(label_dir, label_file), "labels/val/")
  39. # 划分测试集
  40. test_images = image_files[train_count+val_count:]
  41. for image_file in test_images:
  42. label_file = image_file[:image_file.rfind(".")] + ".txt"
  43. shutil.copy(os.path.join(image_dir, image_file), "images/test/")
  44. shutil.copy(os.path.join(label_dir, label_file), "labels/test/")
  45. # 生成训练集图片路径txt文件
  46. with open("train.txt", "w") as file:
  47. file.write("\n".join([root_path + "images/train/" + image_file for image_file in train_images]))
  48. # 生成验证集图片路径txt文件
  49. with open("val.txt", "w") as file:
  50. file.write("\n".join([root_path + "images/val/" + image_file for image_file in val_images]))
  51. # 生成测试集图片路径txt文件
  52. with open("test.txt", "w") as file:
  53. file.write("\n".join([root_path + "images/test/" + image_file for image_file in test_images]))
  54. print("数据划分完成!")

接着会生成划分好的数据集如下:

图片

打开images文件夹:

图片

打开images下的train文件夹:

图片

打开labels下的train文件夹:

图片

3 训练与预测

【1】开始训练

  1. 训练脚本如下:
  1. from ultralytics import YOLO
  2. # Load a model
  3. model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
  4. results = model.train(data='hand.yaml', epochs=30, imgsz=640, device=[0],
  5. workers=0,lr0=0.001,batch=8,amp=False)
  1. hand.yaml内容如下,注意修改自己的数据集路径即可:
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # COCO8 dataset (first 8 images from COCO train2017) by Ultralytics
  3. # Documentation: https://docs.ultralytics.com/datasets/detect/coco8/
  4. # Example usage: yolo train data=coco8.yaml
  5. # parent
  6. # ├── ultralytics
  7. # └── datasets
  8. # └── coco8 ← downloads here (1 MB)
  9. # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
  10. path: E:/Practice/DeepLearning/Yolo_Test/dataset/hand # dataset root dir
  11. train: E:/Practice/DeepLearning/Yolo_Test/dataset/hand/images/train # train images (relative to 'path') 4 images
  12. val: E:/Practice/DeepLearning/Yolo_Test/dataset/hand/images/val # val images (relative to 'path') 4 images
  13. test: # test images (optional)
  14. # Classes
  15. names:
  16. 0: hand-1
  17. 1: hand-2
  18. 2: hand-5
  19. # Download script/URL (optional)
  20. # download: https://ultralytics.com/assets/coco8.zip

CPU训练将device=[0]改为device='cpu'即可

训练完成后再runs/detect/train文件夹下生成如下内容:

  1. weights文件夹下生成两个模型文件,直接使用best.pt即可。

【2】预测推理

  1. 预测脚本如下:
  1. from ultralytics import YOLO
  2. # Load a model
  3. model = YOLO('best.pt') # pretrained YOLOv8n model
  4. # Run batched inference on a list of images
  5. results = model(['1 (1).jpg', '1 (2).jpg', '1 (3).jpg']) # return a list of Results objects
  6. # Process results list
  7. for result in results:
  8. boxes = result.boxes # Boxes object for bounding box outputs
  9. masks = result.masks # Masks object for segmentation masks outputs
  10. keypoints = result.keypoints # Keypoints object for pose outputs
  11. probs = result.probs # Probs object for classification outputs
  12. result.show() # display to screen
  13. result.save(filename='result.jpg') # save to disk
  1. 预测结果:

—THE END—

THE END!

文章结束,感谢阅读。您的点赞,收藏,评论是我继续更新的动力。大家有推荐的公众号可以评论区留言,共同学习,一起进步。


本文转载自: https://blog.csdn.net/csdn_xmj/article/details/137118560
版权归原作者 双木的木 所有, 如有侵权,请联系我们删除。

“OpenCV与AI深度学习 | 实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程)”的评论:

还没有评论