Skip to content

Commit

Permalink
Merge pull request #7 from exadel-inc/sdk-0.5.1-new-functionality
Browse files Browse the repository at this point in the history
Sdk 0.5.1 new functionality
  • Loading branch information
pospielov authored May 6, 2021
2 parents cc71d41 + 64ccb2e commit 4abc5f5
Show file tree
Hide file tree
Showing 25 changed files with 12,299 additions and 81 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CompreFace JavaScript SDK makes face recognition into your application even easi
- [Initialization](#initialization)
- [Adding faces into a face collection](#adding-faces-into-a-face-collection)
- [Recognition](#recognition)
- [Enviroments](#enviroments)
- [Webcam demo](#webcam-demo)
- [Reference](#reference)
- [CompreFace Global Object](#compreface-global-object)
- [Recognition Service](#recognition-service)
Expand Down Expand Up @@ -102,6 +104,17 @@ recognitionService.recognize(path_to_image)
})
```

### Enviroments
NOTE: We provide 3 ways of uploading image to our SDK. They are url, blob and relative path (from local machine).

| Enviroments | from URL | with Blob format | from local machine|
| ------------|--------- | ---------------- | ---------------- |
| Browser ||||
| Nodejs ||||

### Webcam demo
[Documentation is here](/webcam_demo)

## Reference

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

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

Supported options:
Expand Down Expand Up @@ -315,7 +328,7 @@ Adds an image to your face collection.

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

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

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

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

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

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

| Argument | Type | Required | Notes |
| ---------------------- | ------ | -------- | ----------------------------------------- |
| source_image_location | string | required | URL or local machine path to the source image you want to compare |
| target_image_location | string | required | URL or local machine path to the target image you want to compare |
| source_image_location | string | required | URL, source image in BLOB format or source image from your local machine |
| target_image_location | string | required | URL, target image in BLOB format or target image from your local machine |
| options | string | optional | Object that defines detection options |

Supported options:
Expand Down
42 changes: 42 additions & 0 deletions endpoints/common_endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import axios from 'axios';
import FormData from 'form-data';

// Collection of common endpoints that used by almost all services
const common_endpoints = {
async upload_blob(blobData, url, api_key){
var bodyFormData = new FormData();
bodyFormData.append('file', blobData, 'example.jpg');

return new Promise( async (resolve, reject) => {
try {
const response = await axios.post( url, bodyFormData, {
headers: {
'Content-Type': 'multipart/form-data',
"x-api-key": api_key
},
})

resolve(response)
} catch (error) {
reject(error)
}
})
}
}

export { common_endpoints }
208 changes: 158 additions & 50 deletions endpoints/verification_endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,61 +100,169 @@ const verification_endpoints = {
*/
one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, api_key ){
var bodyFormData = new FormData();
let path_is_url = [];
let path_is_relative = [];

if(isSourceImageUrl){
bodyFormData.append('target_image', fs.createReadStream(target_image_path), { knownLength: fs.statSync(target_image_path).size });

return new Promise( async (resolve, reject) => {
await axios.get(source_image_path, { responseType: 'stream' })
.then( async (response) => {
let image_extention = response.headers['content-type'].split("/")[1]
bodyFormData.append('source_image', response.data, `example.${image_extention}`);

try {
const res = await axios.post( url, bodyFormData, {
headers: {
...bodyFormData.getHeaders(),
"x-api-key": api_key
},
})

resolve(res)
} catch (error) {
reject(error)
}
})
.catch(error => {
path_is_url[0] = "source_image";
path_is_url[1] = source_image_path;

path_is_relative[0] = "target_image";
path_is_relative[1] = target_image_path;
}else{
path_is_url = "target_image";
path_is_url[1] = target_image_path;

path_is_relative = "source_image";
path_is_relative[1] = source_image_path;
}

bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });

return new Promise( async (resolve, reject) => {
await axios.get(path_is_url[1], { responseType: 'stream' })
.then( async (response) => {
let image_extention = response.headers['content-type'].split("/")[1]
bodyFormData.append(path_is_url[0], response.data, `example.${image_extention}`);

try {
const res = await axios.post( url, bodyFormData, {
headers: {
...bodyFormData.getHeaders(),
"x-api-key": api_key
},
})

resolve(res)
} catch (error) {
reject(error)
})
})
}else {
bodyFormData.append('source_image', fs.createReadStream(source_image_path), { knownLength: fs.statSync(source_image_path).size });

return new Promise( async (resolve, reject) => {
await axios.get(target_image_path, { responseType: 'stream' })
.then( async (response) => {
let image_extention = response.headers['content-type'].split("/")[1]
bodyFormData.append('target_image', response.data, `example.${image_extention}`);

try {
const res = await axios.post( url, bodyFormData, {
headers: {
...bodyFormData.getHeaders(),
"x-api-key": api_key
},
})

resolve(res)
} catch (error) {
reject(error)
}
})
.catch(error => {
}
})
.catch(error => {
reject(error)
})
})
},
/**
* Verify face(s) from given blob data
* @param {String} source_image_path
* @param {String} target_image_path
* @param {Boolean} isSourceBlob
* @param {String} url
* @param {String} api_key
* @returns {Promise}
*/
url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, api_key){
let bodyFormData = new FormData();
let path_is_url = [];
let path_is_blob = [];

if(isSourceImageUrl){
path_is_url[0] = "source_image";
path_is_url[1] = source_image_path;

path_is_blob[0] = "target_image";
path_is_blob[1] = target_image_path;
}else{
path_is_url = "target_image";
path_is_url[1] = target_image_path;

path_is_blob = "source_image";
path_is_blob[1] = source_image_path;
}
bodyFormData.append(path_is_blob[0], path_is_blob[1], 'example.jpg');

return new Promise( async (resolve, reject) => {
await axios.get(path_is_url[1], { responseType: 'stream' })
.then( async (response) => {
let image_extention = response.headers['content-type'].split("/")[1]
bodyFormData.append(path_is_url[0], response.data, `example.${image_extention}`);

try {
const res = await axios.post( url, bodyFormData, {
headers: {
...bodyFormData.getHeaders(),
"x-api-key": api_key
},
})

resolve(res)
} catch (error) {
reject(error)
})
})
}
})
.catch(error => {
reject(error)
})
})
},

/**
* Both source and target images are blob
* @param {Blob} source_image_blob
* @param {Blob} target_image_blob
* @param {String} url
* @param {String} api_key
*/
both_blob_request(source_image_blob, target_image_blob, url, api_key){
var bodyFormData = new FormData();

bodyFormData.append('source_image', source_image_blob, 'example.jpg');
bodyFormData.append('target_image', target_image_blob, 'example1.jpg');

return new Promise( async (resolve, reject) => {
try {
const response = await axios.post( url, bodyFormData, {
headers: {
'Content-Type': 'multipart/form-data',
"x-api-key": api_key
},
})

resolve(response)
} catch (error) {
reject(error)
}
})
},

one_blob_request(source_image_path, isSourceImageBlob, target_image_path, url, api_key ){
var bodyFormData = new FormData();
let path_is_blob = [];
let path_is_relative = [];

if(isSourceImageBlob){
path_is_blob[0] = "source_image";
path_is_blob[1] = source_image_path;

path_is_relative[0] = "target_image";
path_is_relative[1] = target_image_path;
}else{
path_is_blob = "target_image";
path_is_blob[1] = target_image_path;

path_is_relative = "source_image";
path_is_relative[1] = source_image_path;
}
}

bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });
bodyFormData.append(path_is_blob[0], path_is_blob[1], 'example.jpg');

return new Promise( async (resolve, reject) => {
try {
const response = await axios.post( url, bodyFormData, {
headers: {
'Content-Type': 'multipart/form-data',
"x-api-key": api_key
},
})

resolve(response)
} catch (error) {
reject(error)
}
})
},


}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exadel/compreface-js-sdk",
"version": "0.5.0",
"version": "0.5.1",
"license": "Apache-2.0",
"description": "JavaScript SDK for CompreFace - free and open-source face recognition system from Exadel",
"main": "index.js",
Expand Down
9 changes: 9 additions & 0 deletions services/detection_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { detection_endpoints } from '../endpoints/detection_endpoints.js';
import { recognition_endpoints } from '../endpoints/recognition_endpoints.js';
import { common_endpoints } from '../endpoints/common_endpoints.js';
import { common_functions } from '../functions/index.js';

class DetectionService {
Expand Down Expand Up @@ -59,6 +60,14 @@ class DetectionService {
.catch(error => {
reject(error)
})
}else if(image_path instanceof Blob) {
common_endpoints.upload_blob(image_path, url, this.key)
.then(response => {
resolve(response.data)
})
.catch(error => {
reject(error)
})
}else {
detection_endpoints.detect_request(image_path, url, this.key)
.then(response => {
Expand Down
Loading

0 comments on commit 4abc5f5

Please sign in to comment.