前言
本文主要以学习交流为主,初步体验深度学习的魅力和python框架的便捷,出现其他问题与作者无关
提示:本文禁止转载!
一、环境安装
笔者这里的python版本3.7.8
YOLOv5代码链接:https://github.com/ultralytics/yolov5
pip install -r requirements.txt
很多喜欢使用Anaconda,我不习惯用这个,也不推荐,自我感觉会弄混淆,一个一个库安装成功也是一种乐趣!
其中pytorch,pip下载超时可以去官网下载对应whl文件,pytorch下载链接:https://pytorch.org/
点开https://download.pytorch.org/whl/torch_stable.html,查找对应的版本好下载,pip安装whl文件命令
pip install xxx.whl
再安装pycocotools这个库的时候需要安装visual studio 2015,要不然会报错,迅雷种子安装链接
笔者用的是:ed2k://|file|cn_visual_studio_community_2015_x86_dvd_6847368.iso|4013920256|EB7F6605EDE67509E218E29173AC6574|/
安装visual_studio,可能会出现安装包丢失等问题,要安装证书链接:http://www.itmop.com/downinfo/387017.html。安装好之后还出先这个问题就直接跳过这个包就行了。
二、训练数据
1.数据准备
待测试图片,这里需要很多图片,应该很轻松获取的
其实这种简单的图片,python中的cv2库就可以很快识别缺口,但需要两张图片,一个滑块tu,一个背景图,示例代码如下:
class SlideCrack(object):
def __init__(self, gap, bg):
self.gap = gap
self.bg = bg
@staticmethod
def clear_white(img):
# 清除图片的空白区域,这里主要清除滑块的空白
img = cv2.imread(img)
rows, cols, channel = img.shape
min_x = 255
min_y = 255
max_x = 0
max_y = 0
for x in range(1, rows):
for y in range(1, cols):
t = set(img[x, y])
if len(t) >= 2:
if x <= min_x:
min_x = x
elif x >= max_x:
max_x = x
if y <= min_y:
min_y = y
elif y >= max_y:
max_y = y
img1 = img[min_x:max_x, min_y: max_y]
return img1
def template_match(self, tpl, target):
th, tw = tpl.shape[:2]
result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)
# 寻找矩阵(一维数组当作向量,用Mat定义) 中最小值和最大值的位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
tl = max_loc
return tl[0]
@staticmethod
def image_edge_detection(img):
edges = cv2.Canny(img, 100, 200)
return edges
def discern(self):
img1 = self.clear_white(self.gap)
img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
slide = self.image_edge_detection(img1)
back = cv2.imread(self.bg, 0)
back = self.image_edge_detection(back)
slide_pic = cv2.cvtColor(slide, cv2.COLOR_GRAY2RGB)
back_pic = cv2.cvtColor(back, cv2.COLOR_GRAY2RGB)
x = self.template_match(slide_pic, back_pic)
# 输出横坐标, 即 滑块在图片上的位置
return x
if __name__ == "__main__":
# 滑块图片
image1 = "small.png"
# 背景图片
image2 = "big.png"
sc = SlideCrack(image1, image2)
print(sc.discern())
图片有了,现在需要手动标注,pip安装
pip install labelimg
打开cmd,输入labelimg回车,标注流程如图,这里笔者标注了200个,真的是枯燥!
每个图片生成对应的xml文件,把这些文件放在同一个文件夹里,初始png图片的放在images下,xml放在Annotations下,如图
打开github下载的yolov5文件,新建my_trains文件夹,把上述2个文件夹放进去,如图
2.数据预处理
创建data_preprocessing1.py
import os
import random
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--xml_path', default='my_trains/Annotations', type=str, help='input xml label path')
parser.add_argument('--txt_path', default='my_trains/labels', type=str, help='output txt label path')
opt = parser.parse_args()
trainval_percent = 1.0
train_percent = 0.9
xmlfilepath = opt.xml_path
txtsavepath = opt.txt_path
total_xml = os.listdir(xmlfilepath)
if not os.path.exists(txtsavepath):
os.makedirs(txtsavepath)
num = len(total_xml)
list_index = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list_index, tv)
train = random.sample(trainval, tr)
file_trainval = open(txtsavepath + '/trainval.txt', 'w')
file_test = open(txtsavepath + '/test.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')
for i in list_index:
name = total_xml[i][:-4] + '\n'
if i in trainval:
file_trainval.write(name)
if i in train:
file_train