Skip to content

Commit e05cb21

Browse files
author
doripjonov
committed
code optimized
1 parent 71d2268 commit e05cb21

File tree

6 files changed

+257
-130
lines changed

6 files changed

+257
-130
lines changed

endpoints/recognition_endpoints.js

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,16 @@ import FormData from 'form-data';
1919

2020
const recognition_endpoints = {
2121
/**
22-
* Recognize face(s) from given image
22+
* Universal function to make request for add, recognize and verify face from face collection
2323
* @param {String} image_path
2424
* @param {String} url
2525
* @param {String} api_key
2626
* @returns {Promise}
2727
*/
28-
async recognize_face_request(image_path, url, api_key ){
28+
async face_request(image_path, url, api_key ){
2929
var bodyFormData = new FormData();
3030
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
31-
return new Promise( async (resolve, reject) => {
32-
try {
33-
const response = await axios.post( url, bodyFormData, {
34-
headers: {
35-
...bodyFormData.getHeaders(),
36-
"Content-Length": bodyFormData.getLengthSync(),
37-
"x-api-key": api_key
38-
},
39-
})
40-
41-
resolve(response)
42-
} catch (error) {
43-
reject(error)
44-
}
45-
})
46-
},
47-
48-
/**
49-
* Verify face(s) from given image
50-
* @param {String} image_path
51-
* @param {String} url
52-
* @param {String} api_key
53-
* @returns {Promise}
54-
*/
55-
async verify_face_request(image_path, url, api_key ){
56-
var bodyFormData = new FormData();
57-
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
58-
59-
return new Promise( async (resolve, reject) => {
60-
try {
61-
const response = await axios.post( url, bodyFormData, {
62-
headers: {
63-
...bodyFormData.getHeaders(),
64-
"Content-Length": bodyFormData.getLengthSync(),
65-
"x-api-key": api_key
66-
},
67-
})
68-
69-
resolve(response)
70-
} catch (error) {
71-
reject(error)
72-
}
73-
74-
})
75-
},
7631

77-
/**
78-
* Add image with subject
79-
* @param {String} image_path
80-
* @param {String} api_key
81-
* @returns {Promise}
82-
*/
83-
async add_request(image_path, url, api_key){
84-
var bodyFormData = new FormData();
85-
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
86-
8732
return new Promise( async (resolve, reject) => {
8833
try {
8934
const response = await axios.post( url, bodyFormData, {
@@ -108,30 +53,30 @@ const recognition_endpoints = {
10853
* @param {String} api_key
10954
* @returns {Promise}
11055
*/
111-
add_with_url_request(image_url, url, api_key){
56+
image_url_request(image_url, url, api_key){
11257
var bodyFormData = new FormData();
11358

11459
return new Promise( async (resolve, reject) => {
11560
await axios.get(image_url, { responseType: 'stream' })
116-
.then( async (response) => {
117-
let image_extention = response.headers['content-type'].split("/")[1]
118-
bodyFormData.append('file', response.data, `example.${image_extention}`);
119-
try {
120-
const res = await axios.post( url, bodyFormData, {
121-
headers: {
122-
...bodyFormData.getHeaders(),
123-
"x-api-key": api_key
124-
},
125-
})
61+
.then( async (response) => {
62+
let image_extention = response.headers['content-type'].split("/")[1]
63+
bodyFormData.append('file', response.data, `example.${image_extention}`);
64+
try {
65+
const res = await axios.post( url, bodyFormData, {
66+
headers: {
67+
...bodyFormData.getHeaders(),
68+
"x-api-key": api_key
69+
},
70+
})
12671

127-
resolve(res)
128-
} catch (error) {
72+
resolve(res)
73+
} catch (error) {
74+
reject(error)
75+
}
76+
})
77+
.catch(error => {
12978
reject(error)
130-
}
131-
})
132-
.catch(error => {
133-
reject(error)
134-
})
79+
})
13580
})
13681
},
13782

endpoints/verification_endpoints.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,116 @@ const verification_endpoints = {
4747
}
4848
})
4949
},
50+
51+
/**
52+
* Verify face(s) from given image urls
53+
* @param {String} source_image_path
54+
* @param {String} target_image_path
55+
* @param {String} url
56+
* @param {String} api_key
57+
* @returns {Promise}
58+
*/
59+
both_url_request( source_image_url, target_image_url, url, api_key){
60+
let bodyFormData = new FormData();
61+
let source_image_request = axios.get(source_image_url, { responseType: "stream" });
62+
let target_image_request = axios.get(target_image_url, { responseType: "stream" });
63+
64+
return new Promise( async (resolve, reject) => {
65+
await axios.all([source_image_request, target_image_request])
66+
.then( axios.spread( async (...responses) => {
67+
let source_image_extention = responses[0].headers['content-type'].split("/")[1];
68+
let target_image_extention = responses[1].headers['content-type'].split("/")[1];
69+
70+
bodyFormData.append('source_image', responses[0].data, `example.${source_image_extention}`);
71+
bodyFormData.append('target_image', responses[1].data, `example1.${target_image_extention}`);
72+
73+
try {
74+
const res = await axios.post( url, bodyFormData, {
75+
headers: {
76+
...bodyFormData.getHeaders(),
77+
"x-api-key": api_key
78+
},
79+
})
80+
81+
resolve(res)
82+
} catch (error) {
83+
reject(error)
84+
}
85+
}))
86+
.catch(error => {
87+
reject(error)
88+
})
89+
})
90+
},
91+
92+
/**
93+
* Verify face(s) from given image urls
94+
* @param {String} source_image_path
95+
* @param {String} target_image_path
96+
* @param {Boolean} isSourceImageUrl
97+
* @param {String} url
98+
* @param {String} api_key
99+
* @returns {Promise}
100+
*/
101+
one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, api_key ){
102+
var bodyFormData = new FormData();
103+
104+
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 => {
127+
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 => {
153+
reject(error)
154+
})
155+
})
156+
}
157+
}
158+
159+
50160
}
51161

52162
export { verification_endpoints }

functions/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ const common_functions = {
2424
return `${server}:${port}/${base_url}`;
2525
},
2626

27+
/**
28+
* Check url
29+
* @param {String} image_url
30+
* @returns {Boolean}
31+
*/
32+
isUrl(image_url){
33+
// regex to check passed parameter is url or relative path
34+
let urlRegEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
35+
let isUrl = urlRegEX.test(image_url);
36+
37+
return isUrl;
38+
},
39+
2740
/**
2841
* Add extra options to url
2942
* @param {String} url
@@ -39,27 +52,27 @@ const common_functions = {
3952

4053
// check whether any parameters passed
4154
if(isThereAnyOptions.length > 0){
42-
// check whether limit parameter passed and it is required for particular endpoint (ex: it is not requrid for add())
55+
// check whether limit parameter passed and it is allowed for particular endpoint (ex: it is not requrid for add())
4356
if(uniqueOptions['limit'] >= 0 && required_parameters['limit']){
4457
url = `${url}?limit=${uniqueOptions['limit']}`
4558
}
4659

47-
// check whether det_prob_threshold parameter passed and is it required for particular endpoint
60+
// check whether det_prob_threshold parameter passed and is it allowed for particular endpoint
4861
if(uniqueOptions['det_prob_threshold'] >= 0 && required_parameters['det_prob_threshold']){
4962
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
5063
}
5164

52-
// check whether prediction_count passed and is it required for particular endpoint
65+
// check whether prediction_count passed and is it allowed for particular endpoint
5366
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
5467
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
5568
}
5669

57-
// check whether face_plugins passed and is it required for particular endpoint
70+
// check whether face_plugins passed and is it allowed for particular endpoint
5871
if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
5972
url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
6073
}
6174

62-
// check whether status passed and is it required for particular endpoint
75+
// check whether status passed and is it allowed for particular endpoint
6376
if(uniqueOptions['status'] && required_parameters['status']){
6477
url = `${url}&status=${uniqueOptions['status']}`
6578
}

services/detection_service.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { detection_endpoints } from '../endpoints/detection_endpoints.js';
18+
import { recognition_endpoints } from '../endpoints/recognition_endpoints.js';
1819
import { common_functions } from '../functions/index.js';
1920

2021
class DetectionService {
@@ -33,28 +34,40 @@ class DetectionService {
3334
* @returns
3435
*/
3536
detect(image_path, localOptions){
36-
const { get_full_url, add_options_to_url } = common_functions;
37+
const { get_full_url, add_options_to_url, isUrl } = common_functions;
3738
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
3839
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
3940
let required_url_parameters = {
4041
limit: true,
4142
det_prob_threshold: true,
42-
prediction_count: true,
4343
face_plugins: true,
4444
status: true
4545
};
4646
let full_url = get_full_url(this.base_url, this.server, this.port)
4747
// add parameters to basic url
4848
let url = add_options_to_url(full_url, this.options, localOptions, required_url_parameters);
49-
49+
50+
// regex to check passed parameter is url or relative path
51+
let validUrl = isUrl(image_path)
52+
5053
return new Promise((resolve, reject) => {
51-
detection_endpoints.detect_request(image_path, url, this.key)
52-
.then(response => {
53-
resolve(response.data)
54-
})
55-
.catch(error => {
56-
reject(error.response.data)
57-
})
54+
if(validUrl){
55+
recognition_endpoints.image_url_request(image_path, url, this.key)
56+
.then(response => {
57+
resolve(response.data)
58+
})
59+
.catch(error => {
60+
reject(error)
61+
})
62+
}else {
63+
detection_endpoints.detect_request(image_path, url, this.key)
64+
.then(response => {
65+
resolve(response.data)
66+
})
67+
.catch(error => {
68+
reject(error)
69+
})
70+
}
5871
})
5972
}
6073
}

0 commit comments

Comments
 (0)