OpenCV中的静态人脸检测
代码实现:
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
from PIL import Image
import pytesseract as test
#静态人脸检测
img = cv2.imread('dilirema.png')
cv2.imshow('dilirema',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#加载分类器
faces_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt_tree.xml")
eyes_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
#detectMultiScale()第三个参数数值越大,检测错误率越低,但需要图像足够清晰,若不够清晰可能检测失败
faces = faces_cascade.detectMultiScale(gray,1.02,2)
eyes = eyes_cascade.detectMultiScale(gray,1.02,25)
for x,y,w,h in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
for x,y,w,h in eyes:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.namedWindow('face',cv2.WINDOW_AUTOSIZE)
cv2.imshow('face',img)
cv2.waitKey()
运行结果: