人脸检测 | opencv实现人脸检测
opencv实现人脸检测
特征数据下载地址,完整代码下载地址
使用opencv已经训练好的haar特征xml文件,可以在图片或视频中检测出人脸、微笑、眼睛的坐标,利用该坐标完成进一步操作
环境准备pip install Pillow ; pip install opencv-python
# -*- coding: UTF-8 -*-
import cv2
def detectPicture(path, xml):
detector = cv2.CascadeClassifier(xml)
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.15, 5)
if len(faces) > 0:
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + w), (255, 255, 0), 2)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def detectVideo(xml):
detector = cv2.CascadeClassifier(xml)
cap = cv2.VideoCapture(1)
while True:
ret, image = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
cv2.imshow('frame', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
detectPicture(path, "haarcascades/haarcascade_frontalface_alt.xml")
# detectVideo("haarcascades/haarcascade_frontalface_alt.xml")
效果如图所示
完整代码,扫描目录下所有图片,并将处理完成的图片裁剪保存
# -*- coding: UTF-8 -*-
import os, cv2
from PIL import Image, ImageDraw
# 检测人脸
def detect(path, xml):
detector = cv2.CascadeClassifier(xml)
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.15, 5)
# if len(faces) > 0:
# for (x, y, w, h) in faces:
# cv2.rectangle(image, (x, y), (x + w, y + w), (255, 255, 0), 2)
#
# cv2.imshow("image", image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return faces
# 保存截图
def saveCrop(path, faces, destination):
imageName = os.path.basename(path).split('.')
count = 0
for (x, y, w, h) in faces:
savePath = os.path.join(destination,
imageName[0] + str(count) + "." + imageName[1])
Image.open(path).crop((x, y, x + w, y + w)).save(savePath)
count += 1
# 保存
def saveDraw(path, faces, destination):
image = Image.open(path)
imageName = os.path.basename(path).split('.')
draw = ImageDraw.Draw(image)
for (x, y, w, h) in faces:
draw.rectangle((x, y, x + w, y + w), outline = (255, 255, 0))
savePath = os.path.join(destination, imageName[0] + "_draw." + imageName[1])
image.save(savePath)
if __name__ == '__main__':
src = "images"
destination = "result"
if not os.path.exists(destination):
os.makedirs(destination)
files = os.listdir(src)
for file in files:
if not os.path.isdir(file):
path = os.path.join(src, file)
faces = detect(path, "haarcascades/haarcascade_frontalface_alt.xml")
saveCrop(path, faces, destination)
saveDraw(path, faces, destination)