基于OpenCV和dlib库实现的人脸识别源码解析

需积分: 0 3 下载量 44 浏览量 更新于2024-10-23 收藏 6.64MB ZIP 举报
资源摘要信息:"Face_Recognition_dlib-master.zip" 知识点概述: 本压缩包"Face_Recognition_dlib-master.zip"包含了源码软件,该软件专注于实现基于opencv和dlib库的人脸识别功能。opencv是一个开源的计算机视觉库,广泛应用于图像处理和分析,机器学习等领域。dlib是一个高性能的机器学习和深度学习工具包,其中包括了大量的机器学习算法以及人脸检测和识别的预训练模型。本软件结合了opencv和dlib的先进特性,以提供准确和高效的人脸识别功能。 详细知识点: 1. OpenCV (开源计算机视觉库) OpenCV是一个开源的计算机视觉和机器学习软件库,提供了大量通用的图像处理和分析的函数,支持多种编程语言,包括C++、Python等。OpenCV包含了数千个优化的算法,可以处理图像和视频,识别物体、人脸识别、跟踪移动物体、提取3D模型等。OpenCV被广泛应用于学术研究、产品开发、以及商业项目中。 2. dlib (机器学习与深度学习工具包) dlib是一个包含了机器学习算法和工具的C++工具包,它被设计为易于使用,并且具有高效性。dlib通过预训练模型和各种机器学习工具,如支持向量机、深度学习框架等,提供了人脸检测、物体识别、机器学习等功能。它在人脸识别和图像处理领域中应用广泛。 3. 人脸识别技术 人脸识别技术是利用分析比较人脸视觉特征信息进行身份鉴别的技术。这些视觉特征包括人脸的几何结构、皮肤纹理、人脸的局部特征等。识别过程通常包括人脸检测、特征提取和匹配等步骤。opencv和dlib都提供了强大的人脸识别技术,被广泛应用于安全验证、智能监控、人机交互等多个领域。 4.opencv与dlib在人脸识别中的应用 opencv在人脸检测的基础上,利用HOG+SVM的分类器,进行人脸的识别。而dlib则提供了深度学习算法,特别是基于卷积神经网络(CNN)的人脸识别预训练模型,如dlib的深度卷积网络模型(dlib_face_recognition_resnet_model_v1),这些预训练模型能够从大量人脸图片中学习到人脸的深层次特征,实现高精度的人脸识别。 5.人工智能与计算机视觉的关系 人工智能(AI)是计算机科学的一个分支,旨在制造出能够模拟、延伸和扩展人的智能的机器或软件。计算机视觉是人工智能的一个子领域,它关注如何让机器“看”懂图像内容,并且理解所观察到的场景。opencv和dlib的结合使用,就是应用人工智能技术在计算机视觉领域的一个典型应用,即通过机器学习和深度学习技术实现复杂的人脸识别任务。 总结: "Face_Recognition_dlib-master.zip"压缩包是一个将opencv和dlib的人脸识别技术相结合的源码软件。它利用opencv的强大图像处理功能以及dlib深度学习模型的高效性能,实现了精确且高效的人脸识别解决方案。该软件适用于开发和研究,可用于多种需要人脸识别功能的应用场景。开发者可以使用该资源进行二次开发,为各种人工智能和计算机视觉项目提供支持。

import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] 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) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()

2023-06-07 上传

import face_recognition import cv2 import os unknow_people_list = [i for i in os.listdir('unknow_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))] know_people_list = [i for i in os.listdir('know_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))] def face_select(): for unknow_people in unknow_people_list: # 读取待识别图片 unknow = face_recognition.load_image_file('unknow_people/' + unknow_people) # 将待识别图片转化为特征向量 unknow_encode = face_recognition.face_encodings(unknow)[0] flag = False for know_people in know_people_list: # 读取计算机已经认识的图片 know = face_recognition.load_image_file('know_people/' + know_people) # 获得面部位置 face_location1 = face_recognition.face_locations(know) face_location2 = face_recognition.face_locations(unknow) # 提取面部关键点 face_landmarks_list1 = face_recognition.face_landmarks(know) face_landmarks_list2 = face_recognition.face_landmarks(unknow) # 图片转化为特征向量 know_encode = face_recognition.face_encodings(know)[0] # 两张图片进行比较的结果 res = face_recognition.compare_faces([know_encode], unknow_encode, tolerance=0.5) if res[0]: flag = True name = know_people.split(".")[0] break if flag: print(f'{name}匹配成功!') else: print(f'匹配失败') name = "UNKNOWN" # 绘制人脸特征点和矩形框 for (x1, y1, w1, h1) in face_location1: cv2.rectangle(know, (y1, x1), (h1, w1), (255, 0, 0), 2) cv2.putText(know, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2) for face_landmarks in face_landmarks_list1: for facial_feature in face_landmarks.keys(): for pt_pos in face_landmarks[facial_feature]: cv2.circle(know, pt_pos, 1, (192, 192, 192), 2) for (x1, y1, w1, h1) in face_location2: cv2.rectangle(unknow, (y1, x1), (h1, w1), (255, 0, 0), 2) cv2.putText(unknow, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2) for face_landmarks in face_landmarks_list2: for facial_feature in face_landmarks.keys(): for pt_pos in face_landmarks[facial_feature]: cv2.circle(unknow, pt_pos, 1, (192, 192, 192), 2) # 显示图片 cv2.imshow("known", know) cv2.imshow("unknown", unknow) cv2.waitKey(0) if __name__ == '__main__': face_select()

2023-06-02 上传

Collecting face_recognition Using cached face_recognition-1.3.0-py2.py3-none-any.whl (15 kB) Requirement already satisfied: Pillow in d:\anaconda\envs\pytorch\lib\site-packages (from face_recognition) (9.4.0) Requirement already satisfied: face-recognition-models>=0.3.0 in d:\anaconda\envs\pytorch\lib\site-packages (from face_recognition) (0.3.0) Collecting dlib>=19.7 Using cached dlib-19.24.1.tar.gz (3.2 MB) Preparing metadata (setup.py) ... done Requirement already satisfied: Click>=6.0 in d:\anaconda\envs\pytorch\lib\site-packages (from face_recognition) (8.1.3) Requirement already satisfied: numpy in d:\anaconda\envs\pytorch\lib\site-packages (from face_recognition) (1.24.2) Requirement already satisfied: colorama in d:\anaconda\envs\pytorch\lib\site-packages (from Click>=6.0->face_recognition) (0.4.6) Building wheels for collected packages: dlib Building wheel for dlib (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [7 lines of output] running bdist_wheel running build running build_py running build_ext ERROR: CMake must be installed to build dlib [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Running setup.py clean for dlib Failed to build dlib Installing collected packages: dlib, face_recognition Running setup.py install for dlib ... error error: subprocess-exited-with-error × Running setup.py install for dlib did not run successfully. │ exit code: 1 ╰─> [9 lines of output] running install D:\anaconda\envs\pytorch\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. warnings.warn( running build running build_py running build_ext ERROR: CMake must be installed to build dlib [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure × Encountered error while trying to install package. ╰─> dlib note: This is an issue with the package mentioned above, not pip. hint: See above for output from the failure.

2023-06-02 上传