基于OpenCV的路面质量检测的实现

2022-07-28,,

本期我们将展示一种对路面类型和质量进行分类的方法及其步骤。为了测试这种方法,我们使用了我们制作的rtk数据集。

路面分类

该数据集[1]包含用低成本相机拍摄的图像,以及新兴国家常见的场景,其中包含未铺砌的道路和坑洼。路面类型是有关人或自动驾驶车辆应如何驾驶的重要信息。除了乘客舒适度和车辆维护以外,它还涉及每个人的安全。我们可以通过[2]中的简单卷积神经网络(cnn)结构来实现。

在这种方法中,我们对表面类型分类任务使用特定的模型,我们将其定义为以下类别:沥青,已铺设(用于所有其他类型的路面)和未铺设。对于表面质量,我们使用其他三种不同的模型,每种类型的表面都使用一种。这四个模型都具有相同的结构。我们从第一个模型中得出结果,并称为特定质量模型。

在cnn结构之前,将感兴趣区域(roi)定义为每个输入帧的预处理步骤。毕竟,我们不需要整个图像来对道路进行分类。roi旨在仅保留图像中实际包含道路像素的部分。图像的上半部分以及图像底部的一小部分都将被丢弃,因为在某些帧中,它可能包含负责捕获图像的部分车辆。roi采用硬编码,因为如果我们使用自适应roi,它可能会导致失败并损害模型训练。

在此预处理之后执行数据扩充步骤。数据增强包括增加和减少每帧的亮度。这样,我们可以改进训练输入集,并帮助我们的系统学习识别具有不同照明条件的相同类型和质量的道路。

最后,将输入图像传递到包含三个卷积层和两个完全连接层的cnn结构。

01.rtk数据集

数据集包含具有不同类型的表面和质量的图像。

可从以下位置下载rtk数据集:

http://www.lapix.ufsc.br/pesquisas/projeto-veiculo-autonomo/datasets/?lang=zh-cn

02.路面类型分类

我们使用了python,tensorflow和opencv

让我们逐步分析一下…

首先,我们需要建立表面类型分类模型。为此,您将需要准备数据以训练模型。您可以使用rtk数据集中的图像或制作自己的图像。图像需要按地面道路类型进行组织。

训练数据文件夹结构

在我们的实验中,我们使用了6264帧:

l铺砌(沥青):4344,用于柏油马路。

l铺砌的(混凝土的):1337用于不同的人行道,例如鹅卵石。

l未铺砌:585用于未铺砌,土路,越野。

接下来,在train.py中,定义从何处收集训练数据。我们应该将20%的数据分开以自动用于验证。我们还定义了batch_size为32。

classes = os.listdir('training_data')
num_classes = len(classes)


batch_size = 32
validation_size = 0.2
img_size = 128
num_channels = 3
train_path='training_data'

在train.py上设置的参数将在dataset.py类上读取。

data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)

在dataset.py类中,我们定义了roi和数据扩充。带有数据解释功能的两个函数,adjust_gamma可以降低亮度,而adjust_gammaness可以提高亮度。

def adjust_gamma(image):
 gamma = 0.5
 invgamma = 1.0 / gamma
 table = np.array([((i / 255.0) ** invgamma) * 255 
 for i in np.arange(0, 256)]).astype("uint8")


 return cv2.lut(image, table)


def increase_brightness(img, value):
 hsv = cv2.cvtcolor(img, cv2.color_bgr2hsv)
 h, s, v = cv2.split(hsv)


 lim = 255 - value
 v[v > lim] = 255
 v[v <= lim] += value


 final_hsv = cv2.merge((h, s, v))
 img = cv2.cvtcolor(final_hsv, cv2.color_hsv2bgr)


 return img

加载输入数据时,将为每个图像定义roi。

