前言
第一节:
基于paddledetection在ROS中搭建红绿灯检测控制车模运动(1)—— 环境搭建准备以及软件安装
第二节:
基于paddledetection在ROS中搭建红绿灯检测控制车模运动(2)—— 数据集制作以及训练
第三节:
基于paddledetection在ROS中搭建红绿灯检测控制车模运动(3)—— 使用训练结果在ros中仿真红绿灯控制小车运动
项目地址
训练部分aistudio地址:传动门
racecar仿真软件下载地址:传送门
本地项目下载地址:传送门
数据准备
打开仿真环境:
roslaunch racecar_gazebo racecar_normal_light_runway.launch
在本地pycharm中新建一个工程文件:proj6_lightContrl/src在src下创建脚本读取ros中摄像头数据并按键截图保存本地,prtSc.py文件如下:
import rospy,cv2
from sensor_msgs.msg import Image
import sys
sys.path.append("~/catkin_workspace/install/lib/python3/dist-packages/")
import cv_bridge
class Follower:
def __init__(self):
self.bridge = cv_bridge.CvBridge()
#cv2.namedWindow("window", 1)
self.image_sub = rospy.Subscriber('/camera/zed/rgb/image_rect_color',
Image, self.image_callback)
self.num1 = 0
self.num2 = 0
def image_callback(self, msg):
self.image = self.bridge.imgmsg_to_cv2(msg,desired_encoding='bgr8')
cv2.imshow("pic",self.image)
k = cv2.waitKey(3)
if k == 27: # 按ESC退出
cv2.destroyAllWindows()
elif k == ord('r'): # 按r保存并退出
self.num1 += 1
cv2.imwrite("./red/" + str(self.num1) + ".jpg", self.image)
print("保存成功:" + str(self.num1) + ".jpg")
elif k == ord('g'): # 按r保存并退出
self.num2 += 1
cv2.imwrite("./green/" + str(self.num2) + ".jpg", self.image)
print("保存成功:" + str(self.num2) + ".jpg")
if __name__ == '__main__':
rospy.init_node('follower')
follower = Follower()
rospy.spin()
按键r保存摄像头图片到red文件夹下,按键g保存到green文件夹下:
调整角度和远近制作自己的数据集:
图片标注
cd labelImg
python labelImg.py
打开图片路径:
选择框选:
标注完成:
在aistudio上准备描述文件
在aistudio上创建项目(以下操作均在aistudio上进行)
首先下载paddledetection:
!git clone https://github.com/PaddlePaddle/PaddleDetection.git
将创建的red和green文件夹压缩上传到aistudio上的paddledetection中dataset下的my_light下执行解压命令:
%cd PaddleDetection-release-0.3/dataset/my_light/
!unzip picture.zip
安装paddledetection依赖:
%cd ~/PaddleDetection-release-0.3/
!pip install -r requirements.txt
测试aistudio环境:
#测试项目环境
!export PYTHONPATH=`pwd`:$PYTHONPATH
!python ppdet/modeling/tests/test_architectures.py
输出OK即可。
在my_light文件夹下创建green_xmls和red_xmls存放xml文件,方便记录txt:
%cd ~/PaddleDetection-release-0.3/dataset/my_light/
%mkdir green_xmls
%cd ./green
%mv *.xml ../green_xmls
%cd ~/PaddleDetection-release-0.3/dataset/my_light/
%mkdir red_xmls
%cd ./red
%mv *.xml ../red_xmls
记录xml和picture的关系:
# 记录train图片
import os
import random
path = "/home/aistudio/PaddleDetection-release-0.3/dataset/my_light/"
total_red = os.listdir(path + "red/")
total_green = os.listdir(path + "green/")
num = len(total_red)
list=range(num)
ftrain = open('/home/aistudio/PaddleDetection-release-0.3/dataset/my_light/train.txt', 'w')
fval = open('/home/aistudio/PaddleDetection-release-0.3/dataset/my_light/val.txt', 'w')
for i in list:
if i % 9 != 0:
name=total_red[i][:-4]
writeName = "red/" + name + ".jpg " + "red_xmls/" + name + ".xml\n"
ftrain.write(writeName)
else:
name=total_red[i][:-4]
writeName = "red/" + name + ".jpg " + "red_xmls/" + name + ".xml\n"
fval.write(writeName)
for i in list:
if i % 9 != 0:
name=total_green[i][:-4]
writeName = "green/" + name + ".jpg " + "green_xmls/" + name + ".xml\n"
ftrain.write(writeName)
else:
name=total_green[i][:-4]
writeName = "green/" + name + ".jpg " + "green_xmls/" + name + ".xml\n"
fval.write(writeName)
ftrain.close()
fval.close()
输出的train.txt如下:
输出的val.txt类似。
==数据好像有点少哎!==
在train.txt同目录创建label_list.txt:
训练
修改configs/ssd/ssd_mobilenet_v1_voc.yml这个文件,主要修改位置:
- num_classes: 3
-
trainReader下的anno_path和dataset_dir
anno_path: train.txt
dataset_dir: dataset/my_light -
EvalReader下的anno_path和dataset_dir
anno_path: val.txt
dataset_dir: dataset/my_light -
TestReader下的anno_path和dataset_dir:
dataset_dir: dataset/my_light
anno_path: label_list.txt
训练:
#训练模型
%cd ~/PaddleDetection-release-0.3/
# ssd训练
!python -u tools/train.py -c configs/ssd/ssd_mobilenet_v1_voc.yml -o --eval
等待训练完成即可: