├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Noevil Sparrow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-face-auth 2 | 3 | Installation 4 | --- 5 | 6 | ``` 7 | npm i react-native-face-auth 8 | ``` 9 | 10 | Usage 11 | --- 12 | 13 | **Step 1** 14 | 15 | Go to [https://www.azure.cn/cognitive-services/en-us/face-api](https://www.azure.cn/cognitive-services/en-us/face-api) and signup an accnout. 16 | 17 | Get the API_KEY. 18 | 19 | 20 | **Step 2** 21 | 22 | Create a person group. 23 | 24 | ``` 25 | import FaceAuth from 'react-native-face-auth'; 26 | ... 27 | 28 | let faceAuth = new FaceAuth(API_KEY); 29 | ... 30 | 31 | let createPersonGroupResponse = await faceAuth.createPersonGroup(personGroupID, personGroupName); 32 | ``` 33 | 34 | **Step 3** 35 | 36 | Signin. 37 | 38 | ``` 39 | import FaceAuth from 'react-native-face-auth'; 40 | ... 41 | 42 | let faceAuth = new FaceAuth(API_KEY); 43 | ... 44 | 45 | let signinResponse = await faceAuth.signin(personGroupID, facePictureBase64Data); 46 | 47 | // If signin fail will return an `Error` object. 48 | if (signinResponse.stack && signinResponse.message === 'STRANGER') { 49 | // Signup or other operation. 50 | } else { 51 | // Get the userID and name. 52 | } 53 | ``` 54 | 55 | **Step 4** 56 | 57 | Signup. 58 | 59 | ``` 60 | import FaceAuth from 'react-native-face-auth'; 61 | ... 62 | 63 | let faceAuth = new FaceAuth(API_KEY); 64 | ... 65 | 66 | let signupResponse = await faceAuth.signup(personGroupID, personName, facePictureBase64Data); 67 | ``` 68 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import RNFetchBlob from 'react-native-fetch-blob'; 2 | 3 | class FaceAuth { 4 | constructor(microsoftAPIKey, microsoftAPIProxy = 'https://api.projectoxford.ai') { 5 | this.microsoftAPIKey = microsoftAPIKey; 6 | this.microsoftAPIProxy = microsoftAPIProxy; 7 | }; 8 | 9 | async createPersonGroup(personGroupID, personGroupName) { 10 | let body = JSON.stringify({ 11 | name: personGroupName 12 | }); 13 | 14 | let headers = { 15 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey, 16 | 'Content-Type': 'application/json' 17 | }; 18 | 19 | try { 20 | let response = await RNFetchBlob.fetch('PUT', `${this.microsoftAPIProxy}/face/v1.0/persongroups/${personGroupID}`, headers, body); 21 | let responseJson = await response.json(); 22 | let result = { 23 | status: response.respInfo.status, 24 | data: responseJson 25 | }; 26 | 27 | return result; 28 | } catch (error) { 29 | return error; 30 | } 31 | }; 32 | 33 | async createPerson(personGroupID, personName) { 34 | let body = JSON.stringify({ 35 | name: personName 36 | }); 37 | 38 | let headers = { 39 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey, 40 | 'Content-Type': 'application/json' 41 | }; 42 | 43 | try { 44 | let response = await RNFetchBlob.fetch('POST', `${this.microsoftAPIProxy}/face/v1.0/persongroups/${personGroupID}/persons`, headers, body); 45 | let responseJson = await response.json(); 46 | let result = { 47 | status: response.respInfo.status, 48 | data: responseJson 49 | }; 50 | 51 | return result; 52 | } catch (error) { 53 | return error; 54 | } 55 | }; 56 | 57 | async createPersonFace(personGroupID, personID, picture) { 58 | let headers = { 59 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey, 60 | 'Content-Type': 'application/octet-stream' 61 | }; 62 | 63 | try { 64 | let response = await RNFetchBlob.fetch('POST', `${this.microsoftAPIProxy}/face/v1.0/persongroups/${personGroupID}/persons/${personID}/persistedFaces`, headers, picture); 65 | let responseJson = await response.json(); 66 | let result = { 67 | status: response.respInfo.status, 68 | data: responseJson 69 | }; 70 | 71 | return result; 72 | } catch (error) { 73 | return error; 74 | } 75 | }; 76 | 77 | async readPerson(personGroupID, personID) { 78 | let headers = { 79 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey 80 | }; 81 | 82 | try { 83 | let response = await RNFetchBlob.fetch('GET', `${this.microsoftAPIProxy}/face/v1.0/persongroups/${personGroupID}/persons/${personID}`, headers); 84 | let responseJson = await response.json(); 85 | let result = { 86 | status: response.respInfo.status, 87 | data: { 88 | personID: responseJson.personId, 89 | name: responseJson.name 90 | } 91 | }; 92 | 93 | return result; 94 | } catch (error) { 95 | return error; 96 | } 97 | }; 98 | 99 | async detect(picture) { 100 | let headers = { 101 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey, 102 | 'Content-Type': 'application/octet-stream' 103 | }; 104 | 105 | try { 106 | let response = await RNFetchBlob.fetch('POST', `${this.microsoftAPIProxy}/face/v1.0/detect`, headers, picture); 107 | let responseJson = await response.json(); 108 | let result = { 109 | status: response.respInfo.status, 110 | data: responseJson 111 | }; 112 | 113 | return result; 114 | } catch (error) { 115 | return error; 116 | } 117 | }; 118 | 119 | async identify(personGroupID, faceIDs) { 120 | let body = JSON.stringify({ 121 | personGroupId: personGroupID, 122 | faceIds: faceIDs, 123 | maxNumOfCandidatesReturned: 1, 124 | confidenceThreshold: 0.5 125 | }); 126 | 127 | let headers = { 128 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey, 129 | 'Content-Type': 'application/json' 130 | }; 131 | 132 | try { 133 | let response = await RNFetchBlob.fetch('POST', `${this.microsoftAPIProxy}/face/v1.0/identify`, headers, body); 134 | let responseJson = await response.json(); 135 | let result = { 136 | status: response.respInfo.status, 137 | data: responseJson 138 | }; 139 | 140 | return result; 141 | } catch (error) { 142 | return error; 143 | } 144 | }; 145 | 146 | async train(personGroupID) { 147 | let headers = { 148 | 'Ocp-Apim-Subscription-Key': this.microsoftAPIKey 149 | }; 150 | 151 | try { 152 | let response = await RNFetchBlob.fetch('PUT', `${this.microsoftAPIProxy}/face/v1.0/persongroups/${personGroupID}/train`, headers); 153 | let result = { 154 | status: response.respInfo.status 155 | }; 156 | 157 | return result; 158 | } catch (error) { 159 | return error; 160 | } 161 | }; 162 | 163 | async signin(personGroupID, picture) { 164 | try { 165 | let faceResponse = await this.detect(picture); 166 | 167 | if (faceResponse.status !== 200) { 168 | return faceResponse; 169 | } 170 | 171 | if (!faceResponse.data.length) { 172 | return new Error('NO_FACE'); 173 | } 174 | 175 | let faceIDs = faceResponse.data.map(item => item.faceId); 176 | let identifyResponse = await this.identify(personGroupID, faceIDs); 177 | 178 | if (identifyResponse.status !== 200) { 179 | return identifyResponse; 180 | } 181 | 182 | if (identifyResponse.data.length !== 1) { 183 | return new Error('TOO_MUCH_FACES'); 184 | } 185 | 186 | if (!identifyResponse.data[0].candidates.length) { 187 | return new Error('STRANGER'); 188 | } 189 | 190 | let personID = identifyResponse.data[0].candidates[0].personId; 191 | let readPersonResponse = await this.readPerson(personGroupID, personID); 192 | 193 | if (readPersonResponse.status !== 200) { 194 | return readPersonResponse; 195 | } 196 | 197 | return readPersonResponse.data; 198 | } catch(error) { 199 | return error; 200 | } 201 | }; 202 | 203 | async signup(personGroupID, personName, picture) { 204 | let createPersonResponse = await this.createPerson(personGroupID, personName); 205 | 206 | if (createPersonResponse.status !== 200) { 207 | return createPersonResponse; 208 | } 209 | 210 | let personID = createPersonResponse.data.personId; 211 | let createPersonFaceResponse = await this.createPersonFace(personGroupID, personID, picture); 212 | 213 | if (createPersonFaceResponse.status !== 200) { 214 | return createPersonFaceResponse; 215 | } 216 | 217 | // Train the persons group. 218 | let trainResponse = await this.train(personGroupID); 219 | 220 | let result = { 221 | personID: personID, 222 | personName: personName 223 | }; 224 | 225 | return result; 226 | }; 227 | }; 228 | 229 | export default FaceAuth; 230 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-face-auth", 3 | "version": "1.0.3", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/noevil/react-native-face-auth.git" 12 | }, 13 | "author": "Noevil", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/noevil/react-native-face-auth/issues" 17 | }, 18 | "homepage": "https://github.com/noevil/react-native-face-auth#readme", 19 | "dependencies": { 20 | "react-native-fetch-blob": "^0.10.0" 21 | } 22 | } 23 | --------------------------------------------------------------------------------