Last article we shared opencV image recognition of face, OpencV image face detection, this issue we share how to detect faces from video

Video face detection

Opening the camera in OpenCV is extremely easy, with just one line of code

Capture = cv2.videocapture (0) # Open the camera

After opening the camera, we use the following code to get the picture in the video (per frame)

Ret, frame = capture. Read () # read

With the picture we can according to the picture of the way to detect the face

With the above 2 sentences of code, coupled with the last period of picture recognition, you can detect the face from the video

Complete code:

import cv2

Capture = cv2.videocapture (0) # Open the camera

face = cv2.CascadeClassifier(r’D:Program Files (x86) Anaconda3pkgslibopencv – 3.4.1 track – h875b8b8_3Libraryetchaarcascadeshaarcascade_frontalface_alt. XML ‘) # import facial model

Cv2.namedwindow (‘ camera ‘) # Get camera screen

while True:

Ret, frame = capture. Read () # Fetch video image

Gray = cv2.cvtColor(frame, cv2.color_rGB2Gray) # gray

Faces = face. DetectMultiScale (gray, 1.1, 3, 0, (100100))

For (x, y, w, h) in faces: # 5 parameters, 1 parameter image, 2 coordinate origin, 3 recognition size, 4, color, 5 line width

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

Cv2.imshow (‘ camera ‘, frame) # display

if cv2.waitKey(5) & 0xFF == ord(‘q’):

break

Capture. Release () # Release resources

Cv2.destroyallwindows () # Close the window

Face detection in OpencV is detectMultiScale function. Small editor uses mobile phone to play a video and intercept several pictures of face detection

detectMultiScale(

const Mat& image,

CV_OUT vector<Rect>& objects,

Double scaleFactor = 1.1,

int minNeighbors = 3,

int flags = 0,

Size minSize = Size(),

Size maxSize = Size()

);

Recognize faces in videos

Function introduction:

Parameter 1: image– image to be detected, generally grayscale image to speed up detection;

Parameter 2: objects– a rectangular box vector set of detected objects;

Parameter 3: scaleFactor– represents the scaling factor of the search window in two successive scans. The default is 1.1, which means that each search window is expanded by 10%.

Parameter 4: minNeighbors– Indicates the minimum number of adjacent rectangles that make up the detection target (default: 3).

If the number of small rectangles that make up the detection target is smaller than min_neighbors – 1, both are excluded.

If min_neighbors is 0, the function returns all checked candidate rectangles without doing anything.

This setting value is generally used in the user – defined combination of detection results;

Parameter 5: flags– either use the default value or CV_HAAR_DO_CANNY_PRUNING if set to

CV_HAAR_DO_CANNY_PRUNING, then the function will use Canny edge detection to exclude areas with too many or too few edges,

So these areas are not usually where the face is;

Parameters 6, 7: minSize and maxSize are used to limit the range of the resulting target region.

Recognize faces in videos

OpenCV as an object detection third-party library, its powerful lies in the object detection, Dlib appeared, because of the accuracy of face detection, we have been recognized, next period we share, how to use Dlib to face detection