YDYの博客

一只有理想的菜鸟

harr识别

harr识别

  • Haar人脸识别方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
import numpy as np
facer = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascades/haarcascade_eye.xml')

img = cv2.imread('./image/face2.JPG')
print(img.shape)

scale_percent = 20 # 图像缩小到原来的50%
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
resized_img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
gray=cv2.cvtColor(resized_img,cv2.COLOR_BGR2GRAY)

facer = facer.detectMultiScale(gray,1.1,5)
i=0
for(x,y,w,h) in facer:
cv2.rectangle(resized_img,(x,y),(x+w,y+h),(0,0,255),2)
roi=gray[y:y+h,x:x+w]
eyes=eye.detectMultiScale(roi,1.1,5)

for (xeye,yeye,weye,heye) in eyes:
cv2.rectangle(resized_img,(x+xeye,y+yeye),(x+xeye+weye,y+yeye+heye),(255,0,0),2)

# i=i+1
# winname='face'+str(i)
# cv2.imshow(winname,roi)
cv2.imshow('img',resized_img)
cv2.waitKey()
cv2.destroyAllWindows()

QQ20240527152442.png