import cv2
import numpy as np
cv2.namedWindow("Face_Detect") #定义一个窗口
cap=cv2.VideoCapture(0) #捕获摄像头图像
success,frame=cap.read() #读入第一帧
classifier=cv2.CascadeClassifier("C:/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml")
#定义人脸识别的分类数据集,需要自己查找,在opencv的目录下,参考上面我的路径
while success:#如果读入帧正常
size=frame.shape[:2]
image=np.zeros(size,dtype=np.float16)
image=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.equalizeHist(image,image)
divisor=8
h,w=size
minSize=(int(w/divisor),int(h/divisor)) #像素一定是整数,或者用w//divisor
faceRects=classifier.detectMultiScale(image,1.2,2,cv2.CASCADE_SCALE_IMAGE,minSize)
#人脸识别
if len(faceRects)> 0:
for faceRect in faceRects:
x,y,w,h=faceRect
cv2.circle(frame,(x+w//2,y+h//2),min(w//2,h//2),(255,0,0),2) #圆形轮廓
cv2.circle(frame,(x+w//4,y+2*h//5),min(w//8,h//8),(0,255,0),2) #左眼轮廓
cv2.circle(frame,(x+3*w//4,y+2*h//5),min(w//8,h//8),(0,255,0),2)#右眼轮廓
cv2.circle(frame,(x+w//2,y+2*h//3),min(w//8,h//8),(0,255,0),2) #鼻子轮廓
cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255),2) #矩形轮廓
cv2.imshow("Face_Detect",frame)
#显示轮廓
success,frame=cap.read()#如正常则读入下一帧
c=chr(key&255)
if c in ['q','Q',chr(27)]:#如果键入‘q’退出循环
print('exit'\n)
break#退出循环
循环结束则清零
cap.release()
cv2.destroyAllWindows()
网友评论