Preparations:

The USB camera is connected to the USB port of the PC, and the video is debugged normally. Linux recommends a video program to test Guvcview, or any other program to check that the USB camera works.

1. Download the model file:

XML Model library: github.com/opencv/open…

1). Find the haarcascade_frontalface_default. XML file and click there.

2). Find Raw, right-click (target) and save as.

 

 

2. Use USB camera to realize face recognition and frame the display position of face in the video

Open the Pycharm development tool and continue to create a new video.py file in the project. The code of the video.py file is as follows:

# -* -coding: utF-8 -* -import cv2 # Call model library file face_cascade = cv2.CascadeClassifier(' haarCascade_frontalface_default.xml ') Cap = cv2.videocapture (0) # set video window size cap.set(3, 640) cap.set(4, 480) faceNum = 0 while True: # Gray = cv2.cvtcolor (frame, DetectMultiScale (gray, scaleFactor=1.3, MinNeighbors =3) faceNum = len(faces) print(" faces: %s" % faceNum) if len(faces) > 0: for faceRect in faces: X, y, w, h = faceRect # -- -- -- -- -- -- -- -- draw a rectangle around the face cv2. A rectangle (frame, (x, y), (y + x + w, h), (255, 255, 0), # 2) display images cv2. Imshow (" img ", frame) # -- -- -- -- -- -- -- -- Q key to launch the if cv2. WaitKey (1) & 0 XFF = = word (" Q ") : Cv2.destroyallwindows () Cap.release ()Copy the code

The running result of the camera is as follows: