Haar Cascade人脸识别
如何训练Haar分类器
原理分析
使用Haar Cascade分类器进行人脸识别
将Python源文件中data/haarcascades目录复制到项目中。
def detect_face(img):
face_cascade = cv2.CascadeClassifier('./cascades/haarcascades/haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
def detect_eyes(img):
eye_cascade = cv2.CascadeClassifier('./cascades/haarcascades/haarcascade_eye.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
eyes = eye_cascade.detectMultiScale(gray, 1.03, 5, 0, (40, 40))
for (x, y, w, h) in eyes:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 1)
"""
def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None):
detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) -> objects
. @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
. of rectangles.
.
. @param image Matrix of the type CV_8U containing an image where objects are detected.
. @param objects Vector of rectangles where each rectangle contains the detected object, the
. rectangles may be partially outside the original image.
. @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
. @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
. to retain it.
. @param flags Parameter with the same meaning for an old cascade as in the function
. cvHaarDetectObjects. It is not used for a new cascade.
. @param minSize Minimum possible object size. Objects smaller than that are ignored.
. @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
.
. The function is parallelized with the TBB library.
"""
训练自己的Haar分类器
训练步骤
- 准备大量含有以及不含待识别物体的图片
- 创建含有待识别物体图片的向量文件(指定待识别物体在图片中的位置)
- 训练分类器
实现
下载安装opencv完整包,里面有要用的工具(opencv_createsamples等)
- 准备大量图像作为“消极”数据(我准备了3000+),所有图像的size要一样,我resize成了(300,300)
- 创建消极图像列表
def create_neg_list():
with open('neg.txt', 'w') as f:
for img in os.listdir('data/myhaar/neg'):
line = 'neg/' + img + '\n'
f.write(line)
- 准备大量含有待识别对象的“积极”数据
opencv_createsamples -img veno.jpg -bg neg.txt -info pos.txt -maxxangle 0.5 -maxyangle -0.5 -maxzangle 0.5 -num 3000
实际操作中,我准备了各个角度的veno共10张图片,每张图片各生成了300张积极图片
- 生成积极图像向量
opencv_createsamples -info pos.txt -num 3000 -w 100 -h 100 -vec pos.vec
- 训练
opencv_traincascade -data data -vec pos.vec -bg neg.txt -numPos 1800 -numNeg 900 -numStages 15 -w 100 -h 100 # pos一般是neg的1倍
- 识别
训练之后的cascade.xml文件在data目录下,用该xml进行物体识别
veno_cascade = cv2.CascadeClassifier('./cascades/haarcascades/veno_cascade.xml')
camera = cv2.VideoCapture(0)
success, frame = camera.read()
while success and cv2.waitKey(1) & 0xFF != ord('q'):
cv2.imshow('frame', frame)
success, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
veno = veno_cascade.detectMultiScale(gray, 1.5, 100) #调整参数
if veno is not None:
for (x, y, w, h) in veno:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.destroyAllWindows()
camera.release()
trouble shoot
error:
Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file
solution:
vec-file has to contain >= (numPos + (numStages-1) * (1 - minHitRate) * numPos) + S 说明:(S 即为 numNeg)
7000 >= (numPos + (20-1) * (1 - 0.999) * numPos) + 2973