Face detection

To do face recognition, first of all, we need to detect faces in pictures/videos. Here, we mainly use feature data HAAR and LBP, which I feel not very skilled. Both HAAR and LBP can achieve the extraction of face features. Baidu web disk: Link:Pan.baidu.com/s/1mitYMtOl…Face detection is not face recognition, the two have a day difference to other differences, here to tell the truth I just call someone else’s data to carry out a simple operation.

Cascade classifiers

Cascade classifiers, that is, cascade enhanced classifiers that work with haar-like features, are a special case of integrated learning called Boost. It typically relies on the Adaboost classifier (and other models such as Real Adaboost, Gentle Adaboost, or Logitboost).

As you can see, there are many different parts of the face that can be examined, so let’s just do a simple face test here.

Identify the image

Code:

import cv2 as cv


def main(a):
    pitcurepath = "C:/Users/POG/Desktop/49d2d78cd115011da7c9b4aa54f5460b.jpg"
    global src
    src = cv.imread(pitcurepath)
    cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
    cv.namedWindow("results",cv.WINDOW_AUTOSIZE)
    cv.imshow("input image",src)
    face_detect_demo()
    cv.waitKey(0Def face_detect_demo(): The following are HAAR and LBP feature data respectively, and you can choose either of them. Note: Path '/' and '\' are required # via cascade detector CV.CascadeClassifier, CvtColor (SRC, CV.COLOR_BGR2GRAY) # Tell OpenCV to use face_Detector = CV.CascadeClassifier("C:/Users/POG/Desktop/haarshare/haarcascade_frontalface_alt_tree.xml"Face detection in scale space, the first parameter is which picture, the second parameter is up or down scale change, is the original scale1.02Times, the third parameter is identified as adult face when it appears in the adjacent rectangle of face detection, which is in the adjacent5Personal face detection box appears, if the picture is fuzzy words suggest that down a little bit faces = face_detector. DetectMultiScale (gray,1.02.2)# Here the face detection value is dynamically adjustedfor x,y,w,h in faces:
        The rectangle parameter describes the target image to draw, the first vertex of the rectangle, the other vertex on the rectangle's diagonal, the color of the line, and the width of the line
        cv.rectangle(src,(x,y),(x+w,y+h),(0.0.255),2)
    cv.imshow("results",src)


if __name__ =="__main__":
    main()
Copy the code

Results: (My favorite Taylor Swift, of course!)

Identify the video

In fact, and the recognition of the picture is similar, but more than a open video, because the video is also a frame as a picture processing.

import cv2 as cv


def main(a):
    pitcurepath = "C:/Users/POG/Desktop/49d2d78cd115011da7c9b4aa54f5460b.jpg"
    global src
    #src = cv.imread(pitcurepath)
    capture = cv.VideoCapture(0)
    #cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
    cv.namedWindow("results", cv.WINDOW_AUTOSIZE)

    while(True):
        ret,frame = capture.read()
        frame = cv.flip(frame,1)
        face_detect_demo(frame)
        c = cv.waitKey(10)
        if (c == 27) :# esc to 27
            break
    #cv.imshow("input image", image)
    #face_detect_demo()
    cv.waitKey(0Def face_detect_demo(image): The following are HAAR and LBP feature data respectively, and you can choose either of them. Note: Path '/' and '\' are required # via cascade detector CV.CascadeClassifier, Gray = CV. CvtColor (image, CV.COLOR_BGR2GRAY) # Tell OpenCV to use face_detector = CV.CascadeClassifier("C:/Users/POG/Desktop/haarshare/haarcascade_frontalface_alt_tree.xml"Face detection in scale space, the first parameter is which picture, the second parameter is up or down scale change, is the original scale1.02Times, the third parameter is identified as adult face when it appears in the adjacent rectangle of face detection, which is in the adjacent5Personal face detection box appears, if the picture is fuzzy words suggest that down a little bit faces = face_detector. DetectMultiScale (gray,1.02.2)
    for x,y,w,h in faces:
        The rectangle parameter describes the target image to draw, the first vertex of the rectangle, the other vertex on the rectangle's diagonal, the color of the line, and the width of the line
        cv.rectangle(image,(x,y),(x+w,y+h),(0.0.255),2)
    cv.imshow("results",image)
    #cv.waitKey(10)


if __name__ =="__main__":
    main()
Copy the code

Results: (here to present their ugly photos, we do not blame.(wu! Why I have a moment without mold, mold testing for a while, the side face so less?)So today I’ll write it here, and I’ll learn it again sometime.