Video face detection - OpenCV version (3)

Video face detection is an advanced version of picture face detection. For details of picture detection, click to view my previous article "Picture Face Detection - OpenCV Version (2)" 

Implementation ideas:

Call the camera of the computer, decompose the camera information into pictures frame by frame, identify the position of the face based on the picture detection, draw the processed picture frame by frame to the user, and the effect that the user sees is the face detection of the video.

Effect preview:

Implementation steps

Use OpenCV to call the camera and display

Get the camera:

cap = cv2.VideoCapture(0)

The parameter 0 means to get the first camera.

Display the camera frame by frame, the code is as follows:

while (1):
    ret, img = cap.read ()
    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release() # release the camera
cv2.destroyAllWindows() # Release window resources

cv2.waitKey(1) & 0xFF uses the "&" bit algorithm, which means to get the ASCII code of the last character input by the user. If the input is "q", it will jump out of the loop.

video face recognition

At this time, the "Picture Face Detection" in the previous section is used to  encapsulate the face recognition code into a method. The code is as follows:

def discern(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cap = cv2.CascadeClassifier(
        "C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
    )
    faceRects = cap.detectMultiScale(
        gray, scaleFactor = 1.2, minNeighbors = 3, minSize = (50, 50))
    if len(faceRects):
        for faceRect in faceRects:
            x, y, w, h = faceRect
            cv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2) # frame the face
    cv2.imshow("Image", img)

 When recirculating the camera frame picture, just call the picture recognition method, the code is as follows:

# Get camera 0 means the first camera
cap = cv2.VideoCapture(0)
while (1): # display frame by frame
    ret, img = cap.read ()
    # cv2.imshow("Image", img)
    discern(img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release() # release the camera
cv2.destroyAllWindows() # Release window resources

  

The complete code is as follows:

# -*- coding:utf-8 -*-
# OpenCV version of video detection
import cv2


# Image recognition method encapsulation
def discern(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cap = cv2.CascadeClassifier(
        "C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
    )
    faceRects = cap.detectMultiScale(
        gray, scaleFactor = 1.2, minNeighbors = 3, minSize = (50, 50))
    if len(faceRects):
        for faceRect in faceRects:
            x, y, w, h = faceRect
            cv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2) # frame the face
    cv2.imshow("Image", img)


# Get camera 0 means the first camera
cap = cv2.VideoCapture(0)
while (1): # display frame by frame
    ret, img = cap.read ()
    # cv2.imshow("Image", img)
    discern(img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release() # release the camera
cv2.destroyAllWindows() # Release window resources

 

 


 Serial directory:

"OpenCV Environment Construction (1)"

"Picture face detection - OpenCV version (2)"

"Video face detection - OpenCV version (3)"

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324824848&siteId=291194637