向人脸检测程序添加人脸识别功能

时间:2019-09-06 20:20:05

标签: python opencv

我有一个人脸检测程序,可以在看到人脸时在其周围绘制一个矩形。但是,我希望我的程序能够识别某些面孔并能够显示面孔的名称。

这是我当前的代码:

    import cv2

    #load the cascade
    face_cascade = 
    cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    #capture video from webcam
    cap = cv2.VideoCapture(0)

    #continuously capture video
    while True:
       #read the frame
      _, img = cap.read()
      #convert to grayscale
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      #detect a face
      faces = face_cascade.detectMultiScale(gray, 1.1, 4)
      #draw a rectangle to confirm face
      for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        #display video
        cv2.imshow('img', img)
        #stop if escape key is pressed
        k = cv2.waitKey(30) & 0xff
        if k == 27:
           break

    #release the video capture object
    cap.release()

如何在仍然使用我拥有的代码并且不进行任何大幅度更改的情况下实现人脸识别?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

没关系,我知道了。我决定在以下页面的帮助下使用face_recognition库:enter link description here