for fields in classes:
 index = classes.index(fields)
 print('now going to read {} files (index: {})'.format(fields, index))
 path = os.path.join(train_path, fields, '*g')
 files = glob.glob(path)
 for fl in files:
 image = cv2.imread(fl)


 # region of interest (roi)
 height, width = image.shape[:2]
 newheight = int(round(height/2))
 image = image[newheight-5:height-50, 0:width]


 brght_img = increase_brightness(image, value=150)


 shaded_img = adjust_gamma(image)


 image = cv2.resize(image, (image_size, image_size),0,0, cv2.inter_linear)
 image = image.astype(np.float32)
 image = np.multiply(image, 1.0 / 255.0)


 brght_img = cv2.resize(brght_img, (image_size, image_size),0,0, cv2.inter_linear)
 brght_img = brght_img.astype(np.float32)
 brght_img = np.multiply(brght_img, 1.0 / 255.0)


 shaded_img = cv2.resize(shaded_img, (image_size, image_size),0,0, cv2.inter_linear)
 shaded_img = shaded_img.astype(np.float32)
 shaded_img = np.multiply(brght_img, 1.0 / 255.0)

我们还会平衡输入图像,因为沥青的图像更多,而未铺砌和未铺砌的道路更少。

if index == 0: #asphalt
 images.append(image)
 images.append(brght_img)
 images.append(shaded_img)
elif index == 1: #paved
 for i in range(3):
 images.append(image)
 images.append(brght_img)
 images.append(shaded_img)
elif index == 2: #unpaved
 for i in range(6):
 images.append(image)
 images.append(brght_img)
 images.append(shaded_img)

回到train.py,让我们定义tensorflow教程[2]中所示的cnn层。所有选择到训练步骤的图像都将传递到第一卷积层,其中包含有关通道的宽度,高度和数量的信息。前两层包含32个大小为3x3的滤镜。紧接着是一个具有3x3大小的64个滤镜的图层。所有的步幅都定义为1,填充的定义为0。正态分布用于权重初始化。为了在尺寸上减少输入,这有助于分析输入子区域中的特征信息,在所有卷积层中应用了最大池。在每个卷积层的末尾,在最大合并功能之后,将relu用作激活功能。

def create_convolutional_layer(input,
    num_input_channels, 
    conv_filter_size,  
    num_filters):


 weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
 biases = create_biases(num_filters)


 layer = tf.nn.conv2d(input=input,
      filter=weights,
      strides=[1, 1, 1, 1],
      padding='same')


 layer += biases


 layer = tf.nn.max_pool(value=layer,
       ksize=[1, 2, 2, 1],
       strides=[1, 2, 2, 1],
       padding='same')


 layer = tf.nn.relu(layer)

在卷积层之后,平坦层用于将卷积多维张量转换为一维张量。

def create_flatten_layer(layer):
 layer_shape = layer.get_shape()


 num_features = layer_shape[1:4].num_elements()


 layer = tf.reshape(layer, [-1, num_features])


 return layer

最后添加两个完全连接的层。在第一个完全连接的层中,应用了relu激活功能。第二个完全连接的层具有可能的输出,所需的类别。

def create_fc_layer(input,   
    num_inputs, 
    num_outputs,
    use_relu=true):


 weights = create_weights(shape=[num_inputs, num_outputs])
 biases = create_biases(num_outputs)


 layer = tf.matmul(input, weights) + biases
 if use_relu:
 layer = tf.nn.relu(layer)


 return layer

我们使用softmax函数来实现每个类的概率。最后,我们还使用adam优化器,该优化器根据训练中使用的输入数据更新网络权重。

layer_conv1 = create_convolutional_layer(input=x,
    num_input_channels=num_channels,
    conv_filter_size=filter_size_conv1,
    num_filters=num_filters_conv1)


layer_conv2 = create_convolutional_layer(input=layer_conv1,
    num_input_channels=num_filters_conv1,
    conv_filter_size=filter_size_conv2,
    num_filters=num_filters_conv2)


