yolo5 训练无人人机识别系统

简介: yolo5 训练无人人机识别系统

环境搭建:

安装驱动


点击鼠标右键,如果出现NVIDIA图标,点开,出现如下图片

我的显卡是1650,根据显卡的型号去官网找相应的驱动下载就好了。驱动官网

安装好之后,打开命令行cmd,输入如下指令:


nvidia-smi

显示出如下数据即为安装成功。

安装anaconda


进入官网,下载windows版本,将程序放在c盘以外,防止c盘爆红,接下来傻瓜式点击安装即可。

安装Pytorch

打开侧边栏,找到anaconda文件夹下面的prompt控制台


可以使用如下命令去看系统有哪些环境

conda env list

我的环境是这样的

其中pytorch1是我之前安装的环境,这里我们新创建一个环境pytorch,创建过程中一直输入y就好了。

conda create -n pytorch python=3.8

激活这个环境:

conda activate pytorch

为了提高安装速度,给环境换源:

1. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
2. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
3. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
4. conda config --set show_channel_urls yes

安装pytorch,我的显卡支持11.6的版本,你们可以查自己的显卡支持的版本

conda install pytorch torchvision torchaudio pytorch-cuda=11.6

数据集准备:


使用labelimg标注数据集,将数据集按照这样的目录结构存放



Annotations里面存放着xml格式的标签文件

JPEGImages里面存放着照片数据文件

数据集划分代码如下,放在VOCdevkit同一级目录下就可以运行。

classes = ["plane"]是因为数据集标注时的我打的标签是plane,所以要识别一下。


