我是靠谱客的博主 傲娇嚓茶,这篇文章主要介绍face_recognition、opencv中haar人脸特征:视频/图片 进行 人脸检测/人脸识别日萌社,现在分享给大家,希望可以做个参考。

日萌社

 

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import face_recognition import cv2 # 读入影片并得到影片长度 # input_movie = cv2.VideoCapture("Huanlesong1.mp4") input_movie = cv2.VideoCapture("shot.mp4") #获取视频帧数 length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT)) print("帧数:",length) #1754 """ VideoWriter(filename, fourcc, fps, frameSize[, isColor]) 第一个参数:是要保存的文件的路径 第二个参数: cv2.VideoWriter_fourcc(*'XVID'):指定编码器 推荐使用 ”XVID",但一般依据你的电脑环境安装了哪些编码器。 本身是一个 32 位的无符号数值,用 4 个字母表示采用的编码器。 常用的有 “DIVX"、”MJPG"、“XVID”、“X264"。可用的列表在这里。 第三个参数:fps 要保存的视频的帧速率。(注意:可以右键查看所读取的视频文件的帧速率) 第四个参数:frameSize 要保存的文件的画面尺寸(帧高度/帧宽度)。(注意:可以右键查看所读取的视频文件的帧高度/帧宽度) 第五个参数:isColor 指示是黑白画面还是彩色的画面。(输出格式,等于0时输出灰度视频,不等于0时输出彩色视频) """ # 创建输出视频文件(确保输出视频文件的分辨率/帧速率与输入视频匹配) fourcc = cv2.VideoWriter_fourcc(*'XVID') #可以右键查看所读取的视频文件的帧速、率帧高度、帧宽度 output_movie = cv2.VideoWriter('output.avi', fourcc, 25, (640, 360)) """ 视频中有3个人,但是目的只是识别中视频中2个人,因此下面仅读取其中2个人的人脸图片, 让模型学会根据这两个人的人脸图片在视频中分别识别出这两个人,并标识出来。 """ """ 图像载入函数 load_image_file load_image_file(file, mode='RGB') 加载一个图像文件到一个numpy array类型的对象上。 参数: file:待加载的图像文件名字 mode:转换图像的格式 只支持“RGB”(8位RGB, 3通道)和“L”(黑白) 返回值: 一个包含图像数据的numpy array类型的对象 """ # 加载一些示例图片并学习如何识别它们。 lmm_image = face_recognition.load_image_file("Qidian.png") al_image = face_recognition.load_image_file("Quxiaoxiao.png") """ 人脸编码函数 face_encodings face_encodings(face_image, known_face_locations=None, num_jitters=1) 给定一个图像,返回图像中每个人脸的128脸部编码(特征向量)。 参数: face_image:输入的人脸图像 known_face_locations:可选参数,如果你知道每个人脸所在的边界框 num_jitters=1:在计算编码时要重新采样的次数。越高越准确,但速度越慢(100就会慢100倍) 返回值: 一个128维的脸部编码列表 """ #获取一个128维的脸部编码列表 lmm_face_encoding = face_recognition.face_encodings(lmm_image)[0] al_face_encoding = face_recognition.face_encodings(al_image)[0] known_faces = [ lmm_face_encoding, al_face_encoding ] # 初始化一些变量 face_locations = [] face_encodings = [] face_names = [] frame_number = 0 while True: # 抓取视频中的每一帧 ret, frame = input_movie.read() #统计帧数 frame_number += 1 # 输入视频文件结束时退出 if not ret: break # 将图像从BGR颜色(OpenCV使用)转换为RGB颜色(人脸识别使用) rgb_frame = frame[:, :, ::-1] """ face_locations(img, number_of_times_to_upsample=1, model='hog') 给定一个图像,返回图像中每个人脸的面部特征位置(眼睛、鼻子等),也即是获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left)。 参数: img:一个image(numpy array类型) number_of_times_to_upsample:从images的样本中查找多少次人脸,该参数值越高的话越能发现更小的人脸。 model:使用哪种人脸检测模型。“hog” 准确率不高,但是在CPUs上运行更快, “cnn” 更准确更深度(且 GPU/CUDA加速,如果有GPU支持的话),默认是“hog” 返回值: 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) """ # 获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left) # 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) face_locations = face_recognition.face_locations(rgb_frame) #face_encodings获取一个128维的脸部编码列表,其中传入第二个参数known_face_locations(可选参数,如果你知道每个人脸所在的边界框 ) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) face_names = [] # 遍历列表中 每个128维的脸部编码 for face_encoding in face_encodings: """ compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6) 比较脸部编码列表和候选编码,看看它们是否匹配。 参数: known_face_encodings:已知的人脸编码列表 face_encoding_to_check:待进行对比的单张人脸编码数据 tolerance:两张脸之间的距离小于相似阈值才算匹配。该值越小对比越严格,0.6是典型的最佳值。 返回值: 一个 True或者False值的列表,代表了known_face_encodings列表的每个成员的匹配结果 注意:相似阈值(tolerance容忍度)中0.6是典型的最佳值,该值越小对比越严格,即两张脸之间的距离小于相似阈值才算匹配。 比如 face_distance 计算出来的距离为 0.4和 0.8,然后设置的相似阈值(tolerance容忍度)为0.6, 那么只有 0.4 小于 0.6 才认为是 相似, 0.8 大于 0.6 则认为是 不相似。 其中compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6) 也可以设置相似阈值(tolerance容忍度) """ # 查看该脸是否与已知脸匹配 match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50) # print(match) """ match列表中的True或者False值 对应 known_faces = [lmm_face_encoding, al_face_encoding] """ # 如果你有两张以上的脸,你可以让这个逻辑更漂亮 但是为了演示我把它简单化了 name = None if match[0]: name = "Qidian" face_names.append(name) elif match[1]: name = "Quxiaoxiao" face_names.append(name) # 标记结果 #列表中的每个元组包含人脸的位置(top, right, bottom, left) for (top, right, bottom, left), name in zip(face_locations, face_names): if not name: continue # 根据 人脸的位置(top, right, bottom, left) 在脸上画一个方框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 画一个带文本信息的方框 cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED) #设置文本信息的字体 font = cv2.FONT_HERSHEY_DUPLEX #要填入的文本信息 cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1) # 将生成的图像写入输出视频文件 print("Writing frame {} / {}".format(frame_number, length)) output_movie.write(frame) input_movie.release() cv2.destroyAllWindows()
复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from PIL import Image, ImageDraw import face_recognition """ 图像载入函数 load_image_file load_image_file(file, mode='RGB') 加载一个图像文件到一个numpy array类型的对象上。 参数: file:待加载的图像文件名字 mode:转换图像的格式 只支持“RGB”(8位RGB, 3通道)和“L”(黑白) 返回值: 一个包含图像数据的numpy array类型的对象 """ image = face_recognition.load_image_file('two_people.jpg') """ 人脸特征提取函数 face_landmarks(face_image,face_locations=None,model="large") 给定一个图像,提取图像中每个人脸的脸部特征位置。 人脸特征提取函数face_landmarks 提取后的脸部特征包括: 鼻梁nose_bridge、鼻尖nose_tip、 下巴chin、左眼left_eye、右眼right_eye、左眉 left_eyebrow、 右眉right_eyebrow、上唇top_lip、下 唇bottom_lip 参数: face_image:输入的人脸图片 face_locations=None: 可选参数,默认值为None,代表默认解码图片中的每一个人脸。 若输入face_locations()[i]可指定人脸进行解码 model="large"/"small": 输出的特征模型,默认为“large”,可选“small”。 当选择为"small"时,只提取左眼、右眼、鼻尖这三种脸部特征。 """ face_landmarks_list = face_recognition.face_landmarks(image) #获取到图片中所包含的两个人的人脸特征 print('找到 {} 个脸在这张图中'.format(len(face_landmarks_list))) #2 #加载图片 pil_image = Image.fromarray(image) """ Python图像处理库PIL的ImageDraw模块. ImageDraw模块提供了图像对 象的简单2D绘制。用户可以 使用这个模块创建新的图像,注释或润饰已存在图像,为 web应用实时产生各种图形 """ d = ImageDraw.Draw(pil_image) i = 1 """ 遍历图片中可能存在的多个人脸特征数据 """ for face_landmarks in face_landmarks_list: print('第 {} 个人的脸在这张图中'.format(i)) i+=1 """ keys():获取鼻梁nose_bridge、鼻尖nose_tip、 下巴chin、左眼left_eye、右眼right_eye、左眉 left_eyebrow、 右眉right_eyebrow、上唇top_lip、下 唇bottom_lip """ for facial_feature in face_landmarks.keys(): print('脸部特征:{}。脸部特征值: {}'.format(facial_feature, face_landmarks[facial_feature])) """ 1.polygon()方法用于绘制多边形 第一个参数是多边形的几个顶点位置组成的list,第二个参数fill是填充该多边形的颜色。 例子:d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128)) 2.line()方法是用来画多个点之间构成的线段 第一个参数是 点位置组成的list,第二个参数fill是线段的颜色,第三个参数width是线段的宽度。 例子:d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5) """ d.line(face_landmarks[facial_feature],width=5) pil_image.show()
复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from PIL import Image, ImageDraw import face_recognition """ 图像载入函数 load_image_file load_image_file(file, mode='RGB') 加载一个图像文件到一个numpy array类型的对象上。 参数: file:待加载的图像文件名字 mode:转换图像的格式 只支持“RGB”(8位RGB, 3通道)和“L”(黑白) 返回值: 一个包含图像数据的numpy array类型的对象 """ # image = face_recognition.load_image_file("obama.jpg") image = face_recognition.load_image_file("threepeople.jpg") """ 人脸特征提取函数 face_landmarks(face_image,face_locations=None,model="large") 给定一个图像,提取图像中每个人脸的脸部特征位置。 人脸特征提取函数face_landmarks 提取后的脸部特征包括: 鼻梁nose_bridge、鼻尖nose_tip、 下巴chin、左眼left_eye、右眼right_eye、左眉 left_eyebrow、 右眉right_eyebrow、上唇top_lip、下 唇bottom_lip 参数: face_image:输入的人脸图片 face_locations=None: 可选参数,默认值为None,代表默认解码图片中的每一个人脸。 若输入face_locations()[i]可指定人脸进行解码 model="large"/"small": 输出的特征模型,默认为“large”,可选“small”。 当选择为"small"时,只提取左眼、右眼、鼻尖这三种脸部特征。 """ face_landmarks_list = face_recognition.face_landmarks(image) #加载图片 pil_image = Image.fromarray(image) """ Python图像处理库PIL的ImageDraw模块. ImageDraw模块提供了图像对 象的简单2D绘制。用户可以 使用这个模块创建新的图像,注释或润饰已存在图像,为 web应用实时产生各种图形 """ #载入图片,构建一个ImageDraw对象 d = ImageDraw.Draw(pil_image, 'RGBA') """ 遍历图片中可能存在的多个人脸特征数据 """ for face_landmarks in face_landmarks_list: """ 1.polygon()方法用于绘制多边形 第一个参数是多边形的几个顶点位置组成的list,第二个参数fill是填充该多边形的颜色。 例子:d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128)) 2.line()方法是用来画多个点之间构成的线段 第一个参数是 点位置组成的list,第二个参数fill是线段的颜色,第三个参数width是线段的宽度。 例子:d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5) """ # 画个浓眉 d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128)) d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128)) d.line(face_landmarks['left_eyebrow'], fill=(1,1, 1, 1), width=15) #d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5) d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5) # 涂个性感的嘴唇 d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128)) d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128)) d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8) d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8) # 闪亮的大眼睛 d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30)) d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30)) # 画眼线 d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6) d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6) pil_image.show()

