-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathface_recognition2
More file actions
42 lines (34 loc) · 1.63 KB
/
Copy pathface_recognition2
File metadata and controls
42 lines (34 loc) · 1.63 KB
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
33
34
35
36
37
38
39
40
41
42
# coding:utf8
import cv2
def detect():
# 创建人脸检测的对象
face_cascade = cv2.CascadeClassifier(r'D:\PyCharm\untitled\untitled\untitled\venv\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml')
# 创建眼睛检测的对象
eye_cascade = cv2.CascadeClassifier(r"D:\PyCharm\untitled\untitled\untitled\venv\Lib\site-packages\cv2\data\haarcascade_eye.xml")
# 连接摄像头的对象 0表示摄像头的编号
camera = cv2.VideoCapture(0)
while True:
# 读取当前帧
ret, frame = camera.read()
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 检测人脸 返回列表 每个元素都是(x, y, w, h)表示矩形的左上角和宽高
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# 画出人脸的矩形
for (x, y, w, h) in faces:
# 画矩形 在frame图片上画, 传入左上角和右下角坐标 矩形颜色 和线条宽度
img = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# 把脸单独拿出来
roi_gray = gray[y: y+h, x: x+w]
# 在脸上检测眼睛 (40, 40)是设置最小尺寸,再小的部分会不检测
eyes = eye_cascade.detectMultiScale(roi_gray, 1.03, 5, 0, (40, 40))
# 把眼睛画出来
for(ex, ey, ew, eh) in eyes:
cv2.rectangle(img, (x+ex, y+ey), (x+ex+ew, y+ey+eh), (0, 255, 0), 2)
cv2.imshow("camera", frame)
if cv2.waitKey(5) & 0xff == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
detect()