|
| 1 | +--- |
| 2 | +title: python实现简单的人脸识别 |
| 3 | +date: 2021-10-09 14:04:00 |
| 4 | +tags: |
| 5 | + - opencv-python |
| 6 | + - 人脸识别 |
| 7 | +--- |
| 8 | + |
| 9 | +想做一个简单的人脸识别,大概的需求:通过笔记本的摄像头获取人脸,根据已保存的图片标签(文件名以名字命名),识别出面部后,将名字打印在摄像头的获取界面。 |
| 10 | + |
| 11 | +搜索后确定技术方案:python + opencv + face_recognition |
| 12 | +<!--more--> |
| 13 | + |
| 14 | +## python 人脸识别 |
| 15 | + |
| 16 | +安装需要的包: |
| 17 | +``` |
| 18 | +pip install opencv-python |
| 19 | +pip install pillow |
| 20 | +pip install dlib |
| 21 | +pip install face_recognition |
| 22 | +``` |
| 23 | +创建facerecognition.py文件,使用下面的代码。在代码文件目录下创建images文件夹,里面放入自己照片,并用名字命名照片。 |
| 24 | +另外,需要从```C:\Windows\Fonts```目录中拷贝宋体的字体文件(simsun.ttc)放在代码同目录下。 |
| 25 | + |
| 26 | +```python |
| 27 | +import numpy as np |
| 28 | +import face_recognition |
| 29 | +import cv2 |
| 30 | +import os |
| 31 | +from PIL import ImageFont, ImageDraw, Image |
| 32 | + |
| 33 | +def cv2AddChineseText(img, text, position, textColor=(0, 255, 0), textSize=30): |
| 34 | + if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型 |
| 35 | + img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) |
| 36 | + # 创建一个可以在给定图像上绘图的对象 |
| 37 | + draw = ImageDraw.Draw(img) |
| 38 | + # 字体的格式 |
| 39 | + fontStyle = ImageFont.truetype( |
| 40 | + "simsun.ttc", textSize, encoding="utf-8") |
| 41 | + # 绘制文本 |
| 42 | + draw.text(position, text, textColor, font=fontStyle) |
| 43 | + # 转换回OpenCV格式 |
| 44 | + return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) |
| 45 | + |
| 46 | + |
| 47 | +def face(path): |
| 48 | + #存储知道人名列表 |
| 49 | + known_names=[] |
| 50 | + #存储知道的特征值 |
| 51 | + known_encodings=[] |
| 52 | + for image_name in os.listdir(path): |
| 53 | + load_image = face_recognition.load_image_file(path+image_name) #加载图片 |
| 54 | + image_face_encoding = face_recognition.face_encodings(load_image)[0] #获得128维特征值 |
| 55 | + known_names.append(image_name.split(".")[0]) |
| 56 | + known_encodings.append(image_face_encoding) |
| 57 | + |
| 58 | + |
| 59 | + #打开摄像头,0表示内置摄像头 |
| 60 | + video_capture = cv2.VideoCapture(0) |
| 61 | + process_this_frame = True |
| 62 | + while True: |
| 63 | + ret, frame = video_capture.read() |
| 64 | + # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。 |
| 65 | + rgb_frame = frame[:, :, ::-1] |
| 66 | + if process_this_frame: |
| 67 | + face_locations = face_recognition.face_locations(rgb_frame)#获得所有人脸位置 |
| 68 | + face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) #获得人脸特征值 |
| 69 | + face_names = [] #存储出现在画面中人脸的名字 |
| 70 | + for face_encoding in face_encodings: |
| 71 | + matches = face_recognition.compare_faces(known_encodings, face_encoding,tolerance=0.5) |
| 72 | + if True in matches: |
| 73 | + first_match_index = matches.index(True) |
| 74 | + name = known_names[first_match_index] |
| 75 | + else: |
| 76 | + name="unknown" |
| 77 | + face_names.append(name) |
| 78 | + |
| 79 | + process_this_frame = not process_this_frame |
| 80 | + |
| 81 | + # 将捕捉到的人脸显示出来 |
| 82 | + for (top, right, bottom, left), name in zip(face_locations, face_names): |
| 83 | + cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 画人脸矩形框 |
| 84 | + # 加上人名标签 |
| 85 | + cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) |
| 86 | + frame=cv2AddChineseText(frame, '我猜你叫:' + name, (left + 6, bottom - 6),(255, 255, 255), 30) |
| 87 | + cv2.imshow('frame', frame) |
| 88 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 89 | + break |
| 90 | + |
| 91 | + video_capture.release() |
| 92 | + cv2.destroyAllWindows() |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +if __name__=='__main__': |
| 97 | + face("./images/") #存放已知图像路径 |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +测试识别结果,在images目录下放一张自己的图片,文件名以名字命名,测试结果如下: |
| 102 | + |
| 103 | + |
| 104 | +## 踩过的坑 |
| 105 | +1. 安装dlib时遇到的坑 |
| 106 | +安装dlib时报错:需要安装cmake。 |
| 107 | +``` |
| 108 | +pip install cmake |
| 109 | +``` |
| 110 | +如果cmake已经在python目录下的sitepackages中,则更新pip。 |
| 111 | +``` |
| 112 | +pip install -U --force-reinstall pip -user |
| 113 | +``` |
| 114 | +接下来安装dlib就可以了。cmake需要编译,这个时候CPU消耗会很大,机器比较卡。 |
| 115 | + |
| 116 | +2. 在摄像头捕捉界面显示中文乱码 |
| 117 | +cv1.putText可以用来输出文字在图像捕获框上,但输出中文时变成乱码了。此时,可以通过将中文画成图像,再输出到图像捕获框上。封装一个cv2AddChineseText函数即可。 |
| 118 | + |
| 119 | +参考: |
| 120 | +python人脸识别:<a href="https://zhuanlan.zhihu.com/p/79784400">https://zhuanlan.zhihu.com/p/79784400</a> |
| 121 | +安装dlib错误<a href="https://blog.csdn.net/SunshinePluto/article/details/116177270">https://blog.csdn.net/SunshinePluto/article/details/116177270</a> |
| 122 | +显示中文乱码<a href="https://blog.csdn.net/hijacklei/article/details/116010860">https://blog.csdn.net/hijacklei/article/details/116010860</a> |
| 123 | + |
0 commit comments