Skip to content

Commit 4abc5f5

Browse files
authored
Merge pull request #7 from exadel-inc/sdk-0.5.1-new-functionality
Sdk 0.5.1 new functionality
2 parents cc71d41 + 64ccb2e commit 4abc5f5

25 files changed

+12299
-81
lines changed

README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CompreFace JavaScript SDK makes face recognition into your application even easi
99
- [Initialization](#initialization)
1010
- [Adding faces into a face collection](#adding-faces-into-a-face-collection)
1111
- [Recognition](#recognition)
12+
- [Enviroments](#enviroments)
13+
- [Webcam demo](#webcam-demo)
1214
- [Reference](#reference)
1315
- [CompreFace Global Object](#compreface-global-object)
1416
- [Recognition Service](#recognition-service)
@@ -102,6 +104,17 @@ recognitionService.recognize(path_to_image)
102104
})
103105
```
104106

107+
### Enviroments
108+
NOTE: We provide 3 ways of uploading image to our SDK. They are url, blob and relative path (from local machine).
109+
110+
| Enviroments | from URL | with Blob format | from local machine|
111+
| ------------|--------- | ---------------- | ---------------- |
112+
| Browser ||||
113+
| Nodejs ||||
114+
115+
### Webcam demo
116+
[Documentation is here](/webcam_demo)
117+
105118
## Reference
106119

107120
### CompreFace Global Object
@@ -209,7 +222,7 @@ The first argument is the image location, it could be a URL or a path on the loc
209222

210223
| Argument | Type | Required | Notes |
211224
| --------------- | ------ | -------- | ----------------------------------------- |
212-
| image_location | string | required | URL or local machine path to the image you want to recognize |
225+
| image_location | string | required | URL, image in BLOB format or image from your local machine|
213226
| options | string | optional | Object that defines recognition options |
214227

215228
Supported options:
@@ -315,7 +328,7 @@ Adds an image to your face collection.
315328

316329
| Argument | Type | Required | Notes |
317330
| --------------- | ------ | -------- | ----------------------------------------- |
318-
| image_location | string | required | URL or local machine path to the image you want to add to face collection |
331+
| image_location | string | required | URL, image in BLOB format or image from your local machine |
319332
| subject | string | required | Name or any other person ID. It can be just a random string you generate and save for further identification |
320333
| options | string | optional | Object that defines adding options |
321334

@@ -481,7 +494,7 @@ Compares similarities of given image with image from your face collection.
481494

482495
| Argument | Type | Required | Notes |
483496
| --------------- | ------ | -------- | ----------------------------------------- |
484-
| image_location | string | required | URL or local machine path to the image you want to recognize |
497+
| image_location | string | required | URL, image in BLOB format or image from your local machine |
485498
| options | string | optional | Object that defines recognition options |
486499

487500
Supported options:
@@ -576,7 +589,7 @@ The first argument is the image location, it could be a URL or a path on the loc
576589

577590
| Argument | Type | Required | Notes |
578591
| --------------- | ------ | -------- | ----------------------------------------- |
579-
| image_location | string | required | URL or local machine path to the image you want to recognize |
592+
| image_location | string | required | URL, image in BLOB format or image from your local machine |
580593
| options | string | optional | Object that defines detection options |
581594

582595
Supported options:
@@ -667,8 +680,8 @@ The first two arguments are the image location, it could be a URL or a path on t
667680

668681
| Argument | Type | Required | Notes |
669682
| ---------------------- | ------ | -------- | ----------------------------------------- |
670-
| source_image_location | string | required | URL or local machine path to the source image you want to compare |
671-
| target_image_location | string | required | URL or local machine path to the target image you want to compare |
683+
| source_image_location | string | required | URL, source image in BLOB format or source image from your local machine |
684+
| target_image_location | string | required | URL, target image in BLOB format or target image from your local machine |
672685
| options | string | optional | Object that defines detection options |
673686

674687
Supported options:

endpoints/common_endpoints.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
import axios from 'axios';
17+
import FormData from 'form-data';
18+
19+
// Collection of common endpoints that used by almost all services
20+
const common_endpoints = {
21+
async upload_blob(blobData, url, api_key){
22+
var bodyFormData = new FormData();
23+
bodyFormData.append('file', blobData, 'example.jpg');
24+
25+
return new Promise( async (resolve, reject) => {
26+
try {
27+
const response = await axios.post( url, bodyFormData, {
28+
headers: {
29+
'Content-Type': 'multipart/form-data',
30+
"x-api-key": api_key
31+
},
32+
})
33+
34+
resolve(response)
35+
} catch (error) {
36+
reject(error)
37+
}
38+
})
39+
}
40+
}
41+
42+
export { common_endpoints }

endpoints/verification_endpoints.js

+158-50
Original file line numberDiff line numberDiff line change
@@ -100,61 +100,169 @@ const verification_endpoints = {
100100
*/
101101
one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, api_key ){
102102
var bodyFormData = new FormData();
103+
let path_is_url = [];
104+
let path_is_relative = [];
103105

104106
if(isSourceImageUrl){
105-
bodyFormData.append('target_image', fs.createReadStream(target_image_path), { knownLength: fs.statSync(target_image_path).size });
106-
107-
return new Promise( async (resolve, reject) => {
108-
await axios.get(source_image_path, { responseType: 'stream' })
109-
.then( async (response) => {
110-
let image_extention = response.headers['content-type'].split("/")[1]
111-
bodyFormData.append('source_image', response.data, `example.${image_extention}`);
112-
113-
try {
114-
const res = await axios.post( url, bodyFormData, {
115-
headers: {
116-
...bodyFormData.getHeaders(),
117-
"x-api-key": api_key
118-
},
119-
})
120-
121-
resolve(res)
122-
} catch (error) {
123-
reject(error)
124-
}
125-
})
126-
.catch(error => {
107+
path_is_url[0] = "source_image";
108+
path_is_url[1] = source_image_path;
109+
110+
path_is_relative[0] = "target_image";
111+
path_is_relative[1] = target_image_path;
112+
}else{
113+
path_is_url = "target_image";
114+
path_is_url[1] = target_image_path;
115+
116+
path_is_relative = "source_image";
117+
path_is_relative[1] = source_image_path;
118+
}
119+
120+
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });
121+
122+
return new Promise( async (resolve, reject) => {
123+
await axios.get(path_is_url[1], { responseType: 'stream' })
124+
.then( async (response) => {
125+
let image_extention = response.headers['content-type'].split("/")[1]
126+
bodyFormData.append(path_is_url[0], response.data, `example.${image_extention}`);
127+
128+
try {
129+
const res = await axios.post( url, bodyFormData, {
130+
headers: {
131+
...bodyFormData.getHeaders(),
132+
"x-api-key": api_key
133+
},
134+
})
135+
136+
resolve(res)
137+
} catch (error) {
127138
reject(error)
128-
})
129-
})
130-
}else {
131-
bodyFormData.append('source_image', fs.createReadStream(source_image_path), { knownLength: fs.statSync(source_image_path).size });
132-
133-
return new Promise( async (resolve, reject) => {
134-
await axios.get(target_image_path, { responseType: 'stream' })
135-
.then( async (response) => {
136-
let image_extention = response.headers['content-type'].split("/")[1]
137-
bodyFormData.append('target_image', response.data, `example.${image_extention}`);
138-
139-
try {
140-
const res = await axios.post( url, bodyFormData, {
141-
headers: {
142-
...bodyFormData.getHeaders(),
143-
"x-api-key": api_key
144-
},
145-
})
146-
147-
resolve(res)
148-
} catch (error) {
149-
reject(error)
150-
}
151-
})
152-
.catch(error => {
139+
}
140+
})
141+
.catch(error => {
142+
reject(error)
143+
})
144+
})
145+
},
146+
/**
147+
* Verify face(s) from given blob data
148+
* @param {String} source_image_path
149+
* @param {String} target_image_path
150+
* @param {Boolean} isSourceBlob
151+
* @param {String} url
152+
* @param {String} api_key
153+
* @returns {Promise}
154+
*/
155+
url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, api_key){
156+
let bodyFormData = new FormData();
157+
let path_is_url = [];
158+
let path_is_blob = [];
159+
160+
if(isSourceImageUrl){
161+
path_is_url[0] = "source_image";
162+
path_is_url[1] = source_image_path;
163+
164+
path_is_blob[0] = "target_image";
165+
path_is_blob[1] = target_image_path;
166+
}else{
167+
path_is_url = "target_image";
168+
path_is_url[1] = target_image_path;
169+
170+
path_is_blob = "source_image";
171+
path_is_blob[1] = source_image_path;
172+
}
173+
bodyFormData.append(path_is_blob[0], path_is_blob[1], 'example.jpg');
174+
175+
return new Promise( async (resolve, reject) => {
176+
await axios.get(path_is_url[1], { responseType: 'stream' })
177+
.then( async (response) => {
178+
let image_extention = response.headers['content-type'].split("/")[1]
179+
bodyFormData.append(path_is_url[0], response.data, `example.${image_extention}`);
180+
181+
try {
182+
const res = await axios.post( url, bodyFormData, {
183+
headers: {
184+
...bodyFormData.getHeaders(),
185+
"x-api-key": api_key
186+
},
187+
})
188+
189+
resolve(res)
190+
} catch (error) {
153191
reject(error)
154-
})
155-
})
192+
}
193+
})
194+
.catch(error => {
195+
reject(error)
196+
})
197+
})
198+
},
199+
200+
/**
201+
* Both source and target images are blob
202+
* @param {Blob} source_image_blob
203+
* @param {Blob} target_image_blob
204+
* @param {String} url
205+
* @param {String} api_key
206+
*/
207+
both_blob_request(source_image_blob, target_image_blob, url, api_key){
208+
var bodyFormData = new FormData();
209+
210+
bodyFormData.append('source_image', source_image_blob, 'example.jpg');
211+
bodyFormData.append('target_image', target_image_blob, 'example1.jpg');
212+
213+
return new Promise( async (resolve, reject) => {
214+
try {
215+
const response = await axios.post( url, bodyFormData, {
216+
headers: {
217+
'Content-Type': 'multipart/form-data',
218+
"x-api-key": api_key
219+
},
220+
})
221+
222+
resolve(response)
223+
} catch (error) {
224+
reject(error)
225+
}
226+
})
227+
},
228+
229+
one_blob_request(source_image_path, isSourceImageBlob, target_image_path, url, api_key ){
230+
var bodyFormData = new FormData();
231+
let path_is_blob = [];
232+
let path_is_relative = [];
233+
234+
if(isSourceImageBlob){
235+
path_is_blob[0] = "source_image";
236+
path_is_blob[1] = source_image_path;
237+
238+
path_is_relative[0] = "target_image";
239+
path_is_relative[1] = target_image_path;
240+
}else{
241+
path_is_blob = "target_image";
242+
path_is_blob[1] = target_image_path;
243+
244+
path_is_relative = "source_image";
245+
path_is_relative[1] = source_image_path;
156246
}
157-
}
247+
248+
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });
249+
bodyFormData.append(path_is_blob[0], path_is_blob[1], 'example.jpg');
250+
251+
return new Promise( async (resolve, reject) => {
252+
try {
253+
const response = await axios.post( url, bodyFormData, {
254+
headers: {
255+
'Content-Type': 'multipart/form-data',
256+
"x-api-key": api_key
257+
},
258+
})
259+
260+
resolve(response)
261+
} catch (error) {
262+
reject(error)
263+
}
264+
})
265+
},
158266

159267

160268
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@exadel/compreface-js-sdk",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"license": "Apache-2.0",
55
"description": "JavaScript SDK for CompreFace - free and open-source face recognition system from Exadel",
66
"main": "index.js",

services/detection_service.js

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { detection_endpoints } from '../endpoints/detection_endpoints.js';
1818
import { recognition_endpoints } from '../endpoints/recognition_endpoints.js';
19+
import { common_endpoints } from '../endpoints/common_endpoints.js';
1920
import { common_functions } from '../functions/index.js';
2021

2122
class DetectionService {
@@ -59,6 +60,14 @@ class DetectionService {
5960
.catch(error => {
6061
reject(error)
6162
})
63+
}else if(image_path instanceof Blob) {
64+
common_endpoints.upload_blob(image_path, url, this.key)
65+
.then(response => {
66+
resolve(response.data)
67+
})
68+
.catch(error => {
69+
reject(error)
70+
})
6271
}else {
6372
detection_endpoints.detect_request(image_path, url, this.key)
6473
.then(response => {

0 commit comments

Comments
 (0)