face_detection
import cv2
face_patterns = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades
/haarcascade_frontalface_default.xml')
sample_image = cv2.imread('one.jpg')
faces = face_patterns.detectMultiScale(sample_image,scaleFactor=1.1,minNeighbors=5,minSize=(100, 100))
for (x, y, w, h) in faces:
cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('after', sample_image)
CascadeClassifier是Opencv中做人脸检测时候的一个级联分类器,该类中封装的是目标检测机制即滑动窗口机制+级联分类器的方式。数据结构包括Data和FeatureEvaluator两个主要部分。Data中存储的是从训练获得的xml文件中载入的分类器数据;而FeatureEvaluator中是关于特征的载入、存储和计算。这里采用的训练文件是OpenCV中默认提供的haarcascade_frontalface_default.xml。至于Haar,LBP的具体原理,可以参考opencv的相关文档,简单地,可以理解为人脸的特征数据。
调用 CascadeClassifier 中的调detectMultiScale函数进行多尺度检测,多尺度检测中会调用单尺度的方法detectSingleScale。
参数说明:
这些参数都是可以针对图片进行调整的,处理结果返回一个人脸的矩形对象列表。
循环读取人脸的矩形对象列表,获得人脸矩形的坐标和宽高, 然后在原图片中画出该矩形框,调用的是OpenCV的rectangle 方法,其中矩形框的颜色等是可调整的。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# face_detect.py
# Face Detection using OpenCV. Based on sample code from:
# http://python.pastebin.com/m76db1d6b
# Usage: python face_detect.py
import sys, os
from opencv.cv import *
from opencv.highgui import *
from PIL import Image, ImageDraw
from math import sqrt
def detectObjects(image):
"""Converts an image to grayscale and prints the locations of any faces found"""
grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
cvCvtColor(image, grayscale, CV_BGR2GRAY)
storage = cvCreateMemStorage(0)
cvClearMemStorage(storage)
cvEqualizeHist(grayscale, grayscale)
cascade = cvLoadHaarClassifierCascade(
'/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
cvSize(1,1))
faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 2,
CV_HAAR_DO_CANNY_PRUNING, cvSize(20,20))
result = []
for f in faces:
result.append((f.x, f.y, f.x+f.width, f.y+f.height))
return result
def grayscale(r, g, b):
return int(r * .3 + g * .59 + b * .11)
def process(infile, outfile):
image = cvLoadImage(infile);
if image:
faces = detectObjects(image)
im = Image.open(infile)
if faces:
draw = ImageDraw.Draw(im)
for f in faces:
draw.rectangle(f, outline=(255, 0, 255))
im.save(outfile, "JPEG", quality=100)
else:
print "Error: cannot detect faces on %s" % infile
if __name__ == "__main__":
process('input.jpg', 'output.jpg')
如果需要脸部识别,那就把haarcascade_eye.xml改成haarcascade_frontalface_alt.xml就可以了
人脸检测并绘制眼口:
import cv2
filepath = "img/xingye-1.jpg"
img = cv2.imread(filepath) # 读取图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换灰色
# OpenCV人脸识别分类器
classifier = cv2.CascadeClassifier(
"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
color = (0, 255, 0) # 定义绘制颜色
# 调用识别人脸
faceRects = classifier.detectMultiScale(
gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects): # 大于0则检测到人脸
for faceRect in faceRects: # 单独框出每一张人脸
x, y, w, h = faceRect
# 框出人脸
cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
# 左眼
cv2.circle(img, (x + w // 4, y + h // 4 + 30), min(w // 8, h // 8),
color)
#右眼
cv2.circle(img, (x + 3 * w // 4, y + h // 4 + 30), min(w // 8, h // 8),
color)
#嘴巴
cv2.rectangle(img, (x + 3 * w // 8, y + 3 * h // 4),
(x + 5 * w // 8, y + 7 * h // 8), color)
cv2.imshow("image", img) # 显示图像
c = cv2.waitKey(10)
cv2.waitKey(0)
cv2.destroyAllWindows()