一、对照片进行人脸识别
1.代码
import cv2
img = cv2.imread('image',1)
face_engine = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
faces = face_engine.detectMultiScale(img,scaleFactor=1.3,minNeighbors=5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('new.jpg',img)
2.效果图
二、利用摄像头进行实时人脸识别
1.代码
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while (True):
# 获取摄像头拍摄到的画面
ret, frame = cap.read()
faces = face_cascade.detectMultiScale(frame, 1.3, 5)
img = frame
for (x, y, w, h)in faces:
# 画出人脸框,蓝色,画笔宽度微
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# 框选出人脸区域,在人脸区域而不是全图中进行人眼检测,节省计算资源
face_area = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(face_area)
# 用人眼级联分类器引擎在人脸区域进行人眼识别,返回的eyes为眼睛坐标列表
for (ex, ey, ew, eh)in eyes:
# 画出人眼框,绿色,画笔宽度为1
cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 1)
# 实时展示效果画面
cv2.imshow('frame2', img)
# 每5毫秒监听一次键盘动作
if cv2.waitKey(5) &0xFF ==ord('q'):
break
# 最后,关闭所有窗口
cap.release()
cv2.destroyAllWindows()
2.效果图
网友评论