Python easily realizes face recognition

Today I will share the code about Python image expansion and corrosion, image face recognition and dynamic face recognition~~~ Happy, this rookie was immersed in the cv2 library for a long time and couldn’t help it for a long time~~ I hope you can too Enjoy the little surprises that Python brings. .

0x 00 Python image expansion and corrosion

The expansion and erosion of the image is mainly to find the extremely small and extremely small areas in the image. The structural element in the code means: There are two images B and X. If X is the object to be processed and B is used to process X, then B is called a structure element, which is also called a brush vividly. Structural elements are usually relatively small images. For detailed principles and concepts, see the link at the end of the article~~

"""Picture Expansion and Corrosion"""

import cv2

 

#Read picture: cv2.imread(path, num)

img = cv2.imread("1.jpg",0)

 

#Construct a 3*3 structural element

elment = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))

 

#Expansion image cv2.dilate (image, element structure)

dilate = cv2.dilate (img, elment)

 

#Erosion image cv2.erode (image, element structure)

erode = cv2.erode(img,elment)

 

#Subtract two images to obtain edges, the first parameter is the image after expansion, and the second parameter is the image after corrosion

result = cv2.absdiff(dilate,erode)

 

#阈值类型:'TERM_CRITERIA_COUNT', 'TERM_CRITERIA_EPS', 'TERM_CRITERIA_MAX_ITER',

# 'THRESH_BINARY', 'THRESH_BINARY_INV', 'THRESH_MASK', 'THRESH_OTSU',

# 'THRESH_TOZERO_INV', 'THRESH_TRIANGLE', 'THRESH_TRUNC'

retval,result = cv2.threshold(result,50,255,cv2.THRESH_BINARY);

 

#Reverse color, that is to reverse each pixel of the binary image

result = cv2.bitwise_not(result);

 

#Display image

cv2.imshow('origin',img)

 

#原图

cv2.imshow('result',result)

 

#Edge detection graph

cv2.waitKey(0)

cv2.destroyAllWindows()

0x01 picture face recognition

    Image face recognition is divided into the following steps:

  • Image grayscale, geometric transformation, image enhancement, normalization
  • Feature point positioning, face alignment, and facial feature capture

"""Face Detection"""

import cv2

 

#Call face detection feature library

face = cv2.CascadeClassifier ('haarcascade_frontalface_alt2.xml')

 

#Read image file

sample_imag = cv2.imread('1.jpg')

 

#Face Detection

faces = face.detectMultiScale(sample_imag,scaleFactor=1.1,minNeig hbors=5,minSize=(10,10))

 

#画架处理

for (x,y,w,h) in faces:

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

 

#Result write image

cv2.imwrite('face.jpg',sample_imag)

print("detect success")

 

#New window display image

cv2.namedWindow("Image")

cv2.imshow("Image",sample_imag)

cv2.waitKey(0)

cv2.destroyAllWindows()

Show me how to identify me Zhan Huang: handsome

There is also the time to identify the warrior: (No way, no way, I didn’t even recognize it~~ I went to study training library)

0x02 dynamic face recognition

        Dynamic face recognition does not need to stop and wait, as long as you appear within a certain recognition range, whether you are walking or standing still, the system will automatically recognize it, that is to say, if people walk past in a natural form, the camera will proceed. Capture and collect information, and issue corresponding instructions for dynamic face recognition.

        The first step is to obtain feature data that is helpful for face classification based on the shape description of the face organs and the spacing characteristics between them. The feature weight generally includes the Euclidean spacing, curvature, and viewpoint between feature points.

import cv2

# 1. Use OpenCV's classifier

# 2. Read photos from camera or local

# 3. Change the frame on the picture

# 4. Show pictures on a new window

 

# 1. Use OpenCV's classifier/feature library

detector = cv2.CascadeClassifier ('haarcascade_frontalface_alt2.xml')

# 2. Read photos from camera or local

cap = cv2.VideoCapture(0)

while True:

    ret, img = cap.read ()

    faces = detector.detectMultiScale(img,1.3,5)

    # 3. Change the frame on the picture

    for (x,y,w,h) in faces:

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

    cv2.imshow('frame',img)

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

        break

# 4. Show pictures on a new window

cap.release()

cv2.destroyAllWindows()

0x03 detailed principle reference

Corrosion and expansion of pictures: https://blog.csdn.net/zqx951102/article/details/82997588

https://blog.csdn.net/weixin_39128119/article/details/84172385

Principle of face recognition (with PPT included): https://blog.csdn.net/weixin_42346564/article/details/82500454

Dynamic face recognition: https://blog.csdn.net/dedsaa/article/details/94173130

0x04 summary

I was super happy when I was learning this. It was the kind of long-sighted feeling and the kind of light in my eyes~~Learning Python is really happy. . . I hope you can also have fun from the code. Now, the following is the xml URL of the face recognition library. Download it and try it. You will be super happy to recognize it. .

Click to join the group chat [Python learning exchange/resource sharing ③]

I still want to recommend the Python learning group I built myself : 721195303 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/aaahtml/article/details/112800404