資源簡介
工程中包含訓(xùn)練好的模型,內(nèi)部有詳細(xì)的配置教程,方法簡單效果明顯,程序運(yùn)行流暢。
代碼片段和文件信息
“““
從視屏中識(shí)別人臉,并實(shí)時(shí)標(biāo)出面部特征點(diǎn)
“““
import?dlib?????????????????????#人臉識(shí)別的庫dlib
import?numpy?as?np??????????????#數(shù)據(jù)處理的庫numpy
import?cv2??????????????????????#圖像處理的庫OpenCv
class?face_emotion():
????def?__init__(self):
????????#?使用特征提取器get_frontal_face_detector
????????self.detector?=?dlib.get_frontal_face_detector()
????????#?dlib的68點(diǎn)模型,使用作者訓(xùn)練好的特征預(yù)測(cè)器
????????self.predictor?=?dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat“)
????????#建cv2攝像頭對(duì)象,這里使用電腦自帶攝像頭,如果接了外部攝像頭,則自動(dòng)切換到外部攝像頭
????????self.cap?=?cv2.VideoCapture(0)
????????#?設(shè)置視頻參數(shù),propId設(shè)置的視頻參數(shù),value設(shè)置的參數(shù)值
????????self.cap.set(3?480)
????????#?截圖screenshoot的計(jì)數(shù)器
????????self.cnt?=?0
????def?learning_face(self):
????????#?眉毛直線擬合數(shù)據(jù)緩沖
????????line_brow_x?=?[]
????????line_brow_y?=?[]
????????#?cap.isOpened()?返回true/false?檢查初始化是否成功
????????while(self.cap.isOpened()):
????????????#?cap.read()
????????????#?返回兩個(gè)值:
????????????#????一個(gè)布爾值true/false,用來判斷讀取視頻是否成功/是否到視頻末尾
????????????#????圖像對(duì)象,圖像的三維矩陣
????????????flag?im_rd?=?self.cap.read()
????????????#?每幀數(shù)據(jù)延時(shí)1ms,延時(shí)為0讀取的是靜態(tài)幀
????????????k?=?cv2.waitKey(1)
????????????#?取灰度
????????????img_gray?=?cv2.cvtColor(im_rd?cv2.COLOR_RGB2GRAY)
????????????#?使用人臉檢測(cè)器檢測(cè)每一幀圖像中的人臉。并返回人臉數(shù)rects
????????????faces?=?self.detector(img_gray?0)
????????????#?待會(huì)要顯示在屏幕上的字體
????????????font?=?cv2.FONT_HERSHEY_SIMPLEX
????????????#?如果檢測(cè)到人臉
????????????if(len(faces)!=0):
????????????????#?對(duì)每個(gè)人臉都標(biāo)出68個(gè)特征點(diǎn)
????????????????for?i?in?range(len(faces)):
????????????????????#?enumerate方法同時(shí)返回?cái)?shù)據(jù)對(duì)象的索引和數(shù)據(jù),k為索引,d為faces中的對(duì)象
????????????????????for?k?d?in?enumerate(faces):
????????????????????????#?用紅色矩形框出人臉
????????????????????????cv2.rectangle(im_rd?(d.left()?d.top())?(d.right()?d.bottom())?(0?0?255))
????????????????????????#?計(jì)算人臉熱別框邊長
????????????????????????self.face_width?=?d.right()?-?d.left()
????????????????????????#?使用預(yù)測(cè)器得到68點(diǎn)數(shù)據(jù)的坐標(biāo)
????????????????????????shape?=?self.predictor(im_rd?d)
????????????????????????#?圓圈顯示每個(gè)特征點(diǎn)
????????????????????????for?i?in?range(68):
????????????????????????????cv2.circle(im_rd?(shape.part(i).x?shape.part(i).y)?2?(0?255?0)?-1?8)
????????????????????????????#cv2.putText(im_rd?str(i)?(shape.part(i).x?shape.part(i).y)?cv2.FONT_HERSHEY_SIMPLEX?0.5
????????????????????????????#????????????(255?255?255))
????????????????????????#?分析任意n點(diǎn)的位置關(guān)系來作為表情識(shí)別的依據(jù)
????????????????????????mouth_width?=?(shape.part(54).x?-?shape.part(48).x)?/?self.face_width??#?嘴巴咧開程度
????????????????????????mouth_higth?=?(shape.part(66).y?-?shape.part(62).y)?/?self.face_width??#?嘴巴張開程度
????????????????????????#?print(“嘴巴寬度與識(shí)別框?qū)挾戎龋骸癿outh_width_arv)
????????????????????????#?print(“嘴巴高度與識(shí)別框高度之比:“mouth_higth_arv)
????????????????????????#?通過兩個(gè)眉毛上的10個(gè)特征點(diǎn),分析挑眉程度和皺眉程度
????????????????????????brow_sum?=?0??#?高度之和
????????????????????????frown_sum?=?0??#?兩邊眉毛距離之和
????????????????????????for?j?in?range(17?21):
????????????????????????????brow_sum?+=?(shape.part(j
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-15?13:28??人臉識(shí)別情緒識(shí)別程序\
?????目錄???????????0??2018-05-15?13:30??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\
?????目錄???????????0??2018-05-15?13:28??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\.vs\
?????目錄???????????0??2018-05-15?13:28??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\.vs\Python4.24.3\
?????目錄???????????0??2018-05-15?13:28??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\.vs\Python4.24.3\v14\
?????文件???????33280??2018-05-21?21:30??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\.vs\Python4.24.3\v14\.suo
?????目錄???????????0??2018-05-15?13:28??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\Python4.24.3\
?????文件????????7707??2018-04-25?01:14??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\Python4.24.3\Python4.24.3.py
?????文件????????2168??2018-04-25?00:36??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\Python4.24.3\Python4.24.3.pyproj
?????文件????99693937??2018-04-25?01:06??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\Python4.24.3\shape_predictor_68_face_landmarks.dat
?????文件?????????839??2018-04-25?00:31??人臉識(shí)別情緒識(shí)別程序\Python4.24.3\Python4.24.3.sln
?????文件?????????108??2018-05-15?13:31??人臉識(shí)別情緒識(shí)別程序\程序配置說明.txt
評(píng)論
共有 條評(píng)論