Python black technology: 50 lines of code using Python + OpenCV to achieve face tracking + image recognition + face recognition + the most detailed tutorial + great sharing

Hi my dearest guys, glad we meet again.

First of all, thank you for your attention. Of course, I would prefer to meet friends in computer-related fields and let's discuss and communicate together. The point is, I'm a real person, not a self-media organization that picks up articles, everyone can communicate with each other!

In this article, we will talk about some knowledge about AI-related face tracking and face recognition. Of course, this tutorial is the (top) part. I will talk about using python+opencv to realize face recognition and tracking. In the next part, I will use python to realize face verification and face unlocking through fingerprint comparison (everyone is interested in it.) You can pay attention in advance).

In these two classes, the amount of code is not very large. I will try to annotate as many points as possible to facilitate everyone's understanding. Then we won't talk too much nonsense, let's go straight to the dry goods!

OpenCV:

Opencv is currently a very popular visual library and can support multiple languages. When it comes to opencv, I have to say its cascades classifier.

If we want to judge whether a picture has a face, the early method is to use thousands of classifiers to match from the beginning to the end. There is nothing wrong with this, but there are too many pictures to judge? That might take the Year of the Monkey and the Horse Moon. As for the cascades of opencv, these containers for judging face features are divided into multi-block layers for matching, and when one layer does not match, they are discarded.

This is like a group of people going to a company for an interview. The first requirement of the company is that if only men are required, the group of women will leave. ,till the end. This kind of workload is much easier than going through the process of interviewing everyone regardless of gender.

Environment topology:

Operating system: windows7

python version: 2.7.14

opencv version: 3.x

Environment configuration:

1. Install python (uh... this is when I didn't say it)

2. Install Opencv

This is OK to download from the official website

After downloading, unzip it directly. It is recommended to unzip it to the parent path of your python installation.

3. Install numpy using pip

Open cmd and enter:

pip install numpy

To install, you will be prompted after the installation is complete.

4. Find your opencv installation path (for example, mine is D drive)

复制D:opencvopencv3.xuildpython.7\x64路径下的cv2.py

注意:我的windows版本为64位所以我选择的是X64,如果你的是32位的话你需要选择X86文件夹下的cv2.pyd

复制完之后,粘贴到你的python安装路径下的Lib/site-packages这个文件夹下。

完成上面这些就基本OK啦,我们在写代码之前先来测试一下,环境是否配置成功。

在CMD命令行下运行python:

import numpy

import cv2

如果没有报错,说明安装完成。

实现原理:

1.调用计算机摄像头

cv2.Videocamture(0)

2.将摄像头数据按帧来取(相当于给每一帧图片的人脸加框框)

cam.red()

3.将每一帧摄像头记录的数据带入opencv让classifier去判断人脸

detectMultiScale()

4.如果存在人脸给人脸标记画框

cv2.rectangle()

5.输出画框后的帧动画

cv2.imshow('My Camera',frame)

代码实现:

第一步定义一个识别函数:

先放图片,这个地方比较重要,我在下面会详细的说一下。

6-7行代码说明:

首先创建classifier,为什么要弄这个呢?

引用的haarcascade开头的文件是opencv里面关于人脸级联分类器,你在opencv文件夹下的sourcesdatahaarcascades可以看到:

说明功能:

人脸检测器(默认):haarcascade_frontalface_default.xml

人脸检测器(快速Harr):haarcascade_frontalface_alt2.xml

人脸检测器(侧视):haarcascade_profileface.xml

眼部检测器(左眼):haarcascade_lefteye_2splits.xml

眼部检测器(右眼):haarcascade_righteye_2splits.xml

嘴部检测器:haarcascade_mcs_mouth.xml

鼻子检测器:haarcascade_mcs_nose.xml

身体检测器:haarcascade_fullbody.xml

人脸检测器(快速LBP):lbpcascade_frontalface.xml

1:haarcascade_frontalface_alt.xml

Stump-based 20x20 gentle adaboost frontal face detector.

2:haarcascade_frontalface_alt2.xml

Tree-based 20x20 gentle adaboost frontal face detector.

3:haarcascade_frontalface_alt_tree.xml

Stump-based 20x20 gentle adaboost frontal face detector.This detector uses tree of stage classifiers instead of a cascade

4:haarcascade_frontalface_default.xml

Stump-based 24x24 discrete(?) adaboost frontal face detector.

以上这四个鄙人都测试过,haarcascade_frontalface_alt.xml这个效果是最好的,其它的大家可以单个测试。

设定灰度:

灰度的设定是为了增强面部轮廓的对比度,这是增加精度必不可少的。

核心代码解剖:

DetectMultiScale 函数是一个检测物体的通用函数,我们介绍一下:

gray:这是转换成灰度后的图片

scaleFactor:补偿参数,一般设置值为1.1-1.5比较好,效果大家自己改着看

minNeighbors:对当前其周围有多少物体进行定义

minSize:设定窗口大小

flags:略抽象,选择默认或者上图即可

画方框:

定义xywh利用for循环读取faces变量里面的坐标,然后通过rectangle()函数进行画方框。

代码下部分:

OK,写完代码之后,我们把上部分代码跟下部分代码拼凑起来,运行下试一下:

大家凑合看吧,我笔记本cam有点渣,不过随着人的晃动,都可以完美的识别出来,由于没法上视频,大家就自行测试吧。最后再强调一点:明后天出(下)部分,用python来通过指纹对比实现人脸验证、人脸解锁(大家感兴趣的可以提前关注哦)。

大家如果想要学习Python可以加我的群:719+139+688,小编整理了2018最新的学习资料和大神分享直播。欢迎还没有找到方向的小伙伴来学习。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326719226&siteId=291194637