OpenCV中训练CascadeClassifier(级联分类器)

参考:


Cascade Classifier Training
[OpenCV3]级联分类器训练——traincascade快速使用详解



级联分类器介绍


  • 文章总概:Cascade分类器全程级联增强弱分类器(boosted cascade of weak classifiers),而使用此分类器主要包括两方面------训练&定位。而麻烦的在于训练,如何训练,正是这篇文章将要介绍的:1.收集数据,2.准备训练数据以及训练模型。
  • 个人理解:人脸检测中一个比较好的算法是mtcnn网络,也是一个级联分类器,与Cascade的区别在于一个用的卷积神经网络,一个用的是Haar或者LBP提取特征。而弱分类器的原理可以总结为:一个分类器通过学习而能够分辨的特征的能力有限,但如果让多个分类器去学习识别不同的特征,这样通过多个分类器进行分类后,所产生的结果错误率将极大降低。



数据准备


不更新了,只能用CPU训练的速度太慢了,而且网上都说这种方法的正确率不太好,所以不更新了

  • 可以通过官方给的样本做正样本标签,也可以通过此工具labelImg,建议使用后者,后者在大数据集方面使用较多(列如微软VOC数据集)。

正样本以及负样本生成


正负样本生成脚本

import sys
import numpy as np
import xml.etree.ElementTree as ET
import cv2
import os
import numpy.random as npr
from utils import IoU
from utils import ensure_directory_exists

save_dir = "/home/rui"
anno_path = "./firepos/annotation"
im_dir = "./firepos/images"
pos_save_dir = os.path.join(save_dir, "./res/positive")
neg_save_dir = os.path.join(save_dir, './res/negative')

ensure_directory_exists(pos_save_dir)
ensure_directory_exists(neg_save_dir)


names_xml = os.listdir(anno_path)

img_rule_h = 45
img_rule_w = 45

size = img_rule_h

num = len(names_xml)
print "%d pics in total" % num
p_idx = 0 # positive
n_idx = 0 # negative
d_idx = 0 # dont care
idx = 0
box_idx = 0
for ne_xml in names_xml:
    tree = ET.parse(os.path.join(anno_path, ne_xml))
    root = tree.getroot()
    loc_bbox = []
    width_xml = root.find("size").find("width").text
    height_xml = root.find("size").find("height").text
    for node in root.findall('object'):
        label_ = node.find('name').text
        if label_ == "fire":
            xmin_ = node.find('bndbox').find('xmin').text
            ymin_ = node.find('bndbox').find('ymin').text
            xmax_ = node.find('bndbox').find('xmax').text
            ymax_ = node.find('bndbox').find('ymax').text
            loc_bbox.append(xmin_)
            loc_bbox.append(ymin_)
            loc_bbox.append(xmax_)
            loc_bbox.append(ymax_)
    im_path = "{}/{}".format(im_dir, ne_xml.split(".")[0])
    if os.path.exists(im_path + ".jpg"):
        im_path = "{}.jpg".format(im_path)
    else:
        im_path = "{}.JPG".format(im_path)
    boxes = np.array(loc_bbox, dtype=np.float32).reshape(-1, 4)
    img = cv2.imread(im_path)
    h, w, c =img.shape
    if h != int(height_xml) or w != int(width_xml):
        print h, height_xml,w,width_xml
        continue
    idx += 1
    if idx % 100 == 0:
        print idx, "images done"

    height, width, channel = img.shape

    neg_num = 0
    while neg_num < 700:
        size_new = 0.0
        if width > height:
            size_new = npr.randint(img_rule_h + 1, max(img_rule_h, height / 2 - 1))
        else:
            size_new = npr.randint(img_rule_w + 1, max(img_rule_w, width / 2 - 1))

        size_new = int(size_new)
        nx = npr.randint(0, width - size_new)
        ny = npr.randint(0, height - size_new)
        crop_box = np.array([nx, ny, nx + size_new, ny + size_new])
        Iou = IoU(crop_box, boxes)
        cropped_im = img[ny : ny + size_new, nx : nx + size_new, :]
        resized_im = cv2.resize(cropped_im, (img_rule_w, img_rule_h), interpolation=cv2.INTER_LINEAR)

        if len(Iou) != 0:
            if np.max(Iou) < 0.1:
            # Iou with all gts must below 0.3
                save_file = os.path.join(neg_save_dir, "%s.jpg"%n_idx)
                cv2.imwrite(save_file, resized_im)
                n_idx += 1
                neg_num += 1
        else:
            # Iou with all gts must below 0.3

            save_file = os.path.join(neg_save_dir, "%s.jpg"%n_idx)
            cv2.imwrite(save_file, resized_im)
            n_idx += 1
            neg_num += 1

    for box in boxes:
        # box (x_left, y_top, x_right, y_bottom)
        x1, y1, x2, y2 = box
        w = x2 - x1 + 1
        h = y2 - y1 + 1

