SoFunction
Updated on 2024-10-29

Python learning notes of the video face detection and recognition example tutorials

preamble

Previous article.write a blog article (netspeak)Shared with you a simple picture face recognition technology, in fact, in the actual application, many are recognized by means of video streaming, such as face recognition channel access control attendance system, face dynamic tracking recognition system and so on.

Without further ado, let's take a look at the details together!

case (law)

Here we still use the haar face feature classifier that comes with opencv to recognize faces by reading a video.

Code Implementation:

# -*- coding: utf-8 -*-
__author__ = "Seven."
__blog__ = "https://blog./"
import cv2
import os


# Saved video to detect faces and take screenshots
def CatchPICFromVideo(window_name, camera_idx, catch_pic_num, path_name):
 (window_name)

 # Video source
 cap = (camera_idx)

 # Tell OpenCV to use the face recognition classifier
 classfier = (()+"\\haarcascade\\haarcascade_frontalface_alt.xml")

 # The color of the border to be drawn after the face is recognized, in RGB format, color is an array that cannot be added or deleted.
 color = (0, 255, 0)

 num = 0
 while ():
 ok, frame = () # Read a frame of data
 if not ok:
  break

 grey = (frame, cv2.COLOR_BGR2GRAY) # Convert the current image to grayscale.

 # Face detection, 1.2 and 2 are the image scaling and the number of valid points to be detected respectively
 faceRects = (grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
 if len(faceRects) > 0: # Greater than 0 then face detected
  for faceRect in faceRects: # Frame each face individually
  x, y, w, h = faceRect

  # Save the current frame as an image
  img_name = "%s/%" % (path_name, num)
  # print(img_name)
  image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
  (img_name, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

  num += 1
  if num > (catch_pic_num): # Exit the loop if the specified maximum number of saves is exceeded
   break

  # Draw rectangular boxes
  (frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)

  # Show how many face pictures are currently captured, so that you have an idea when you stand there to be photographed and don't have to wait in the dark.
  font = cv2.FONT_HERSHEY_SIMPLEX
  (frame, 'num:%d/100' % (num), (x + 30, y + 30), font, 1, (255, 0, 255), 4)

  # Exceeds the specified maximum number of saves to end the program
 if num > (catch_pic_num): break

 # Display image
 (window_name, frame)
 c = (10)
 if c & 0xFF == ord('q'):
  break

  # Release the camera and destroy all windows
 ()
 ()


if __name__ == '__main__':
 # 100 consecutive image cuts
 CatchPICFromVideo("get face", ()+"\\video\\kelake.mp4", 100, "E:\\VideoCapture")

The motion picture is a bit flowery, so tell me about it:

If you are capturing the camera, just change the following code:

# If getting a camera, change the parameter to 0.
cap = (0)

source code (computing)

/52itstyle/Python/tree/master/Day09local download

summarize

Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.