1. import xml.etree.ElementTree as ET
2. import pickle
3. import os
4. from os import listdir, getcwd
5. from os.path import join
6. import random
7. from shutil import copyfile
8. 
9. classes = ["plane"]
10. # classes=["ball"]
11. 
12. TRAIN_RATIO = 80
13. 
14. 
15. def clear_hidden_files(path):
16.     dir_list = os.listdir(path)
17. for i in dir_list:
18.         abspath = os.path.join(os.path.abspath(path), i)
19. if os.path.isfile(abspath):
20. if i.startswith("._"):
21.                 os.remove(abspath)
22. else:
23.             clear_hidden_files(abspath)
24. 
25. 
26. def convert(size, box):
27.     dw = 1. / size[0]
28.     dh = 1. / size[1]
29.     x = (box[0] + box[1]) / 2.0
30.     y = (box[2] + box[3]) / 2.0
31.     w = box[1] - box[0]
32.     h = box[3] - box[2]
33.     x = x * dw
34.     w = w * dw
35.     y = y * dh
36.     h = h * dh
37. return (x, y, w, h)
38. 
39. 
40. def convert_annotation(image_id):
41.     in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' % image_id)
42.     out_file = open('VOCdevkit/VOC2007/YOLOLabels/%s.txt' % image_id, 'w')
43.     tree = ET.parse(in_file)
44.     root = tree.getroot()
45.     size = root.find('size')
46.     w = int(size.find('width').text)
47.     h = int(size.find('height').text)
48. 
49. for obj in root.iter('object'):
50.         difficult = obj.find('difficult').text
51.         cls = obj.find('name').text
52. if cls not in classes or int(difficult) == 1:
53. continue
54.         cls_id = classes.index(cls)
55.         xmlbox = obj.find('bndbox')
56.         b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
57. float(xmlbox.find('ymax').text))
58.         bb = convert((w, h), b)
59.         out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
60.     in_file.close()
61.     out_file.close()
62. 
63. 
64. wd = os.getcwd()
65. wd = os.getcwd()
66. data_base_dir = os.path.join(wd, "VOCdevkit/")
67. if not os.path.isdir(data_base_dir):
68.     os.mkdir(data_base_dir)
69. work_sapce_dir = os.path.join(data_base_dir, "VOC2007/")
70. if not os.path.isdir(work_sapce_dir):
71.     os.mkdir(work_sapce_dir)
72. annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
73. if not os.path.isdir(annotation_dir):
74.     os.mkdir(annotation_dir)
75. clear_hidden_files(annotation_dir)
76. image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
77. if not os.path.isdir(image_dir):
78.     os.mkdir(image_dir)
79. clear_hidden_files(image_dir)
80. yolo_labels_dir = os.path.join(work_sapce_dir, "YOLOLabels/")
81. if not os.path.isdir(yolo_labels_dir):
82.     os.mkdir(yolo_labels_dir)
83. clear_hidden_files(yolo_labels_dir)
84. yolov5_images_dir = os.path.join(data_base_dir, "images/")
85. if not os.path.isdir(yolov5_images_dir):
86.     os.mkdir(yolov5_images_dir)
87. clear_hidden_files(yolov5_images_dir)
88. yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
89. if not os.path.isdir(yolov5_labels_dir):
90.     os.mkdir(yolov5_labels_dir)
91. clear_hidden_files(yolov5_labels_dir)
92. yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
93. if not os.path.isdir(yolov5_images_train_dir):
94.     os.mkdir(yolov5_images_train_dir)
95. clear_hidden_files(yolov5_images_train_dir)
96. yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
97. if not os.path.isdir(yolov5_images_test_dir):
98.     os.mkdir(yolov5_images_test_dir)
99. clear_hidden_files(yolov5_images_test_dir)
100. yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
101. if not os.path.isdir(yolov5_labels_train_dir):
102.     os.mkdir(yolov5_labels_train_dir)
103. clear_hidden_files(yolov5_labels_train_dir)
104. yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
105. if not os.path.isdir(yolov5_labels_test_dir):
106.     os.mkdir(yolov5_labels_test_dir)
107. clear_hidden_files(yolov5_labels_test_dir)
108. 
109. train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w')
110. test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w')
111. train_file.close()
112. test_file.close()
113. train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
114. test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
115. list_imgs = os.listdir(image_dir)  # list image files
116. prob = random.randint(1, 100)
117. print("Probability: %d" % prob)
118. for i in range(0, len(list_imgs)):
119.     path = os.path.join(image_dir, list_imgs[i])
120. if os.path.isfile(path):
121.         image_path = image_dir + list_imgs[i]
122.         voc_path = list_imgs[i]
123.         (nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
124.         (voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
125.         annotation_name = nameWithoutExtention + '.xml'
126.         annotation_path = os.path.join(annotation_dir, annotation_name)
127.         label_name = nameWithoutExtention + '.txt'
128.         label_path = os.path.join(yolo_labels_dir, label_name)
129.     prob = random.randint(1, 100)
130. print("Probability: %d" % prob)
131. if (prob < TRAIN_RATIO):  # train dataset
132. if os.path.exists(annotation_path):
133.             train_file.write(image_path + '\n')
134.             convert_annotation(nameWithoutExtention)  # convert label
135.             copyfile(image_path, yolov5_images_train_dir + voc_path)
136.             copyfile(label_path, yolov5_labels_train_dir + label_name)
137. else:  # test dataset
138. if os.path.exists(annotation_path):
139.             test_file.write(image_path + '\n')
140.             convert_annotation(nameWithoutExtention)  # convert label
141.             copyfile(image_path, yolov5_images_test_dir + voc_path)
142.             copyfile(label_path, yolov5_labels_test_dir + label_name)
143. train_file.close()
144. test_file.close()

运行后多生成了两个文件夹,如下:

至此,我们就得到了想要的数据集


训练模型

下载yolo5源码:


地址

导入数据


将VOCdevkit文件夹放入目录


安装环境依赖:


pip install -r requirements.txt

确定训练权重


yolov5的5.0版本给我们提供了几个预训练权重,我们可以对应我们不同的需求选择不同的版本的预训练权重。预训练权重可以通过这个网址进行下载,本次训练自己的数据集用的预训练权重为yolov5s.pt。


修改参数


预训练模型和数据集都准备好了,就可以开始训练自己的yolov5目标检测模型了,训练目标检测模型需要修改两个yaml文件中的参数。一个是data目录下的相应的yaml文件,一个是model目录文件下的相应的yaml文件。

一:

修改data目录下的相应的yaml文件。找到目录下的voc.yaml文件,将该文件复制一份,将复制的文件重命名,最好和项目相关。我这里修改为plane.yaml。该项目是对无人机的识别。


里面的内容改成这样:


1. # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/
2. # Train command: python train.py --data voc.yaml
3. # Default dataset location is next to /yolov5:
4. #   /parent_folder
5. #     /VOC
6. #     /yolov5
7. 
8. 
9. # download command/URL (optional)
10. #download: bash data/scripts/get_voc.sh
11. 
12. # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
13. train: VOCdevkit/images/train  # 16551 images
14. val: VOCdevkit/images/val  # 4952 images
15. 
16. # number of classes
17. nc: 1
18. 
19. # class names
20. names: [ 'plane']


二:

修改models目录下的相应的yaml文件。找到目录下的yolov5s.yaml文件,将该文件复制一份,将复制的文件重命名。我这里修改为yolov5s_plane.yaml。

将这里的识别对象改为1即可:


然后找到train.py文件,改这三个地方,改成我们刚才下载和修改的文件

1.     parser.add_argument('--weights', type=str, default='weights/yolov5x.pt', help='initial weights path')
2.     parser.add_argument('--cfg', type=str, default='models/yolov5s_plane.yaml', help='model.yaml path')
3.     parser.add_argument('--data', type=str, default='data/plane.yaml', help='data.yaml path')


以上我们把参数就都配置好了,运行的话可能会提示我们虚拟内存不够,这时我们需要修改这个地方。


把这个参数改成0

然后就可以开始训练了,训练结束后会生成一个run文件夹


应用模型:


找到run文件夹下面的best.pt文件,他通常在train文件夹下面的最后一个exp下面。


打开detect.py文件,修改这一出为我们训练好的文件


这个地方设置为0,就是调用电脑摄像头

我们这里在手机上下载一款应用,DroidCam,与电脑连接统一局域网,移动端显示如下:


我们修改detect.py中的配置

这样就连上了手机摄像头


连接数据库


我们先引入数据库的用户名,密码等配置


1. import pymysql
2. 
3. 
4. 
5. id = 5
6. def insert_sql():
7. # 建立连接
8.     conn = pymysql.connect(
9.         host="localhost",
10. # host="192.168.1.112",
11.         user="root",  # 用户名
12.         passwd="password111",  # 用户密码
13.         db="检测小车")  # 数据库名
14. 
15. global id
16. # 创建游标,默认是元组型
17.     cursor = conn.cursor()
18. # sql = "select * from t_plane"#数据库中表的名
19.     sql = '''INSERT INTO t_plane(id,x,y) VALUES(num,7,2);'''  # 数据库中表的名
20.     sql = sql.replace("num", str(id))
21.     cursor.execute(sql)
22.     conn.commit()
23. id += 1
24.     cursor.close()
25.     conn.close()


id为5,是因为我的数据id设置为自增的,现在有四条,所以id就暂时设置成5。

在程序中检测到物品都会给予一个评分,我们设置判断条件,但评分>0.9时,向数据库插入数据,意味着发现了目标



项目地址:

init_unmaned_planesystem: 无人机识别模型初始版

目录
相关文章
|
4月前
|
人工智能 运维 API
最新版阿里云百炼 Token Plan 个人版/团队版功能介绍
随着大模型应用快速普及,开发者同时使用多款不同能力的大模型已经成为常态。做原型开发时,既要调用文本推理大模型编写业务逻辑,又要调用图像生成模型输出素材,有时还需要视频生成、实时语音对话能力。传统模式下,每一类模型需要单独开通服务、单独管理密钥,不同产品计费口径互不兼容,预算分散,用量统计割裂,在调试Agent智能体、本地AI编程工具的时候,频繁切换模型就要修改大量配置,给个人开发者、工作室、中小企业带来不小的运维负担。百炼Token Plan作为面向交互式AI开发的订阅服务,核心就是解决多模型调用的管理难题,采用统一Credits积分抵扣机制,一份订阅权限可以调用平台内文本、图像、视频、语音等
571 0
|
5月前
|
数据采集 缓存 JSON
放弃逆向爬虫!京东商品详情 API,商用电商数据系统底层方案全解
本文深度解析京东商品详情API(联盟接口与JOS商家接口),对比爬虫风险,详解数据字段、五大商用场景(铺货、比价、选品、ERP、CPS)、技术实现要点及高频避坑指南,助力企业构建合规、稳定、可扩展的电商数据底座。
620 0
|
6月前
|
Web App开发 前端开发 JavaScript
从Chromium内核到浏览器指纹:指纹浏览器技术栈全解析
在跨境电商和海外社媒运营的技术栈里,指纹浏览器正从一个"可选工具"演变为"基础设施"。但对于许多运营团队来说,选型决策依据往往停留在"哪个界面好看""哪个口碑不错"的层面,缺乏对底层技术原理的系统理解。
|
5月前
|
人工智能 文字识别 监控
taobao.item.search.img(拍立淘图片搜索 API)全业务场景手册
拍立淘按图搜商品(taobao.item.search.img)是阿里视觉AI接口,支持图片URL或Base64输入,精准返回同款/相似商品ID、标题、价格、销量等结构化数据,广泛应用于跨境铺货、比价导购、竞品监控、品牌维权、智能选品等六大场景。(239字)
|
5月前
|
算法 API
企业纳税人信用查询-纳税人信用等级查询-一般纳税人信用查询API接口介绍
企业纳税信用分A-E五级(A级≥90分,D级<40分),反映企业税收遵从度。本API支持通过企业名称、统一信用代码等查询最新信用等级、评价年度及主管税务机关等信息,助力信用监管与商业决策。
552 0
|
SQL 存储 关系型数据库
PostgreSQL数据库指南: 自带指令命令一览表。
总结来说,掌握这些基本命令可以帮助你更有效地使用PostgreSQL数据库进行日常操作和管理工作。希望以上内容对你有所帮助!
841 6
|
11月前
|
网络协议 Linux 网络安全
Rocky Linux 下 DHCP 服务配置详细文档
DHCP(动态主机配置协议)是局域网中用于自动分配IP地址、网关、DNS等网络参数的核心协议,工作于UDP 67/68端口。通过“发现-提供-请求-确认”四步租约机制,实现客户端即插即用,提升IP利用率。支持固定IP绑定、跨子网中继及多网段超级作用域配置,广泛应用于各类网络环境。
|
机器学习/深度学习 数据采集 人工智能
文档智能 & RAG 让AI大模型更懂业务 —— 阿里云LLM知识库解决方案评测
随着数字化转型的深入,企业对文档管理和知识提取的需求日益增长。阿里云推出的文档智能 & RAG(Retrieval-Augmented Generation)解决方案,通过高效的内容清洗、向量化处理、精准的问答召回和灵活的Prompt设计,帮助企业构建强大的LLM知识库,显著提升企业级文档管理的效率和准确性。
|
人工智能 Rust PyTorch
Hibiki:实时语音翻译模型打破语言交流障碍!支持将语音实时翻译成其他语言的语音或文本
Hibiki 是由 Kyutai Labs 开发的实时语音翻译模型,能够将一种语言的语音实时翻译成另一种语言的语音或文本,支持高保真度和低延迟。
2364 18
Hibiki:实时语音翻译模型打破语言交流障碍!支持将语音实时翻译成其他语言的语音或文本

热门文章

最新文章