最近在研究一个人脸识别开源框架,编译需要依赖 dlib、boost、cmake 等相关环境,在编译的时候,踩了一大堆坑,网上资料大都不是很全面再加上 Windows 环境下去配置这些本身就比 Liunx 或 Mac下配置要相对麻烦一些。如果你是准备在 Windows 环境下编译,就做好准备踩坑吧~~~
# -*- coding: utf-8 -*-# 摄像头实时识别人脸import face_recognitionimport cv2video_capture = cv2.VideoCapture(0)# 加载本地图片xhb_img = face_recognition.load_image_file("xhb.jpg")xhb_face_encoding = face_recognition.face_encodings(xhb_img)[0]# 初始化变量face_locations = []face_encodings = []face_names = []process_this_frame = Truewhile True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) if process_this_frame: face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) face_names = [] for face_encoding in face_encodings: # 可以自定义设置识别阈值(tolerance)此处设置为0.5,默认为0.6,太小可能识别不出来,太大可能造成识别混淆 match = face_recognition.compare_faces([xhb_face_encoding], face_encoding,tolerance=0.5) if match[0]: name = "xiaohaibin" else: name = "unknown" face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1) cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): breakvideo_capture.release()cv2.destroyAllWindows()复制代码
运行结果展示
相关资料
踩坑
1.解决 cl.exe 找不到的问题
在装VS2015时,默认是不安装C++,你需要重新运行setup ,然后选择modify,选择 language 下的C++,然后开始安装,就可以解决问题了 –来自
2.解决执行 pip install face_recognition 出错,报SSL Error
方案一(推荐):在pip安装所在文件夹路径下,创造python文件(.py)
import os ini="""[global] index-url = https://pypi.doubanio.com/simple/ [install] trusted-host=pypi.doubanio.com """ pippath=os.environ["USERPROFILE"]+"\\pip\\" if not os.path.exists(pippath): os.mkdir(pippath) with open(pippath+"pip.ini","w+") as f: f.write(ini) 复制代码