ok , Guys , I am back . Those who say I'm dragging my feet and don't write an article can come and beat me on the chest with your little fist ....
So today we are going to talk about using python+opencv+face++ To realize face verification and face unlocking . The amount of code is also small , You can apply this code to other intelligent fields , Such as smart home , Judge who you are when you enter the door , You can also add machine learning to judge whether the person coming is a guest or an acquaintance . Before we talk about it, we will talk about some knowledge about face recognition .OK Cut the crap. Let's start with the topic .
Unlock the principle :
The principle , It's very simple , It's not that complicated ( Of course, in addition to the official language ). Let's talk about the use of Face++ The principle of face matching . Recommend a place for learning and communication :719+139+688, Now you turn on the camera , Then you just press grab . After grabbing , Your face image will be uploaded to the remote server , Then the server will extract your face and generate a unique fingerprint ( Identification code ), This fingerprint is in Face++ It's called face_token We will continue to talk about this later , This fingerprint represents your current identity .
After uploading, it is analyzed and collected by the server , The server will pass json Send you a packet , This packet contains all your facial features .
When you get the server and return json after , take face_token extracted ( It's important ,face_token Equivalent to a key ) Then you take what you get face_token Put it in python Of if Inside to judge if face_token If it matches, the verification is successful , If it does not match, the unlocking fails .
Environment topology :
operating system :windows7 Python edition :python2.7OpenCV edition :2.xFace++ Interface
Implementation scheme :
1. Upload faces ( In the last article, we have implemented camera capture , In this article, we will not be verbose , Use a face picture instead ) After the upload json Will return the facial fingerprint (face_token)
2. Create a face collection , And put the steps 1 Back to face_token Join the group
3. adopt python Of if Judge whether to include the current face
Code section :
1. Get faces face_token:
Let's focus on request What's in it :
api The rule is to post Submit , So I used request.post()
The content transmitted is requested URL, Picture path ( Required ! It can be a local absolute path , It can also be a network picture image_file、image_url)data Data means payload The parameters inside :
apikey/apisecret Required No self application return_attributes optional There is the returned data There are age, gender and so on return_landmark optional Whether to detect the returned face key 0 Is not to return 1 For testing 83 A key point 2 For testing 106 A key point
Running effect :
Let's just look for a photo ( There are requirements for pixels Bear in mind Otherwise, some may report errors )
We can see the returned json There are many values in the bag , The most important thing here is to get this face face_toen, And save it , It is necessary to record to the server .
notes : There may be some friends who may report wrong photos on the Internet , This is probably a problem with the picture pixels , Try to find high definition , After operation , The face part will be marked with a green box .
2. Create a face collection , And add face_token
face_tokens What is filled in is the picture returned by Obama just now face_token, Don't make a mistake ~
The name of the face set should also be remembered , It will also be used later in the query .
Take the face_token Add to a new face collection .
The next face comparison will be directly compared with the fingerprint collected by the server . Here is another simple point payload Some parameters in it :
display_name: The name of the face set outer_id:FaceSet Global custom identity force_merge:0: Will not face_tokens Add existing FaceSet in , Go straight back to FACESET_EXIST error 1: take face_tokens Add existing FaceSet in face_tokens: Incoming face id
Running effect :
After running , Will return a faceset_token, We need to record this place , Because we have put our Obama facetoken Uploaded to the collection we created on the server , This faceset token It's our face set id Or a photo album .
3. Determine if it's an administrator :
We put Obama's face value (face token) Put in python Of if Inside , Face set to be queried ( Photo album ) Set to the faceset tokens, Then we take a random photo to match .
The laptop camera is too low, It's hard to recognize , Just make do to see the effect , Face recognition is OK .
complete ~
Of course , It is reasonable to say that there should be spraying now , It's not safe to unlock with pictures , No problem , If you want to be more advanced , You can add some shaking your head Blink and so on , Security can be achieved .