python dlib人臉識(shí)別代碼實(shí)例
本文實(shí)例為大家分享了python dlib人臉識(shí)別的具體代碼,供大家參考,具體內(nèi)容如下
import matplotlib.pyplot as plt import dlib import numpy as np import glob import re #正臉檢測(cè)器 detector=dlib.get_frontal_face_detector() #臉部關(guān)鍵形態(tài)檢測(cè)器 sp=dlib.shape_predictor(r"D:\LB\JAVASCRIPT\shape_predictor_68_face_landmarks.dat") #人臉識(shí)別模型 facerec = dlib.face_recognition_model_v1(r"D:\LB\JAVASCRIPT\dlib_face_recognition_resnet_model_v1.dat") #候選人臉部描述向量集 descriptors=[] photo_locations=[] for photo in glob.glob(r'D:\LB\JAVASCRIPT\faces\*.jpg'): photo_locations.append(photo) img=plt.imread(photo) img=np.array(img) #開始檢測(cè)人臉 dets=detector(img,1) for k,d in enumerate(dets): #檢測(cè)每張照片中人臉的特征 shape=sp(img,d) face_descriptor=facerec.compute_face_descriptor(img,shape) v=np.array(face_descriptor) descriptors.append(v) #輸入的待識(shí)別的人臉處理方法相同 img=plt.imread(r'D:\test_photo10.jpg') img=np.array(img) dets=detector(img,1) #計(jì)算輸入人臉和已有人臉之間的差異程度(比如用歐式距離來(lái)衡量) differences=[] for k,d in enumerate(dets): shape=sp(img,d) face_descriptor=facerec.compute_face_descriptor(img,shape) d_test=np.array(face_descriptor) #計(jì)算輸入人臉和所有已有人臉描述向量的歐氏距離 for i in descriptors: distance=np.linalg.norm(i-d_test) differences.append(distance) #按歐式距離排序 歐式距離最小的就是匹配的人臉 candidate_count=len(photo_locations) candidates_dict=dict(zip(photo_locations,differences)) candidates_dict_sorted=sorted(candidates_dict.items(),key=lambda x:x[1]) #matplotlib要正確顯示中文需要設(shè)置 plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['figure.figsize'] = (20.0, 70.0) ax=plt.subplot(candidate_count+1,4,1) ax.set_title("輸入的人臉") ax.imshow(img) for i,(photo,distance) in enumerate(candidates_dict_sorted): img=plt.imread(photo) face_name="" photo_name=re.search(r'([^\\]*)\.jpg$',photo) if photo_name: face_name=photo_name[1] ax=plt.subplot(candidate_count+1,4,i+2) ax.set_xticks([]) ax.set_yticks([]) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) if i==0: ax.set_title("最匹配的人臉\n\n"+face_name+"\n\n差異度:"+str(distance)) else: ax.set_title(face_name+"\n\n差異度:"+str(distance)) ax.imshow(img) plt.show()
以上所述是小編給大家介紹的python dlib人臉識(shí)別詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
python簡(jiǎn)單實(shí)現(xiàn)9宮格圖片實(shí)例
在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python實(shí)現(xiàn)朋友圈中的九宮格圖片的實(shí)例講解,有需要的朋友們可以參考下。2020-09-09macbook如何徹底刪除python的實(shí)現(xiàn)方法
本文主要介紹了macbook如何徹底刪除python的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Jupyter notebook 輸出部分顯示不全的解決方案
這篇文章主要介紹了Jupyter notebook 輸出部分顯示不全的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04Python + selenium + crontab實(shí)現(xiàn)每日定時(shí)自動(dòng)打卡功能
這篇文章主要介紹了Python + selenium + crontab實(shí)現(xiàn)每日定時(shí)自動(dòng)打卡功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python使用Queue在多個(gè)子進(jìn)程間交換數(shù)據(jù)的方法
這篇文章主要介紹了python使用Queue在多個(gè)子進(jìn)程間交換數(shù)據(jù)的方法,實(shí)例分析了Queue實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交互的技巧,需要的朋友可以參考下2015-04-04在Django下測(cè)試與調(diào)試REST API的方法詳解
今天小編就為大家分享一篇在Django下測(cè)試與調(diào)試REST API的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python處理二進(jìn)制數(shù)據(jù)的方法
這篇文章主要介紹了python處理二進(jìn)制數(shù)據(jù)的方法,涉及Python針對(duì)二進(jìn)制數(shù)據(jù)的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06python如何打印楊輝三角及輸出第m行第k個(gè)數(shù)
這篇文章主要介紹了python如何打印楊輝三角及輸出第m行第k個(gè)數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08