Skip to content

Commit d3126b3

Browse files
author
doripjonov
committed
fixed url parameters function
1 parent 82015dd commit d3126b3

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

endpoints/recognition_endpoints.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const recognition_endpoints = {
7171
async add_request(image_path, url, api_key){
7272
var bodyFormData = new FormData();
7373
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
74-
74+
7575
const response = await axios.post( url, bodyFormData, {
7676
headers: {
7777
...bodyFormData.getHeaders(),
@@ -96,16 +96,20 @@ const recognition_endpoints = {
9696
return new Promise( async (resolve, reject) => {
9797
await axios.get(image_url, { responseType: 'stream' })
9898
.then( async (response) => {
99-
bodyFormData.append('file', response.data, 'exadel.png');
100-
101-
const res = await axios.post( url, bodyFormData, {
102-
headers: {
103-
...bodyFormData.getHeaders(),
104-
"x-api-key": api_key
105-
},
106-
})
107-
108-
return resolve(res)
99+
let image_extention = response.headers['content-type'].split("/")[1]
100+
bodyFormData.append('file', response.data, `example.${image_extention}`);
101+
try {
102+
const res = await axios.post( url, bodyFormData, {
103+
headers: {
104+
...bodyFormData.getHeaders(),
105+
"x-api-key": api_key
106+
},
107+
})
108+
109+
resolve(res)
110+
} catch (error) {
111+
reject(error)
112+
}
109113
})
110114
.catch(error => {
111115
return reject(error)

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,17 @@
1515
*/
1616
import { CompreFace } from './core/compre_face.js';
1717

18-
export { CompreFace }
18+
let server = "http://localhost";
19+
let port = 8000;
20+
let image_url = "https://www.youtube.com/watch?v=Bs65qyxglHg";
21+
let key = "00000000-0000-0000-0000-000000000002";
22+
let image_id = '0ef8edcf-42fa-42ff-b6f0-6c15bd27782d';
23+
let options = { limit: 0, det_prob_threshold: 0, prediction_count: 1 }
24+
25+
let core = new CompreFace(server, port, options);
26+
let recognition_service = core.initFaceRecognitionService(key);
27+
let faceCollection = recognition_service.getFaceCollection();
28+
29+
faceCollection.add(image_url, "Vimeo")
30+
.then(response => console.log(response))
31+
.catch(error => console.log(error))

services/recognition_service.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,25 @@ class RecognitionService {
3939
* @param {Object} options
4040
* @returns {String}
4141
*/
42-
add_options_to_url(url, localOptions, needLimitOption, needDetOption, needPredictionOption){
42+
add_options_to_url(url, localOptions, required_parameters){
43+
// merge options passed by localy and globally NOTE: global options will override local on if same value passed from both of them
4344
let uniqueOptions = {...localOptions, ...this.options};
4445
let isThereAnyOptions = Object.keys(uniqueOptions);
4546

46-
// check whether user passed options by main class
47+
// check whether any parameters passed
4748
if(isThereAnyOptions.length > 0){
48-
if(!isNaN(uniqueOptions['limit']) && needLimitOption){
49+
// check whether limit parameter passed and it is required for particular endpoint (ex: it is not requrid for add())
50+
if(uniqueOptions['limit'] >= 0 && required_parameters['limit']){
4951
url = `${url}?limit=${uniqueOptions['limit']}`
5052
}
5153

52-
if(!isNaN(uniqueOptions['det_prob_threshold']) && needDetOption){
54+
// check whether det_prob_threshold parameter passed and is it required for particular endpoint
55+
if(uniqueOptions['det_prob_threshold'] >= 0 && required_parameters['det_prob_threshold']){
5356
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
5457
}
5558

56-
if(!isNaN(uniqueOptions['prediction_count']) && needPredictionOption){
59+
// check whether prediction_count passed and is it required for particular endpoint
60+
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
5761
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
5862
}
5963
}
@@ -94,12 +98,15 @@ class RecognitionService {
9498
* @returns {Promise}
9599
*/
96100
add(image_path, subject, options){
101+
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
102+
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
103+
let required_url_parameters = { det_prob_threshold: true };
97104
let urlRegEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
98105
let isUrl = urlRegEX.test(image_path);
99106

100107
// add parameters to basic url
101108
url = `${url}?subject=${subject}`
102-
url = that.add_options_to_url(url, options, false, true, false);
109+
url = that.add_options_to_url(url, options, required_url_parameters);
103110

104111
return new Promise((resolve, reject) => {
105112
if(isUrl){
@@ -129,9 +136,12 @@ class RecognitionService {
129136
* @returns {Promise}
130137
*/
131138
recognize(image_path, options){
139+
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
140+
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
141+
let required_url_parameters = {limit: true, det_prob_threshold: true, prediction_count: true };
132142
// add parameters to basic url
133143
url = `${url}/recognize`;
134-
url = that.add_options_to_url(url, options, true, true, true);
144+
url = that.add_options_to_url(url, options, required_url_parameters);
135145

136146
return new Promise((resolve, reject) => {
137147
recognition_endpoints.recognize_face_request(image_path, url, key)
@@ -152,9 +162,12 @@ class RecognitionService {
152162
* @returns {Promise}
153163
*/
154164
verify(image_path, image_id, options){
165+
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
166+
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
167+
let required_url_parameters = { det_prob_threshold: true, prediction_count: true };
155168
// add parameters to basic url
156169
url = `${url}/${image_id}/verify`;
157-
url = that.add_options_to_url(url, options, true, true, false);
170+
url = that.add_options_to_url(url, options, required_url_parameters);
158171

159172
return new Promise((resolve, reject) => {
160173
recognition_endpoints.verify_face_request(image_path, url, key)

0 commit comments

Comments
 (0)