参考:
Cascade Classifier Training
[OpenCV3]级联分类器训练——traincascade快速使用详解
级联分类器介绍
- 文章总概:Cascade分类器全程级联增强弱分类器(boosted cascade of weak classifiers),而使用此分类器主要包括两方面------训练&定位。而麻烦的在于训练,如何训练,正是这篇文章将要介绍的:1.收集数据,2.准备训练数据以及训练模型。
- 个人理解:人脸检测中一个比较好的算法是mtcnn网络,也是一个级联分类器,与Cascade的区别在于一个用的卷积神经网络,一个用的是Haar或者LBP提取特征。而弱分类器的原理可以总结为:一个分类器通过学习而能够分辨的特征的能力有限,但如果让多个分类器去学习识别不同的特征,这样通过多个分类器进行分类后,所产生的结果错误率将极大降低。
数据准备
- 可以通过官方给的样本做正样本标签,也可以通过此工具labelImg,建议使用后者,后者在大数据集方面使用较多(列如微软VOC数据集)。
正样本以及负样本生成
正负样本生成脚本
import sys
import numpy as np
import .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_ = os.listdir(anno_path)
img_rule_h = 45
img_rule_w = 45
size = img_rule_h
num = len(names_ )
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_ in names_ :
tree = ET.parse(os.path.join(anno_path, ne_ ))
root = tree.getroot()
loc_bbox = []
width_ = root.find(\"size\").find(\"width\").text
height_ = root.find(\"size\").find(\"height\").text
for node in root.findall(\' \'):
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_ .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_ ) or w != int(width_ ):
print h, height_ ,w,width_
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
待更新
继续阅读与本文标签相同的文章
下一篇 :
java22 2018-12-18 作业
-
史上最强多线程面试44题和答案:线程锁+线程池+线程同步等
2026-05-18栏目: 教程
-
9月最新184道阿里、百度、腾讯、头条Java面试题合集
2026-05-18栏目: 教程
-
美团携手世界粮食计划署共推“拒绝隐性饥饿”健康饮食倡导行动
2026-05-18栏目: 教程
-
圆通回应“承诺达”解散:由直营模式改回加盟商授权经营
2026-05-18栏目: 教程
-
2019 年度 “CCF 杰出会员” 公布,清华北大等86人当选
2026-05-18栏目: 教程