layer_conv3= create_convolutional_layer(input=layer_conv2,
    num_input_channels=num_filters_conv2,
    conv_filter_size=filter_size_conv3,
    num_filters=num_filters_conv3)


layer_flat = create_flatten_layer(layer_conv3)


layer_fc1 = create_fc_layer(input=layer_flat,
      num_inputs=layer_flat.get_shape()[1:4].num_elements(),
      num_outputs=fc_layer_size,
      use_relu=true)


layer_fc2 = create_fc_layer(input=layer_fc1,
      num_inputs=fc_layer_size,
      num_outputs=num_classes,
      use_relu=false) 


y_pred = tf.nn.softmax(layer_fc2,name='y_pred')


y_pred_cls = tf.argmax(y_pred, dimension=1)
session.run(tf.global_variables_initializer())
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,
             labels=y_true)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.adamoptimizer(learning_rate=1e-4).minimize(cost)
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

可以python train.py 在终端中训练模型的运行:

现在,有了经过训练的模型,我们就可以测试。首先,让我们准备好接收输入测试帧和输出文件名。

outputfile = sys.argv[2]


# opening frames
cap = cv.videocapture(sys.argv[1])


vid_writer = cv.videowriter(outputfile, cv.videowriter_fourcc('m','j','p','g'), 15, (round(cap.get(cv.cap_prop_frame_width)),round(cap.get(cv.cap_prop_frame_height))))

检索训练好的模型并访问图形。

sess = tf.session()
saver = tf.train.import_meta_graph('roadsurface-model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))


graph = tf.get_default_graph()


y_pred = graph.get_tensor_by_name("y_pred:0")


x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, len(os.listdir('training_data'))))

请记住,我们不需要整个图像,我们的培训着重于使用roi,在这里我们也使用它。

width = int(round(cap.get(cv.cap_prop_frame_width)))
height = int(round(cap.get(cv.cap_prop_frame_height)))


newheight = int(round(height/2))


while cv.waitkey(1) < 0:


 hasframe, images = cap.read()


 finalimg = images


 images = images[newheight-5:height-50, 0:width]
 images = cv.resize(images, (image_size, image_size), 0, 0, cv.inter_linear)
 images = np.array(images, dtype=np.uint8)
 images = images.astype('float32')
 images = np.multiply(images, 1.0/255.0)

最后,基于输出预测,我们可以在每帧中打印分类的表面类型。

x_batch = images.reshape(1, image_size, image_size, num_channels)


feed_dict_testing = {x: x_batch, y_true: y_test_images}
result = sess.run(y_pred, feed_dict=feed_dict_testing)


outputs = [result[0,0], result[0,1], result[0,2]]


value = max(outputs)
index = np.argmax(outputs)


if index == 0:
 label = 'asphalt'
 prob = str("{0:.2f}".format(value))
 color = (0, 0, 0)
elif index == 1:
 label = 'paved'
 prob = str("{0:.2f}".format(value))
 color = (153, 102, 102)
elif index == 2:
 label = 'unpaved'
 prob = str("{0:.2f}".format(value))
 color = (0, 153, 255)


cv.rectangle(finalimg, (0, 0), (145, 40), (255, 255, 255), cv.filled)
cv.puttext(finalimg, 'class: ', (5,15), cv.font_hershey_simplex, 0.5, (0,0,0), 1)
cv.puttext(finalimg, label, (70,15), cv.font_hershey_simplex, 0.5, color, 1)
cv.puttext(finalimg, prob, (5,35), cv.font_hershey_simplex, 0.5, (0,0,0), 1)


vid_writer.write(finalimg.astype(np.uint8))

可以测试在终端中运行的模型:python test.py path_to_your_frames_sequence name_your_video_file.avi。

03.路面质量分类

现在让我们包括质量分类。我们仅使用用于训练表面类型分类模型的相同cnn架构,并分别在每个表面类别上应用每个质量类别。因此,除了现有模型外,我们还培训了3种新模型。为此,大家将需要准备用于训练每个表面类别的模型的数据。在rtk数据集页面中,我们已经给出了按班级组织的框架。

