【openCV】人脸检测基础CascadeClassifier实例
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
#读图方法cv2 也可以使用plt.imread
def readtu(image):
img_read=cv2.imread(image)
img_read=cv2.cvtColor(img_read,cv2.COLOR_BGR2RGB)
return img_read
#显示图像方法
def showtu(image):
plt.axis('off')
plt.imshow(image)
image_son=readtu('/ai/baqiao.jpg')
showtu(image_son)
#级联分类器进行人脸识别
detector = cv2.CascadeClassifier('/ai/face_core/haarcascade_frontalface_default.xml')
#Parameters:
#cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not needed anymore, release it using cvReleaseHaarClassifierCascade(&cascade).
#image – Matrix of the type CV_8U containing an image where objects are detected.
#objects – Vector of rectangles where each rectangle contains the detected object.
#scaleFactor – 每次缩小图像的比例 每次检测都会缩小一次图像
#minNeighbors – 匹配成功的所需矩阵框数量 默认3
#flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
#minSize – 匹配人脸的最小范围
#maxSize – 匹配人脸的最大范围
rects=detector.detectMultiScale(image_son,scaleFactor=1.1,minNeighbors=2,minSize=(10,10),flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in rects:
#根据坐标画矩形框 rectangle参数 pt1:矩形的顶点(x,y) pt2:矩形的顶点pt1相反(x+w,y+h)。
cv2.rectangle(image_son,(x,y),(x+w,y+h),(0,255,0),2)
showtu(image_son)
正文到此结束