复制代码
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
43
44
45
46
47
48
49
50
import face_recognition import cv2 from PIL import Image,ImageDraw """ 图像载入函数 load_image_file load_image_file(file, mode='RGB') 加载一个图像文件到一个numpy array类型的对象上。 参数: file:待加载的图像文件名字 mode:转换图像的格式 只支持“RGB”(8位RGB, 3通道)和“L”(黑白) 返回值: 一个包含图像数据的numpy array类型的对象 """ # 加载一些示例图片并学习如何识别它们。 rgb_frame = face_recognition.load_image_file("2.jpg") #加载图片 pil_image = Image.fromarray(rgb_frame) """ face_locations(img, number_of_times_to_upsample=1, model='hog') 给定一个图像,返回图像中每个人脸的面部特征位置(眼睛、鼻子等),也即是获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left)。 参数: img:一个image(numpy array类型) number_of_times_to_upsample:从images的样本中查找多少次人脸,该参数值越高的话越能发现更小的人脸。 model:使用哪种人脸检测模型。“hog” 准确率不高,但是在CPUs上运行更快, “cnn” 更准确更深度(且 GPU/CUDA加速,如果有GPU支持的话),默认是“hog” 返回值: 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) """ # 获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left) # 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) face_locations = face_recognition.face_locations(rgb_frame) print(face_locations) # face_encodings获取一个128维的脸部编码列表,其中传入第二个参数known_face_locations(可选参数,如果你知道每个人脸所在的边界框 ) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) top, right, bottom, left = face_locations[0] face_names = "nagisa" # 根据 人脸的位置(top, right, bottom, left) 在脸上画一个方框 cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 0, 255), 2) # 画一个带文本信息的方框 cv2.rectangle(rgb_frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED) # 设置文本信息的字体 font = cv2.FONT_HERSHEY_DUPLEX # 要填入的文本信息 cv2.putText(rgb_frame, face_names, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1) # 彩色显示图像 cv2.imshow("img", rgb_frame[:, :, ::-1]) # 保持画面的持续。 cv2.waitKey(0)
复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import face_recognition import cv2 from PIL import Image """ 图像载入函数 load_image_file load_image_file(file, mode='RGB') 加载一个图像文件到一个numpy array类型的对象上。 参数: file:待加载的图像文件名字 mode:转换图像的格式 只支持“RGB”(8位RGB, 3通道)和“L”(黑白) 返回值: 一个包含图像数据的numpy array类型的对象 """ # 加载一些示例图片并学习如何识别它们。 lmm_image = face_recognition.load_image_file("2.jpg") mask_image = face_recognition.load_image_file("1.jpg") """ 人脸编码函数 face_encodings face_encodings(face_image, known_face_locations=None, num_jitters=1) 给定一个图像,返回图像中每个人脸的128脸部编码(特征向量)。 参数: face_image:输入的人脸图像 known_face_locations:可选参数,如果你知道每个人脸所在的边界框 num_jitters=1:在计算编码时要重新采样的次数。越高越准确,但速度越慢(100就会慢100倍) 返回值: 一个128维的脸部编码列表 """ #获取一个128维的脸部编码列表 face_encoding = face_recognition.face_encodings(lmm_image)[0] # print(face_encoding) known_faces = [face_encoding] # 初始化一些变量 face_locations = [] face_encodings = [] face_names = [] frame_number = 0 # 调用笔记本内置摄像头,所以参数为0,如果有其他的摄像头可以调整参数为1,2 cap = cv2.VideoCapture(0) while True: # 从摄像头读取图片 sucess, frame = cap.read() # 将图像从BGR颜色(OpenCV使用)转换为RGB颜色(人脸识别使用) rgb_frame = frame[:, :, ::-1] """ face_locations(img, number_of_times_to_upsample=1, model='hog') 给定一个图像,返回图像中每个人脸的面部特征位置(眼睛、鼻子等),也即是获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left)。 参数: img:一个image(numpy array类型) number_of_times_to_upsample:从images的样本中查找多少次人脸,该参数值越高的话越能发现更小的人脸。 model:使用哪种人脸检测模型。“hog” 准确率不高,但是在CPUs上运行更快, “cnn” 更准确更深度(且 GPU/CUDA加速,如果有GPU支持的话),默认是“hog” 返回值: 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) """ # 获取每个人脸所在的边界框/人脸的位置(top, right, bottom, left) # 一个元组列表,列表中的每个元组包含人脸的位置(top, right, bottom, left) face_locations = face_recognition.face_locations(rgb_frame) # print(face_locations) #face_encodings获取一个128维的脸部编码列表,其中传入第二个参数known_face_locations(可选参数,如果你知道每个人脸所在的边界框 ) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) face_names = [] # 遍历列表中 每个128维的脸部编码 for face_encoding in face_encodings: """ compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6) 比较脸部编码列表和候选编码,看看它们是否匹配。 参数: known_face_encodings:已知的人脸编码列表 face_encoding_to_check:待进行对比的单张人脸编码数据 tolerance:两张脸之间的距离小于相似阈值才算匹配。该值越小对比越严格,0.6是典型的最佳值。 返回值: 一个 True或者False值的列表,代表了known_face_encodings列表的每个成员的匹配结果。 注意:相似阈值(tolerance容忍度)中0.6是典型的最佳值,该值越小对比越严格,即两张脸之间的距离小于相似阈值才算匹配。 比如 face_distance 计算出来的距离为 0.4和 0.8,然后设置的相似阈值(tolerance容忍度)为0.6, 那么只有 0.4 小于 0.6 才认为是 相似, 0.8 大于 0.6 则认为是 不相似。 其中compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6) 也可以设置相似阈值(tolerance容忍度) """ # 查看该脸是否与已知脸匹配 match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50) """ match列表中的True或者False值 对应 known_faces = [lmm_face_encoding, al_face_encoding] """ # 如果你有两张以上的脸,你可以让这个逻辑更漂亮 但是为了演示我把它简单化了 name = None if match[0]: name = "nagisa" face_names.append(name) # elif match[1]: # name = "Quxiaoxiao" # face_names.append(name) # 标记结果 #列表中的每个元组包含人脸的位置(top, right, bottom, left) for (top, right, bottom, left), name in zip(face_locations, face_names): if not name: name = "Unknown" """ 第一种方式:绘制人脸检测框 """ # # 根据 人脸的位置(top, right, bottom, left) 在脸上画一个方框 # cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # # 画一个带文本信息的方框 # cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED) # #设置文本信息的字体 # font = cv2.FONT_HERSHEY_DUPLEX # #要填入的文本信息 # cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1) """ 第二种方式:使用高斯滤波生成类似马赛克 贴到指定人脸位置 """ # face_image = frame[top:bottom, left:right] # face_image = cv2.GaussianBlur(face_image, (99, 99), 30) # frame[top:bottom, left:right] = face_image """ 第三种方式:把小图贴到每一帧图片中的指定人脸位置 """ face_image = frame[top:bottom, left:right] # print(face_image.shape[0:2]) mask_image = cv2.resize(mask_image, (face_image.shape[1], face_image.shape[0])) frame[top:bottom, left:right] = mask_image #彩色显示图像 cv2.imshow("img", frame) # 保持画面的持续。 k = cv2.waitKey(1) if k == 27: # 通过esc键退出摄像 cv2.destroyAllWindows() break elif k == ord("s"): # 通过s键保存图片,并退出。 cv2.imwrite("image2.jpg", frame) cv2.destroyAllWindows() break # 关闭摄像头 cap.release()
复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np import cv2 import cv2 as cv import matplotlib.pyplot as plt # 实例化检测器 face_cas = cv.CascadeClassifier("haarcascade_frontalface_default.xml" ) face_cas.load('haarcascade_frontalface_default.xml') eyes_cas = cv.CascadeClassifier("haarcascade_eye.xml") eyes_cas.load("haarcascade_eye.xml") # 调用笔记本内置摄像头,所以参数为0,如果有其他的摄像头可以调整参数为1,2 cap = cv2.VideoCapture(0) while True: # 从摄像头读取图片 sucess, img = cap.read() # 转为灰度图片 # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 显示摄像头,背景是灰度。 # cv2.imshow("img", gray) #彩色显示图像 # cv2.imshow("img", img) # 以灰度图的形式读取图片 gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # 调用识别人脸 faceRects = face_cas.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) for faceRect in faceRects: x, y, w, h = faceRect # 框出人脸 cv.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 3) # 4.在识别出的人脸中进行眼睛的检测 roi_color = img[y:y + h, x:x + w] roi_gray = gray[y:y + h, x:x + w] eyes = eyes_cas.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) ## 检测结果的绘制 # plt.figure(figsize=(8, 6), dpi=100) # plt.imshow(img[:, :, ::-1]), plt.title('检测结果') # plt.xticks([]), plt.yticks([]) # plt.show() #彩色显示图像 cv2.imshow("img", img) # 保持画面的持续。 k = cv2.waitKey(1) if k == 27: # 通过esc键退出摄像 cv2.destroyAllWindows() break elif k == ord("s"): # 通过s键保存图片,并退出。 cv2.imwrite("image2.jpg", img) cv2.destroyAllWindows() break # 关闭摄像头 cap.release()

最后

以上就是傲娇嚓茶最近收集整理的关于face_recognition、opencv中haar人脸特征:视频/图片 进行 人脸检测/人脸识别日萌社的全部内容,更多相关face_recognition、opencv中haar人脸特征:视频/图片内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部