#        if float(w) / h < 2:
#            continue

        # ignore small faces
        # in case the ground truth boxes of small faces are not accurate
        if w < img_rule_w or h < img_rule_h or x1 < 0 or y1 < 0:
            continue

        # generate positive examples and part faces
        pos_nums = 300
        while pos_nums > 0:
            size_new = npr.randint(int(pow(w * h, 0.5) - 1), int(max(w, h)))
            # delta here is the offset of box center

            delta_x = npr.randint(int(-size_new * 0.1), int(size_new * 0.1))
            delta_y = npr.randint(int(-size_new * 0.1), int(size_new * 0.1))

            nx1 = max(x1 + w / 2 + delta_x - size_new / 2, 0)
            ny1 = max(y1 + h / 2 + delta_y - size_new / 2, 0)
            nx2 = min(width, nx1 + size_new)
            ny2 = min(height, ny1 + size_new)
            if nx2 > width or ny2 > height:
                continue
            crop_box = np.array([nx1, ny1, nx2, ny2])

            cropped_im = img[int(ny1) : int(ny2), int(nx1) : int(nx2), :]
            resized_im = cv2.resize(cropped_im, (img_rule_w, img_rule_h))

            box_ = box.reshape(1, -1)

            pos_nums -= 1
            save_file = os.path.join(pos_save_dir, "%s.jpg"%p_idx)
            cv2.imwrite(save_file, resized_im)
            p_idx += 1
        box_idx += 1
        print "%s images done, pos: %s, neg: %s"%(idx, p_idx, n_idx)


将正样本写入

import os

pos_dir = "/home/rui/res/positive"

pos_list = os.listdir(pos_dir)

f = open("/home/rui/temp.txt", "w")

for im in pos_list:
    name = "positive/{} 1 0 0 45 45\n".format(im)
    print name
    f.writelines(name)
f.close()

find -name *.jpg >> neg.txt

待更新

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: OpenCV级联分类器快速训练工具是一个用于训练级联分类器的工具。级联分类器是一种用于目标检测的机器学习算法,它可以在图像快速识别并定位出特定的目标物体。 这个工具提供了一个简单而高效的方法,可以通过输入一系列正样本和负样本图像来训练级联分类器。正样本图像是包含待检测目标的图像,而负样本则是不包含目标的图像。 训练过程分为多个阶段,每个阶段都会对图像进行一系列的特征提取和分类器训练的操作。在每个阶段,分类器会根据当前的分类准确度和错误率进行更新和优化,从而逐步提高检测的准确性和速度。 值得注意的是,由于级联分类器训练是一个复杂且计算密集的过程,所以这个工具使用了一些优化策略来提高训练的效率。例如,它会自动选择最佳的特征子集、采用图像金字塔的方法来处理不同尺度的目标物体等。 使用这个工具,我们可以快速地训练出一个高效的级联分类器,用于在图像检测特定的目标物体。这个工具在计算机视觉领域有着广泛的应用,例如人脸检测、车牌识别、物体识别等。 ### 回答2: OpenCV级联分类器快速训练工具是一个用于训练级联分类器的工具,可以用于检测人脸、车辆、物体等。它是基于机器学习的技术,通过对大量正负样本的训练,自动生成一个分类器模型,用于在图像或视频进行目标检测。 该工具提供了简单、快速且高效的训练流程。首先,需要准备一组正样本和一组负样本图像。正样本包含待检测目标,负样本则不包含目标。通过提取图像特征,例如Haar-like特征,计算每个样本的特征向量。 接下来,使用AdaBoost算法进行强分类器训练。该算法通过选择最佳的特征来构建强分类器,以尽可能减少误检率。AdaBoost还会对错误分类的样本进行加权,以便更好地处理难以分类的样本。 训练完成后,通过级联分类器的方式将强分类器级联,构成一个多层次的分类系统。级联分类器能够高效地过滤掉大量的负样本,从而减少了计算量,提高了检测速度。 使用该工具,可以根据不同的应用需求进行参数的调整和优化,例如调整级联的层数、每层分类器的阈值等。这样可以在保证检测准确率的前提下,进一步提高检测速度。 总的来说,OpenCV级联分类器快速训练工具是一个功能强大且易于使用的工具,可用于快速训练目标检测模型,广泛应用于人机交互、智能安防、自动驾驶等领域。 ### 回答3: OpenCV级联分类器快速训练工具是一种用于训练级联分类器的工具,用于检测和识别特定对象。级联分类器是一种基于机器学习的目标检测算法,通过组合多个弱分类器来实现高效率的对象检测。 该工具可以帮助用户快速训练自定义的级联分类器,以便对特定对象进行检测。训练过程主要分为两个步骤:正样本收集和训练训练。 在正样本收集阶段,用户需要准备一组正样本图像,这些图像包含待检测的对象。工具通过采集这些正样本图像的对象特征,并根据这些特征构建级联分类器。 在训练训练阶段,工具利用正样本图像和一定数量的负样本图像,通过对这些图像进行分析和训练,逐步构建级联分类器模型。工具会根据正负样本之间的差异进行迭代训练,不断优化分类器的准确性和鲁棒性。 通过这个快速训练工具,用户可以有效地训练级联分类器,并应用于目标检测任务级联分类器在图像处理领域有广泛的应用,例如人脸检测、车辆检测等。它可以快速而准确地识别出感兴趣的对象,为图像分析和识别提供了有力支持。 总的来说,OpenCV级联分类器快速训练工具提供了一种方便、高效的方式,使用户能够自定义和训练级联分类器,从而实现对特定对象的快速检测和识别。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值