Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest

preface

Using Python to achieve OpenCV face detection, nonsense not to say ~

Let’s have a good time

The development tools

Python version: 3.6.4

Related modules:

Cv2 module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

OpenCV is an open source, cross-platform computer vision library that can be used for a variety of image and video processing operations.

Today to tell you how to detect the face through OpenCV

Image detection

Take a look at the image detection, the original

Training data is ready-made, the use of ready-made data, through training and then to detect the face

The specific code is as follows

import cv2

# image name
filename = 'cxk.png'


def detect(filename) :
    # cv2 CascadeClassifier CascadeClassifier, XML file is training data
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    # fetch image
    img = cv2.imread(filename)
    # To grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Face detection
    faces = face_cascade.detectMultiScale(gray, 1.3.5)
    # Draw face rectangle
    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x+w, y+h), (255.0.0), 2)
    # Name the display window
    cv2.namedWindow('people')
    # display images
    cv2.imshow('people', img)
    # Save images
    cv2.imwrite('cxks.png', img)
    Set the display time,0 means always display
    cv2.waitKey(0)

detect(filename)
Copy the code

The results show

Detection of video

Video on tiktok

Here, only video clips with good detection effect are taken as examples

After all, the quality of the training data is there, and sometimes there are some mistakes

If we want to improve the detection accuracy, we need a high quality face database

The specific code is as follows

import cv2


def face_rec() :
    # Load video
    cameraCapture = cv2.VideoCapture('video.mp4')
    # cv2 CascadeClassifier CascadeClassifier, XML file is training data
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    # fetch data
    success, frame = cameraCapture.read()
    while success and cv2.waitKey(1) = = -1:
        # fetch data
        ret, img = cameraCapture.read()
        # Face detection
        faces = face_cascade.detectMultiScale(img, 1.3.5)
        # Draw rectangle box
        for (x, y, w, h) in faces:
            img = cv2.rectangle(img, (x, y), (x+w, y+h), (255.0.0), 2)
        Set the display window
        cv2.namedWindow('camera'.0)
        cv2.resizeWindow('camera'.840.480)
        # Show the processed video
        cv2.imshow('camera', img)
        # fetch data
        success, frame = cameraCapture.read()
    # Release video
    cameraCapture.release()
    Release all Windows
    cv2.destroyAllWindows()


if __name__ == '__main__':
    face_rec()
Copy the code

The result is the video, which will not be shown here. You can go to the Internet to find the video to do the test and you will understand