AI New Era - God teaches you to use python+Opencv to unlock face (with source code)

Well, guys, I'm back. Those who say I'm procrastinating and not writing articles can come and beat my chest with your little fists...

So today we will talk about using python+opencv+face++ to realize face verification and face unlocking. There is also a small amount of code. You can apply these codes in other smart fields, such as smart home, to determine who you are when you enter the door, and you can also add machine learning to determine whether the person who comes is a guest or an acquaintance. Before we talk about it, we will properly topology the knowledge points about face recognition. OK, stop talking nonsense. The following is the topic.

A4tX9lYgIU1b585OyfRSeo27Eo8zWMECwWCSGGMj

Unlocking principle:

The principle is actually very simple, not so complicated and difficult to understand (except for the official language of course). Let's first talk about the principle of using Face++ to achieve face matching. Recommend a place for everyone to learn and communicate: 719+139+688, now you turn on the camera, and then you just press grab. After grabbing, your face image will be uploaded to the remote server, and then the server will extract your face to generate a unique fingerprint (identification code), which is called face_token in Face++. We will continue to talk about this later. Fingerprints represent your current identity.

3LYJFWstBq0xoC7uZRIYUKqVzfrgBzLq2uK04qWy

After uploading and being analyzed and recorded by the server, the server will send you a data packet through json, which contains all the features of your face.

When you get the json returned by the server, extract the face_token (this is very important, the face_token is equivalent to a key), and then you put the obtained face_token into the if of python to judge if the face_token matches, the verification is successful, If it does not match then unlocking fails.

Environment topology:

Operating system: windows7

Python version: python2.7

OpenCV version: 2.x

Face++ interface

Implementation plan:

1. Upload the face (we have already implemented the camera capture in this previous article, we will not be verbose in this article, just use a face image instead) After uploading, json will return the face fingerprint (face_token)

2. Create a face collection and add the face_token returned in step 1 to the collection

3. Determine whether to include the current face through python's if

Code part:

1. Get the face face_token:

FOt25mwmMcvoB8dRZKwBd66BkMOhCJ4y1CqzhkpD

Here is the focus of the content of the request:

The api stipulates that post submission is required, so request.post() is used here

The transmitted content is the requested URL, the image path (required! It can be a local absolute path, or it can be a network image as image_file, image_url). The data data is the parameters in the payload:

apikey/apisecret is required, if there is no self-application

return_attributes is optional, there are returned data such as age, gender, etc.

return_landmark is optional whether to detect and return face key points 0 is no return 1 is to detect 83 key points 2 is to detect 106 key points

running result:

Let's find a photo at random (remember that if you have requirements for pixels, or some may report an error)

D6ajQdsJ1NZ3R3HLAGkofAKWSUKPJkkEqGyjm4wS

=tHUIUryb57GGflihjE7Fo2ay8lM9Pf5rzA0xvUB

We can see that there are many values ​​in the returned json package. The most important thing here is to get the face_toen of this face, and then save it and record it on the server.

Note: There may be some mistakes in the photos that some friends randomly find on the Internet. This is likely to be a problem of picture pixels. Try to find high-definition photos. After running, the face part will be marked by a green frame.

2. Create a face collection and add face_token

Tv9OqcOEe64axQhdaT22ZqdKWbifU9U1PRezZEWr

The face_tokens filled in is the face_token returned by the Obama picture just now, don't fill it in wrong~

Also remember the name of the face set, which will be used when you query it later.

Add the face_token obtained in the previous section to a new face collection.

The next face comparison will be directly compared with the fingerprints recorded by the server. Here is a simple click on a few parameters in the payload:

display_name: the name of the face collection

outer_id: FaceSet global custom ID

force_merge:

0: Do not add face_tokens to the existing FaceSet, directly return FACESET_EXIST error

1: Add face_tokens to the existing FaceSet

face_tokens: incoming face tokens

running result:

jasgzxVV5E7QJBIF9B=NHc13U9DKbOU2aEntVubt

After running, it will return a faceset_token, we need to record this place, because we have uploaded our Obama facetoken to the collection we created on the server, this faceset token is our face collection id or a photo album It will be all right.

3. Determine whether it is an administrator:

YMBVxI8CRLDdW4hGdYwGNrMBXcwGW4mDpuPTzcqz

We put Obama's face value (face token) into python's if, set the face set (album) to be queried as the faceset tokens we obtained earlier, and then we take a photo to match.

7hAbMpKoGj7sOJEVLBa38ve8ZuQqBYf40ixPfZ4K

The laptop camera is too low to recognize, so let’s make do with the effect, it’s okay to recognize the face directly.

ddRddT350MPLlajVfy6gmGKH6kFGa5=9Zy0f0Ctr

done~

Of course, it stands to reason that there should be sprayers spraying by now. It's not safe to use pictures to unlock. It doesn't matter. If you want to do more advanced, you can add some messy verifications such as shaking your head and blinking your eyes. Security is possible. arrived.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325221366&siteId=291194637