Python人臉識別微笑檢測
import cv2 # 影像處理的庫 OpenCv
import dlib # 人臉識別的庫 dlib
import numpy as np # 資料處理的庫 numpy
class face_emotion():
def __init__(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
self.cap = cv2.VideoCapture(0)
self.cap.set(3, 480)
self.cnt = 0
def learning_face(self):
line_brow_x = []
line_brow_y = []
while(self.cap.isOpened()):
flag, im_rd = self.cap.read()
k = cv2.waitKey(1)
# 取灰度
img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)
faces = self.detector(img_gray, 0)
font = cv2.FONT_HERSHEY_SIMPLEX
# 如果檢測到人臉
if(len(faces) != 0):
# 對每個人臉都標出 68 個特徵點
for i in range(len(faces)):
for k, d in enumerate(faces):
cv2.rectangle(im_rd, (d.left(), d.top()), (d.right(), d.bottom()), (0,0,255))
self.face_width = d.right() - d.left()
shape = self.predictor(im_rd, d)
mouth_width = (shape.part(54).x - shape.part(48).x) / self.face_width
mouth_height = (shape.part(66).y - shape.part(62).y) / self.face_width
brow_sum = 0
frown_sum = 0
for j in range(17, 21):
brow_sum += (shape.part(j).y - d.top()) + (shape.part(j + 5).y - d.top())
frown_sum += shape.part(j + 5).x - shape.part(j).x
line_brow_x.append(shape.part(j).x)
line_brow_y.append(shape.part(j).y)
tempx = np.array(line_brow_x)
tempy = np.array(line_brow_y)
z1 = np.polyfit(tempx, tempy, 1)
self.brow_k = -round(z1[0], 3)
brow_height = (brow_sum / 10) / self.face_width # 眉毛高度佔比
brow_width = (frown_sum / 5) / self.face_width # 眉毛距離佔比
eye_sum = 外匯跟單gendan5.com(shape.part(41).y - shape.part(37).y + shape.part(40).y - shape.part(38).y +
shape.part(47).y - shape.part(43).y + shape.part(46).y - shape.part(44).y)
eye_hight = (eye_sum / 4) / self.face_width
if round(mouth_height >= 0.03) and eye_hight<0.56:
cv2.putText(im_rd, "smile", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0,255,0), 2, 4)
if round(mouth_height<0.03) and self.brow_k>-0.3:
cv2.putText(im_rd, "unsmile", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0,255,0), 2, 4)
cv2.putText(im_rd, "Face-" + str(len(faces)), (20,50), font, 0.6, (0,0,255), 1, cv2.LINE_AA)
else:
cv2.putText(im_rd, "No Face", (20,50), font, 0.6, (0,0,255), 1, cv2.LINE_AA)
im_rd = cv2.putText(im_rd, "S: screenshot", (20,450), font, 0.6, (255,0,255), 1, cv2.LINE_AA)
im_rd = cv2.putText(im_rd, "Q: quit", (20,470), font, 0.6, (255,0,255), 1, cv2.LINE_AA)
if (cv2.waitKey(1) & 0xFF) == ord('s'):
self.cnt += 1
cv2.imwrite("screenshoot" + str(self.cnt) + ".jpg", im_rd)
# 按下 q 鍵退出
if (cv2.waitKey(1)) == ord('q'):
break
# 視窗顯示
cv2.imshow("Face Recognition", im_rd)
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
my_face = face_emotion()
my_face.learning_face()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2846323/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 人臉檢測識別,人臉檢測,人臉識別,離線檢測,C#原始碼C#原始碼
- 人臉活體檢測人臉識別:眨眼+張口
- 人臉識別之人臉檢測的重要性
- 人臉識別檢測專案實戰
- IOS人臉識別開發入門教程--人臉檢測篇iOS
- 人臉識別 -- 活體檢測(張嘴搖頭識別)
- 人臉識別 — 活體檢測(張嘴搖頭識別)
- 從零玩轉人臉識別之RGB人臉活體檢測
- 人臉識別活體檢測技術理論
- 人臉檢測之身份識別你需要的那些事
- python—呼叫API人臉識別PythonAPI
- 計算機視覺專案-人臉識別與檢測計算機視覺
- 人臉檢測 二
- python ubuntu dlib人臉識別3-人臉對齊PythonUbuntu
- 如何評價美顏api中人臉識別和人臉檢測的準確度?API
- opencv 人臉識別OpenCV
- OpenCV — 人臉識別OpenCV
- 人臉識別之Python DLib庫進行人臉關鍵點識別Python
- OpenCV-Python 人臉眼睛嘴識別OpenCVPython
- [計算機視覺]人臉應用:人臉檢測、人臉對比、五官檢測、眨眼檢測、活體檢測、疲勞檢測計算機視覺
- 人臉活體檢測
- 前端人臉檢測指南前端
- 華為雲——AI人臉編輯讓Lena微笑AI
- 人臉檢測的harr檢測函式函式
- 中科視拓免費開放口罩人臉檢測與識別技術
- 虹軟人臉識別 - faceId及IR活體檢測的介紹
- 40多個關於人臉檢測/識別的API、庫和軟體API
- 人臉檢測(detection)與人臉校準(alignment)
- opencv視訊人臉檢測OpenCV
- OpenCV 人臉檢測自學(3)OpenCV
- C#人臉識別入門篇-STEP BY STEP人臉識別--入門篇C#
- 智慧人臉識別門禁系統開發,人臉識別開鎖流程
- 人臉識別之特徵臉方法(Eigenface)特徵
- 前端人臉識別--兩張臉相似度前端
- NIST:人臉識別供應商測試報告測試報告
- python ubuntu dlib 5 -人臉識別並打分PythonUbuntu
- windows環境下python人臉識別初窺WindowsPython
- 使用Python結合Face++ API識別人臉PythonAPI