(detailed tutorial) opencv+pycharm + notebook camera for face recognition

Posted by kpasiva on Fri, 14 Jan 2022 15:07:33 +0100

catalogue

1, Install opencv Python

2, Prepare classifier

3, Code explanation

4, Operation results

Appendix:

summary

1, Install opencv Python

It is recommended to directly install opencv Python using pycharm

Open file - Settings - python interpreter - click + sign

Search opencv python, click the installation package, and wait a moment to install it

Check whether it is installed. Create a new project and enter the following code. If there is no error, install it

import cv2

2, Prepare classifier

We use the classifiers officially provided by opencv, including face recognition, side face recognition, eye recognition, etc. if you want to train your classifier, you can see my other article.

I download three classifiers, and the old fellow takes the iron.

Link: https://pan.baidu.com/s/1VOwkHZ9-MYCusUuC00yvDw  
Extraction code: 9420

3, Code explanation

Prepare the above work and start the operation process

import cv2                                      

camera = cv2.VideoCapture(0)                    
face_casecade = cv2.CascadeClassifier(r'C:\Users\Gaomagic\PycharmProjects\kouzhaodetect\mask\xml\cascade.xml')

Import cv2 package.

        cv2.VideoCapture(0) turns on the camera. 0 means the built-in camera in the notebook

        cv2.CascadeClassifier() loads the classifier and writes the path of the classifier just downloaded in parentheses. Note: the English path should be preceded by r, otherwise \ indicates an escape symbol.

while (True):             
    ret, frame = camera.read()      

Enter a while loop, camera Read() reads a frame of image, frame is the read image, ret is the bool variable, either true or flash, and when the image is read, it is true

    if ret:
        gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
        face = face_casecade.detectMultiScale(gray_img, 1.3, 2)

Enter if judgment when ret is true, CV2 Cvtcolor (frame, CV2. Color_bgr2gray) means to convert the read picture frame into a gray image. face=face_ casecade. The first parameter of detectmultiscale() is the converted grayscale image. The second parameter is the scale of reducing the image each time. The default is 1:1. The third parameter is the number of surrounding rectangular boxes required for successful matching. The area matched by each feature is a rectangular box. It is considered successful only when multiple rectangular boxes exist at the same time, such as face. The default value is 3.

        for (x, y, w, h) in face:       
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            print("(x:",(x+w)/2,",y:",(y+h)/2,")")
            cv2.imshow("camera",frame);

Enter the for loop, CV2 Rectangle() draws a rectangle on the figure. The first parameter is the read picture, the second parameter is the coordinate of the upper left point, the second parameter is the coordinate of the lower right point, the third parameter is the RGB value, and the fourth parameter is the thickness of the rectangular border.

cv2.imshow("camera",frame) displays the picture.

       if cv2.waitKey(1) & 0xff == ord('q'):                
               break

If you enter q on the keyboard, you exit the loop, and ord er () is right. q is converted to ASCALL code

camera.release()                             
cv2.destroyAllWindows()

Close windows and cameras

4, Operation results

Appendix:

import cv2                            

camera = cv2.VideoCapture(0)               
face_casecade = cv2.CascadeClassifier(r'C:\Users\Gaomagic\PycharmProjects\kouzhaodetect\mask\xml\cascade.xml')
while (True):       
    ret, frame = camera.read()           
    if ret:
        gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)   
        face = face_casecade.detectMultiScale(gray_img, 1.3, 2) 
        for (x, y, w, h) in face:
           
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            print("(x:",(x+w)/2,",y:",(y+h)/2,")")
            cv2.imshow("camera",frame);  
        if cv2.waitKey(1) & 0xff == ord('q'): 
            break
camera.release()                             
cv2.destroyAllWindows()

summary

New blogger, please point out any mistakes.

Topics: Python OpenCV Pycharm face recognition