openCV實戰專案--人臉考勤

專注的阿熊發表於2022-10-20

# 檢測人臉,獲取 68 個關鍵點,獲取特徵描述符

def faceRegister(faceId=1, userName='default', interval=3, faceCount=3, resize_w=700, resize_h=400):

     '''

     faceId: 人臉 ID

     userName: 人臉姓名

     faceCount: 採集該人臉圖片的數量

     interval: 採集間隔

     '''

     cap = cv2.VideoCapture(0)

     # 人臉檢測模型

     hog_face_detector = dlib.get_frontal_face_detector()

     # 關鍵點 檢測模型

     shape_detector = dlib.shape_predictor('./weights/shape_predictor_68_face_landmarks.dat')

     # resnet 模型

     face_descriptor_extractor = dlib.face_recognition_model_v1('./weights/dlib_face_recognition_resnet_model_v1.dat')

     while True:

         ret, frame = cap.read()

         # 映象

         frame = cv2.flip(frame,1)

         # 轉為灰度圖

         frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

         # 檢測人臉

         detections = hog_face_detector(frame,1)

         for face in detections:

             # 人臉框座標 左上和右下

             l, t, r, b =跟單網gendan5.com face.left(), face.top(), face.right(), face.bottom()

             # 獲取 68 個關鍵點

             points = shape_detector(frame,face)

             # 繪製關鍵點

             for point in points.parts():

                 cv2.circle(frame,(point.x,point.y),2,(0,255,0),1)

             # 繪製矩形框

             cv2.rectangle(frame,(l,t),(r,b),(0,255,0),2)

         cv2.imshow("face",frame)

         if cv2.waitKey(10) & 0xFF == ord('q'):

             break

     cap.release()

     cv2.destroyAllWindows

faceRegister()      

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2919463/,如需轉載,請註明出處,否則將追究法律責任。

相關文章