preface

Today, I went to work as usual, and then I saw such a problem on the hot list

The answers below are also interesting, ranging from those who say it is a true pupil killer, to those who worry about the frequent collection of facial information, to those who say it is necessary to wear clothes. In short, there is a lot of talk about facial recognition.

As a smacking coder, I was wondering if I could implement a simple face recognition function in Java. Then do it!

Ten key technologies for face recognition

Before that, first of all, we must know what problems face recognition needs to be broken. After checking some information, we summed up the following ten keys to be solved

  • Face Detection: A technique that detects the location of a human Face in an image
  • Face Alignment is a technology used to locate key coordinates of facial features
  • Face Attribute recognition (Face Attribute) : is a technology to identify the gender, age, posture, expression and other attributes of the Face
  • FaceFeatureExtraction: it is the process of converting a face image into a series of fixed length values
  • Face Compare: An algorithm that measures the similarity between two faces
  • Face Verification: An algorithm that determines whether two images of faces are the same person
  • Face Recognition (Face Recognition) : is to identify the input Face map corresponding to the identity of the algorithm
  • Face retrieval: is to find and input similar face sequence algorithm
  • Face Clustering (Face Cluster) : it is an algorithm that groups faces in a set according to their identities
  • Face Liveness: Is how we determine whether images of a Face are from a real person or from an attack prosthetic (photos, video, etc.)

Enmmm, after checking a lot of information, I found that face recognition technology is still quite complicated. It is a little unrealistic to tear a recognition algorithm with Java by hand. After all, strength does not allow me to be so arrogant.

Ha ha, the slap in the face came so quickly, it was really caught off guard.

I found a free face recognition SDK: ArcSoft, address: ai.arcsoft.com.cn

This platform can generate APPID with one KEY, SDK KEY will be used later, choose different environment according to need (this article is based on Windows environment), and then download SDK is a compressed package.

Java Project Setup

Finally, under my hard search, I finally found a Java version of ArcSoft Demo, open source is really a wonderful thing, no more words, open!

1. Download the demo project

Github address: github.com/xinzhfiu/Ar… Create table user_face_info to create database locally. This table is mainly used to store portrait features. The main field face_feature uses binary bloB to store face features.

SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user_face_info -- ---------------------------- DROP TABLE IF EXISTS `user_face_info`; CREATE TABLE 'user_face_info' (' id 'int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', 'group_id' int(11) DEFAULT NULL COMMENT 'id',' face_id 'varchar(31) DEFAULT NULL COMMENT' iD ', 'name' varchar(63) DEFAULT NULL COMMENT 'iD ',' age 'int(3) DEFAULT NULL COMMENT' iD ', 'email' varchar(255) DEFAULT NULL COMMENT 'address ',' gender 'smallint(1) DEFAULT NULL COMMENT' Gender, 1= male, 1 = female ', 'phone_number' varchar(11) DEFAULT NULL COMMENT 'phone ',' face_feature 'blob COMMENT' face ', 'create_time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create_time ', 'update_time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update_time ', 'fpath' varchar(255) COMMENT 'fpath ', PRIMARY KEY (' id') USING BTREE, KEY `GROUP_ID` (`group_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; SET FOREIGN_KEY_CHECKS = 1;Copy the code

2. Modify the application.properties file

The whole project is still relatively complete, just need to change some configuration can start, but there are a few points to note, will be highlighted later.

Config. arcface-sdk.sdk-lib-path: indicates the path for storing three. DLL files in the SDK package

Config.arcface-sdk. app-id: indicates the APPID of the developer center

Config.arcface -sdk.sdk-key: SDK key of the developer center

config.arcface-sdk.sdk-lib-path=d:/arcsoft_lib config.arcface-sdk.app-id=8XMHMu71Dmb5UtAEBpPTB1E9ZPNTw2nrvQ5bXxBobUA8 Config. Arcface - SDK. The SDK - key = # BA8TLA9vVwK7G6btJh2A2FCa8ZrC6VWZLNbBBFctCz5R druid address local database Spring. The datasource. The druid. Url = JDBC: mysql: / / 127.0.0.1:3306 / xin - master? useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.druid.username=junkang spring.datasource.druid.password=junkangCopy the code

3. Create a lib folder from the root directory

Create folder lib in the root directory of the project, and put arcsoft-sdK-face-2.2.0.1. jar in the downloaded SDK package into the root directory of the project

4. Introduce the Arcsoft dependency package

< the dependency > < groupId > com. Arcsoft. Face < / groupId > < artifactId > arcsoft - SDK - face < / artifactId > < version > 2.2.0.1 < / version > The < scope > system < / scope > < systemPath > ${basedir} / lib/arcsoft - SDK - face - 2.2.0.1. Jar < systemPath > < / dependency >Copy the code

The PEM. XML file must be configured with includeSystemScope attributes; otherwise, arcsoft-SDK-face-2.2.0.1. jar may not be referenced

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  <configuration> <includeSystemScope>true</includeSystemScope> <fork>true</fork> </configuration> </plugin> </plugins> </build>Copy the code

5. Start the project

At this point the configuration is complete and the Run Application file is started

Test the demo. The following page is successfully launched

Start operation

1, input face image

Enter the name on the page, click the camera to register and tune up the local camera. After submission, the current image will be passed into the background, the current face signs will be identified and extracted, and saved to the database.

2, face comparison

After inputting face image, test whether it can be recognized successfully, submit the current image, and find that the similarity of recognition success is 92%. But as a programmer to be suspicious of everything, this result is not the old iron in the page to write dead, right?

To further verify, this time to block the face and try again, found that the prompt “face does not match”, proving that there is really a comparison.

Source code analysis

A brief look at the project source code, analysis of the implementation process:

Pages and JS are written by back-end programmers, don’t ask me why? Understand all understand, ha ha ha ~, the source code here is not posted, too cumbersome, interested in can focus on the following three parts.

1. JS activates the local camera to take photos and uploads the image file string

2. Background analysis of pictures and extraction of portrait features

Background analysis of the front end of the picture, portrait feature extraction into the database, portrait feature extraction is mainly by FaceEngine engine, look all the way down the source code, their knowledge is really do not understand what kind of algorithm.

3. Portrait feature comparison

Face recognition: after extracting portrait features from the incoming image, compare and analyze the existing portrait information in the library

Background analysis of the front end of the picture, portrait feature extraction into the database, portrait feature extraction is mainly by FaceEngine engine, look all the way down the source code, their knowledge is really do not understand what kind of algorithm.

The general flow chart of the whole face recognition function is as follows:

conclusion

The design idea of the whole project is quite clear, the difficulty lies in the face recognition engine and front-end JS part of the code, other functions are relatively common.

Finally, like and forward all right everybody iron juice!