This article will teach you how to use Go language to realize face recognition
â–ŒForeword
Nowadays, neural networks have become very popular, and people use it for various tasks, especially facial recognition applications.
Recently, I used a software with Go language as the backend to implement a face recognition project. It can identify who the portrait (such as the pop singer) in the uploaded photo is. This sounds great, I decided to give it a try and introduce you to the whole process of the project.
It should be noted that I try to control the required system configuration as low as possible, so that more users can install by using cheap servers, and this is why the implementation process does not use CUDA or GPU—— Although you can easily rent such a server now, it requires a high cost, which will also shut out many potential users. If it only requires the CPU and does not require external dependencies to work, the situation will be much better.
â–ŒChoose the right language
If you ask data scientists or those who have practical experience in neural networks, almost everyone will suggest that you use the Python language to solve machine learning tasks. Considering the language community, the number of available libraries, the simplicity of the language, etc., the Python language is indeed a wise choice. In addition, in Python, you can also find some popular face recognition libraries through some wonderful examples and documentation.
However, this time, I decided to choose Go language for several reasons:
My forum is written in Go language, and I personally really like the convenience brought by single-binary as the backend. Therefore, it is great to deploy and integrate the face recognition process in the backend without some dependencies and IPC implemented by Python.
Go language is generally faster than Python and consumes less memory. The key part of any high-performance Python library is written in C++/C++, so you will have the overhead of the Python VM anyway. I prefer faster languages, unless this language will seriously affect development time. I don't use C or C++ as the main language for writing web applications, but the Go language is very good, it is almost as simple as Python.
I haven't found a library for face recognition in the Go language, so implementing such an application in the Go language is an interesting and helpful thing for the entire community.
â–ŒChoose the right frame
As mentioned earlier, neural networks and corresponding implementation frameworks are now widely used. In the field of computer vision alone, the available frameworks include Caffe, Torch, TensorFlow, etc.
However, there is a very cool machine learning library-the dlib library, which immediately attracted my attention. First, it is written in C++, so you can easily create Go language bindings using cgo. Secondly, in the face recognition task of the Wild benchmarks, it is said that it can achieve 99.38% accuracy, which sounds incredible. Furthermore, some popular face recognition libraries face_recognition and openface both use the dlib library at the bottom, so it will be a very good choice for this task.
â–ŒInstall dependencies
Once the framework is finalized, how do we develop and deploy this project on the machine? First of all, the installation of C++ dependencies will be very difficult, because you can't do it with simple "go get" or "pip install" commands. Either you can only hope that these dependent libraries are available in your operating system repository, or you can only install them through a cumbersome compilation process. In this case, this problem is even more annoying, because many people have encountered it during the dlib compilation process. problem.
If you have to install through the compilation process, you can refer to the following tutorial, which may help
https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
Fortunately, we have a better choice: if the user's target system is known, we can build a binary installation package of the dlib library to greatly simplify the whole process. When it comes to server software, Ubuntu is almost standard, so you must first ensure that you can support this system.
Ubuntu's standard warehouse comes with dlib library, but its version is too old: face recognition only supports dlib19.3 version, so we need to build our own package. I created PPA (Custom Repository) for Ubuntu 16.04 and 18.04. The installation process is very simple, as follows:
sudoadd-apt-repositoryppa:kagamih/dlibsudoapt-getupdatesudoapt-getinstalllibdlib-dev
The above command will install the latest dlib19.15 version and Intel's math core library. For Intel processors, this seems to be the fastest implementation of the standard BLAS and LAPACK interfaces.
For Debian sid and Ubuntu 18.10 (not yet released), the installation process of dlib is also provided in the standard warehouse. You only need the following command:
sudoapt-getinstalllibdlib-devlibopenblas-dev
This will use OpenBLAS instead of MKL, which is also very fast. Alternatively, you can also achieve this by enabling non-free package and installing libmkl-dev.
We also need libjpeg to load JPEG images: install the libjpeg-turbo8-dev package on Ubuntu, or install libjpeg62-turbo-dev on Debian.
So far, I have not given installation instructions for other systems. If you encounter problems during the installation of dlib, you can visit my github, hoping to provide you with reasonable and effective installation suggestions.
GitHub address:
https://github.com/Kagami/go-face
In addition, I also consider providing a Docker image for the dlib library (a few of which already exist). Many projects with complex dependencies tend to use this distributed approach. But in my opinion, a native package can provide users with a better experience, you don't need to write long commands on the console, and you don't need to deal with the content in the sandbox environment.
â–ŒWrite dependency library
The working principle of the current face recognition library is usually: by returning a set of numbers (vector embedding or descriptor) for each face on the photo to compare and distinguish them, and by comparing these numbers to find the name of the person in the image (usually By calculating the Euclidean distance vector, the minimum distance between two faces belonging to the same person is obtained). This concept will not be repeated here this time.
The original code for creating the face in the image is not an important issue. The process is almost an official example. You can check facerec.cc and its corresponding header file facerec.h, which defines 5 functions and several interaction structures between the Go language and the dlib library.
Here, although the dlib library supports all popular image formats, it can only load them from files. This will cause confusion because we usually only save the image in memory and write it to a temporary file. Therefore, here I use libjpeg to write my own image loader. Since most photos are stored in this format, the loader in this format is sufficient for most of the needs. I will add image loader in other formats as needed in the future.
I put the connection layer of C++ and Go language in face.go. It provides a Face structure, which is used to save the coordinates of the face in the image and its descriptor, and provides an interface for all operations, such as initialization and actual recognition, through the Recognizer.
Once we have the descriptor, what can we do? In the simplest case, you can compare the Euclidean distance between the unknown descriptor and all known descriptors. But this is not perfect. Even the most advanced face recognition technology will get the wrong answer. If we want to improve the results a little bit, we need to use many images of each person and check if any of these images are very close to the provided face.
This is exactly what the classifier classify.cc does. First, calculate the distance, then sort these distances, and calculate the number of clicks of the same person in the top 10 minimum distances. )
Such as support vector machines, will provide better algorithm performance on this task. dlib even provides a convenient API for training such models. Few articles mention the performance of SVM on large data sets, so I plan to test it on large sets first.
â–ŒUse
You can view the results obtained below in github:
import "github.com/Kagami/go-face"
GitHub address:
https://github.com/Kagami/go-face
For an overview of all related structures and methods, please refer to the GoDoc document, which mainly includes the following contents:
Initialize the recognizer
Identify all known images and collect descriptors
Pass the known descriptor with the corresponding category to the recognizer
Get the descriptor of the unknown image
Categorize its category
The following is a working example to illustrate all the above steps:
packagemainimport("fmt""log""path/filepath""github.com/Kagami/go-face")//Pathtodirectorywithmodelsandtestimages.Hereit's//assumeditpointstothe//clone.constdataDir="testdata"//Thisexampleshowsthebasicusageofthepackage:createan//recognizer ,recognizefaces,classifythemusingfewknown//ones.funcmain(){//Inittherecognizer.rec,err:=face.NewRecognizer(dataDir)iferr!=nil{log.Fatalf("Can'tinitfacerecognizer:%v",err)}// Freetheresourceswhenyou'refinished.deferrec.Close()//Testimagewith10faces.testImagePristin:=filepath.Join(dataDir,"pristin.jpg")//Recognizefacesonthatimage.faces,err:=rec.RecognizeFile(testImagePristin)iferr!=nil{log. Fatalf("Can'trecognize:%v",err)}iflen(faces)!=10{log.Fatalf("Wrongnumberoffaces")}//Fillknownsamples.Intherealworldyouwouldusealotof//imagesforeachpersontogetbetterclassificationresults//butinoursample[bigimages ]int32fori,f:=rangefaces{samples=append(samples,f.Descriptor)//Eachfaceisuniqueonthatimagesogoestoitsown//c ategory.cats=append(cats,int32(i))}//Namethecategories,iepeopleontheimage.labels:=[]string{"Sungyeon","Yehana","Roa","Eunwoo","Xiyeon","Kyulkyung" ,"Nayoung","Rena","Kyla","Yuha",}//Passsamplestotherecognizer.rec.SetSamples(samples,cats)//Nowlet'strytoclassifysomenotyetknownimage.testImageNayoung:=filepath.Join(dataDir,"nayoung.jpg" )nayoungFace,err:=rec.RecognizeSingleFile(testImageNayoung)iferr!=nil{log.Fatalf("Can'trecognize:%v",err)}ifnayoungFace==nil{log.Fatalf("Notasinglefaceontheimage")}catID:= rec.Classify(nayoungFace.Descriptor)ifcatID
Run the following command:
mkdir-p~/go&&cd~/go#Orcdtoyour$GOPATHmkdir-psrc/go-face-example&&cdsrc/go-face-examplegitclonehttps://github.com/Kagami/go-face-testdatatestdataeditmain.go#Pasteexamplecodegoget.../. ./bin/go-face-example
Due to the extensive use of C++ templates in the dlib code, it takes some time to compile go-face (it takes about 1 minute to run on my i7). Fortunately, the Go language is able to build output caches, so that it can be built faster in the future.
The sample output above should print "Nayoung", indicating that the unknown image can be correctly identified.
â–ŒModel
go-face requires shape_predictor_5_face_landmarks.dat and
dlib_face_recognition_resnet_model_v1.dat model can start working. You can download them from the dlib-models repository:
mkdirmodels&&cdmodelswgethttps://github.com/davisking/dlib-models/raw/master/shape_predictor_5_face_landmarks.dat.bz2bunzip2shape_predictor_5_face_landmarks.dat.bz2wgethttps://github.com/davisking/dlib-models/raw/master/shape_predictor_5_face_landmarks.dat.bz2bunzip2shape_predictor_5_face_landmarks.dat.bz2wgethttps://github.com/davisking/dlib-cognition_bz dat.bz2
In addition, when you want to run the sample code, you can also access these models through the go-face-testdata repository.
â–ŒFuture work
I am very satisfied with the results. I get good recognition results through a simple API and can be easily embedded in Go applications. Of course, there is still room for improvement:
In pursuit of simplicity and speed, when creating descriptors, go-face cannot perform some preprocessing of the image, such as dithering. However, it is necessary to increase the image preprocessing operation because it may improve the recognition performance.
The Dlib library supports many image formats (such as JPEG, PNG, GIF, BMP, DNG), but go-face currently only implements the JPEG format, and we hope to support more formats in future work.
As suggested by Davis, the author of dlib, compared to the minimum search distance, the use of multi-class SVM may get better classification results, so additional testing and verification are required.
In go-face, unless I really need it, I try not to copy the value, but in fact it has tested the test performance of a large sample (10,000+ face data set), and there may be some bottlenecks, which need to be improved in the future.
Extracting feature vectors from faces is a powerful concept, because you don’t need to collect your own training data, which is also a very difficult task (Davis mentioned the 3 million face dataset used to create the ResNet model in dlib ), but in order to obtain higher recognition performance, this may be unavoidable, so it is worth to provide corresponding tools for the training of your own model.
ZGAR GenkiIppai Pods 5.0
ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.
From production to packaging, the whole system of tracking, efficient and orderly process, achieving daily efficient output. WEIKA pays attention to the details of each process control. The first class dust-free production workshop has passed the GMP food and drug production standard certification, ensuring quality and safety. We choose the products with a traceability system, which can not only effectively track and trace all kinds of data, but also ensure good product quality.
We offer best price, high quality Pods, Pods Touch Screen, Empty Pod System, Pod Vape, Disposable Pod device, E-cigar, Vape Pods to all over the world.
Much Better Vaping Experience!
Pods, Vape Pods, Empty Pod System Vape,Disposable Pod Vape Systems, Japanese culture style
ZGAR INTERNATIONAL(HK)CO., LIMITED , https://www.szvape-pods.com