用于质量课程的培训数据文件夹结构

要训练每种模型,小伙伴们可以在终端中运行:

python trainasphaltquality.py 
python trainpavedquality.py 
python trainunpavedquality.py

现在,预测部分发生了什么变化。我们使用四个不同的图,每个训练模型一个。

graph = tf.graph()
graphaq = tf.graph()
graphpq = tf.graph()
graphuq = tf.graph()

04.模型恢复

恢复类型模型

with graph.as_default():
 saver = tf.train.import_meta_graph('roadsurfacetype-model.meta')


 y_pred = graph.get_tensor_by_name("y_pred:0")


 x = graph.get_tensor_by_name("x:0")
 y_true = graph.get_tensor_by_name("y_true:0")
 y_test_images = np.zeros((1, len(os.listdir('training_data_type'))))


sess = tf.session(graph = graph)
saver.restore(sess, tf.train.latest_checkpoint('typecheckpoint/'))

恢复沥青质量模型

with graphaq.as_default():
 saveraq = tf.train.import_meta_graph('roadsurfaceasphaltquality-model.meta')


 y_predaq = graphaq.get_tensor_by_name("y_pred:0")


 xaq = graphaq.get_tensor_by_name("x:0")
 y_trueaq = graphaq.get_tensor_by_name("y_true:0")
 y_test_imagesaq = np.zeros((1, len(os.listdir('training_data_asphalt_quality'))))


sessaq = tf.session(graph = graphaq)
saveraq.restore(sessaq, tf.train.latest_checkpoint('asphaltcheckpoint/'))

恢复铺砌的质量模型

with graphpq.as_default():
 saverpq = tf.train.import_meta_graph('roadsurfacepavedquality-model.meta')


 y_predpq = graphpq.get_tensor_by_name("y_pred:0")


 xpq = graphpq.get_tensor_by_name("x:0")
 y_truepq = graphpq.get_tensor_by_name("y_true:0")
 y_test_imagespq = np.zeros((1, len(os.listdir('training_data_paved_quality'))))


sesspq = tf.session(graph = graphpq)
saverpq.restore(sesspq, tf.train.latest_checkpoint('pavedcheckpoint/'))

恢复未铺砌的质量模型

with graphuq.as_default():
 saveruq = tf.train.import_meta_graph('roadsurfaceunpavedquality-model.meta')


 y_preduq = graphuq.get_tensor_by_name("y_pred:0")


 xuq = graphuq.get_tensor_by_name("x:0")
 y_trueuq = graphuq.get_tensor_by_name("y_true:0")
 y_test_imagesuq = np.zeros((1, len(os.listdir('training_data_unpaved_quality'))))


sessuq = tf.session(graph = graphuq)
saveruq.restore(sessuq, tf.train.latest_checkpoint('unpavedcheckpoint/'))

此时,输出预测也要考虑质量模型,我们可以在每个帧中打印分类的表面类型以及该表面的质量。

