OpenCV + python to achieve face recognition

OpenCV + python to achieve face recognition

 

 

hair-like

In layman's terms, it can be used as a face feature.

The Haar eigenvalue reflects the grayscale change of the image. For example, some features of the face can be simply described by rectangular features, such as: the eyes are darker than the cheeks, the sides of the bridge of the nose are darker than the bridge of the nose, the mouth is darker than the surrounding area, etc.

opencv api

To use opencv, you must first know what it can do and how to do it. So the importance of API will be reflected. As far as this example is concerned, there are very few functions used, that is, ordinary image reading, grayscale conversion, display image, and simple image editing.

as follows:

read image

Just give the path of the image to be manipulated.

import cv2
image = cv2.imread(imagepath)

Grayscale conversion

The effect of grayscale conversion is that the computational intensity of the image converted to grayscale is reduced.

import cv2
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

draw

One embodiment of the power of opencv is that it can edit and process images arbitrarily. 
The last parameter of the function below specifies the size of the brush.

import cv2
cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)

display image

Edited images are either displayed directly or saved to a physical storage medium.

import cv2
cv2.imshow("Image Title",image)

Get face recognition training data

It seems complicated, but it is actually some description of the face features, so that after reading the data, opencv can perceive the features on the read pictures according to the sample data in the training, and then perform face recognition on the pictures.

import cv2
face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')

The xml file shown here is the universally trained data shared by opencv on GitHub. We can use it directly.

Training data reference address:

https://github.com/opencv/opencv/tree/master/data/haarcascades 

face detection

To put it bluntly, it is the process of recognizing new images based on the training data.

import cv2

# Detect faces in pictures

faces = face_cascade.detectMultiScale(
   gray,
   scaleFactor = 1.15,
   minNeighbors = 5,
   minSize = (5,5),
   flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

We can arbitrarily specify the values ​​of the parameters inside to achieve recognition with different precisions. The return value is the embodiment of the detection result of opencv on the image.

Process the results of face detection

After the face detection just now, we can get the return value for further processing. But this is not to say how complicated it will be, it is nothing more than adding some eigenvalues.

import cv2

print "Found {0} faces!".format(len(faces))

for(x,y,w,h) in faces:
   cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)

  example

With the foundation just now, we can complete a simple small example of face recognition.

Picture material

The image below will serve as our detection basis. 

face detection code

import cv2
import numpy as np

import sys,os,glob,numpy
from skimage import io


#Specify the face recognition of the picture and then store
img = cv2.imread("test.jpg")
color = (0, 255, 0)


grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

classfier = cv2.CascadeClassifier("C:\\Users\\22291_000\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")



faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects) > 0: # If it is greater than 0, a face is detected
for faceRect in faceRects: # Frame each A face
x, y, w, h = faceRect
cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 3) #5 control Thickness of green box



# write image
cv2.imwrite('output.jpg',img)
cv2.imshow("Find Faces!",img)
cv2.waitKey(0)

 

face detection result

Output picture: 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325185533&siteId=291194637