其他分享
首页 > 其他分享> > OpenCV中的静态人脸检测

OpenCV中的静态人脸检测

作者:互联网

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()

运行结果:
在这里插入图片描述

标签:eyes,img,cascade,静态,cv2,OpenCV,人脸,faces,import
来源: https://blog.csdn.net/remakeprogramer/article/details/113902467