if index == 0: #asphalt
  label = 'asphalt'
  prob = str("{0:.2f}".format(value))
  color = (0, 0, 0)
  x_batchaq = images.reshape(1, image_size, image_size, num_channels)


  feed_dict_testingaq = {xaq: x_batchaq, y_trueaq: y_test_imagesaq}
  resultaq = sessaq.run(y_predaq, feed_dict=feed_dict_testingaq)
  outputsq = [resultaq[0,0], resultaq[0,1], resultaq[0,2]]
  valueq = max(outputsq)
  indexq = np.argmax(outputsq)
  if indexq == 0: #asphalt - good
   quality = 'good'
   colorq = (0, 255, 0)
   probq = str("{0:.2f}".format(valueq))
  elif indexq == 1: #asphalt - regular
   quality = 'regular'
   colorq = (0, 204, 255)
   probq = str("{0:.2f}".format(valueq))
  elif indexq == 2: #asphalt - bad
   quality = 'bad'
   colorq = (0, 0, 255)
   probq = str("{0:.2f}".format(valueq)) 
 elif index == 1: #paved
  label = 'paved'
  prob = str("{0:.2f}".format(value))
  color = (153, 102, 102)
  x_batchpq = images.reshape(1, image_size, image_size, num_channels)


  feed_dict_testingpq = {xpq: x_batchpq, y_truepq: y_test_imagespq}
  resultpq = sesspq.run(y_predpq, feed_dict=feed_dict_testingpq)
  outputsq = [resultpq[0,0], resultpq[0,1], resultpq[0,2]]
  valueq = max(outputsq)
  indexq = np.argmax(outputsq)
  if indexq == 0: #paved - good
   quality = 'good'
   colorq = (0, 255, 0)
   probq = str("{0:.2f}".format(valueq))
  elif indexq == 1: #paved - regular
   quality = 'regular'
   colorq = (0, 204, 255)
   probq = str("{0:.2f}".format(valueq))
  elif indexq == 2: #paved - bad
   quality = 'bad'
   colorq = (0, 0, 255)
   probq = str("{0:.2f}".format(valueq))
 elif index == 2: #unpaved
  label = 'unpaved'
  prob = str("{0:.2f}".format(value))
  color = (0, 153, 255)
  x_batchuq = images.reshape(1, image_size, image_size, num_channels)


  feed_dict_testinguq = {xuq: x_batchuq, y_trueuq: y_test_imagesuq}
  resultuq = sessuq.run(y_preduq, feed_dict=feed_dict_testinguq)
  outputsq = [resultuq[0,0], resultuq[0,1]]
  valueq = max(outputsq)
  indexq = np.argmax(outputsq)
  if indexq == 0: #unpaved - regular
   quality = 'regular'
   colorq = (0, 204, 255)
   probq = str("{0:.2f}".format(valueq))
  elif indexq == 1: #unpaved - bad
   quality = 'bad'
   colorq = (0, 0, 255)
   probq = str("{0:.2f}".format(valueq))

打印结果

cv.rectangle(finalimg, (0, 0), (145, 80), (255, 255, 255), cv.filled)
cv.puttext(finalimg, 'class: ', (5,15), cv.font_hershey_duplex, 0.5, (0,0,0))
cv.puttext(finalimg, label, (70,15), cv.font_hershey_duplex, 0.5, color)
cv.puttext(finalimg, prob, (5,35), cv.font_hershey_duplex, 0.5, (0,0,0))
cv.puttext(finalimg, 'quality: ', (5,55), cv.font_hershey_duplex, 0.5, (0,0,0))
cv.puttext(finalimg, quality, (70,55), cv.font_hershey_duplex, 0.5, colorq)
cv.puttext(finalimg, probq, (5,75), cv.font_hershey_duplex, 0.5, (0,0,0))

大家可以在终端中测试运行情况:python testrtk.py path_to_your_frames_sequence name_your_video_file.avi。

一些结果样本:

致谢

lthiago rateke << span="">rateke.thiago@gmail.com>

lkarla aparecida justen << span="">justen.karla@gmail.com>

laldo von wangenheim << span="">aldo.vw@ufsc.br>

参考文献

[1] t. rateke, k. a. justen and a. von wangenheim, road surface classification with images captured from low-cost cameras — road traversing knowledge (rtk) dataset, (2019), revista de informática teórica e aplicada (rita)

[2] a. sachan, tensorflow tutorial 2: image classifier using convolutional neural network, (2017), cv-tricks.com

到此这篇关于基于opencv的路面质量检测的文章就介绍到这了,更多相关基于opencv的路面质量检测内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《基于OpenCV的路面质量检测的实现.doc》

下载本文的Word格式文档,以方便收藏与打印。