OpenCV + python implement face detection (detection based on photos and video)

OpenCV + python implement face detection (detection based on photos and video)

 

 

Hair-like

Popular speaking, is can be used as facial features.

Haar feature values ​​reflect changes in a gray image. For example: Some of the features of the face can be described by a simple rectangular features, such as: Eye color deeper than the cheeks, nose deeper than the both sides of the color nose, mouth deeper than the surrounding color.

opencv api

To use opencv, you must first know that it can do, how to do it. So the importance of the API will be manifested. For this example, the function used rarely, also ordinary reading image, gradation conversion, displaying an image, a simple image editing nothing.

as follows:

Read picture

Only need to give the picture to be operated path.

import cv2
image = cv2.imread(imagepath)

Grayscale conversion

Gradation conversion role is: converted to calculate the intensity of the gradation image is reduced.

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

Paint

a manifestation of the power of opencv is that it can edit any picture processing. 
The following function is the last parameter specifies the size of the brush.

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

Display image

The edited image are displayed either directly, or saved to a physical storage medium.

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

Face recognition training data acquisition

Seems complicated, in fact, to describe some of the facial features, such opencv After reading the data in the sample data is according to the training, you can read on the perceived characteristics of the picture, then the picture recognition.

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

里卖弄的这个xml文件,就是opencv在GitHub上共享出来的具有普适的训练好的数据。我们可以直接的拿来使用。

训练数据参考地址:

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

探测人脸

说白了,就是根据训练的数据来对新图片进行识别的过程。

import cv2

# 探测图片中的人脸

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

我们可以随意的指定里面参数的值,来达到不同精度下的识别。返回值就是opencv对图片的探测结果的体现。

处理人脸探测的结果

结束了刚才的人脸探测,我们就可以拿到返回值来做进一步的处理了。但这也不是说会多么的复杂,无非添加点特征值罢了。

import cv2

print "发现{0}个人脸!".format(len(faces))

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

  实例

有了刚才的基础,我们就可以完成一个简单的人脸识别的小例子了。

 

基于照片:

图片素材

下面的这张图片将作为我们的检测依据。 

人脸检测代码

import cv2 
import numpy as np 

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


#指定图片的人脸识别然后存储 
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: # 大于0则检测到人脸 
for faceRect in faceRects: # 单独框出每一张人脸 
x, y, w, h = faceRect 
cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 3) #5控制绿色框的粗细 



# 写入图像 
cv2.imwrite('output.jpg',img) 
cv2.imshow("Find Faces!",img)
cv2.waitKey(0)

 

人脸检测结果

输出图片: 

 

 

基于视频:

人脸检测代码

import cv2
import sys
import logging as log
import datetime as dt
from time import sleep

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

# 打开视频捕获设备
video_capture = cv2.VideoCapture(0)


while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass

# 读视频帧
ret, frame = video_capture.read()

# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# 调用分类器进行检测
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
#flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)

# 画矩形框
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)


# 显示视频
cv2.imshow('Video', frame)


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


# 关闭摄像头设备
video_capture.release()

# 关闭所有窗口
cv2.destroyAllWindows()

 

人脸检测结果

 

Guess you like

Origin blog.csdn.net/yanjiangdi/article/details/80174249