安装OpenCV-python :

pip3 install opencv-python

 

代码 :

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cv2

#加载OpenCV 自带人脸特征模型
defStr = r'D:\Program Files\Python\Python37-32\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'

#输入识别的图片地址
pathUrl = r'C:\\projects\\img2.jpg'

#加载模型
faceCascade = cv2.CascadeClassifier(defStr)

#加载图片
image = cv2.imread(pathUrl)

#灰度转换
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#识别人脸检查参数,识别不准确在这里调整
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.0026,
    minNeighbors=4,
    minSize=(4,4)
)

print("发现{0}个人脸!".format(len(faces)))
for(x,y,w,h) in faces:
    print(x,y,w,h)
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
    #cv2.circle(image,((x+x+w)/2,(y+y+h)/2),w/2,(0,255,0),2)


#显示图像
cv2.imshow("Gakki!",image)
cv2.waitKey(0)

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

 

识别专业团队效果 ~

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