Skip to content

Commit 71d2268

Browse files
author
doripjonov
committed
added detection service
1 parent 7e31633 commit 71d2268

File tree

4 files changed

+97
-160
lines changed

4 files changed

+97
-160
lines changed

functions/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
17+
// Collection of common functions that used by almost all services
18+
const common_functions = {
19+
/**
20+
* Construct full url from given parameters
21+
* @returns {String}
22+
*/
23+
get_full_url(base_url, server, port) {
24+
return `${server}:${port}/${base_url}`;
25+
},
26+
27+
/**
28+
* Add extra options to url
29+
* @param {String} url
30+
* @param {Object} globalOptions
31+
* @param {Object} localOptions
32+
* @param {Object} required_parameters
33+
* @returns {String}
34+
*/
35+
add_options_to_url(url, globalOptions, localOptions, required_parameters){
36+
// merge options passed by localy and globally NOTE: global options will override local on if same value passed from both of them
37+
let uniqueOptions = {...localOptions, globalOptions};
38+
let isThereAnyOptions = Object.keys(uniqueOptions);
39+
40+
// check whether any parameters passed
41+
if(isThereAnyOptions.length > 0){
42+
// check whether limit parameter passed and it is required for particular endpoint (ex: it is not requrid for add())
43+
if(uniqueOptions['limit'] >= 0 && required_parameters['limit']){
44+
url = `${url}?limit=${uniqueOptions['limit']}`
45+
}
46+
47+
// check whether det_prob_threshold parameter passed and is it required for particular endpoint
48+
if(uniqueOptions['det_prob_threshold'] >= 0 && required_parameters['det_prob_threshold']){
49+
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
50+
}
51+
52+
// check whether prediction_count passed and is it required for particular endpoint
53+
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
54+
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
55+
}
56+
57+
// check whether face_plugins passed and is it required for particular endpoint
58+
if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
59+
url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
60+
}
61+
62+
// check whether status passed and is it required for particular endpoint
63+
if(uniqueOptions['status'] && required_parameters['status']){
64+
url = `${url}&status=${uniqueOptions['status']}`
65+
}
66+
}
67+
68+
return url;
69+
}
70+
}
71+
72+
export { common_functions }

services/detection_service.js

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,25 @@
1515
*/
1616

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

1920
class DetectionService {
2021
constructor(server, port, options, key){
2122
this.server = server;
2223
this.port = port;
2324
this.options = options;
2425
this.key = key;
26+
this.base_url = 'api/v1/detection/detect';
2527
}
2628

2729
/**
28-
* Construct full url from given server and port number
29-
* @returns {String}
30-
*/
31-
get_full_url(){
32-
let destination = 'api/v1/detection/detect';
33-
let full_url = `${this.server}:${this.port}/${destination}`;
34-
35-
return full_url;
36-
}
37-
/**
38-
* Add extra options to url
30+
* Detect faces from given image
31+
* @param {String} image_path
3932
* @param {Object} options
40-
* @returns {String}
33+
* @returns
4134
*/
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
44-
let uniqueOptions = {...localOptions, ...this.options};
45-
let isThereAnyOptions = Object.keys(uniqueOptions);
46-
47-
// check whether any parameters passed
48-
if(isThereAnyOptions.length > 0){
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']){
51-
url = `${url}?limit=${uniqueOptions['limit']}`
52-
}
53-
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']){
56-
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
57-
}
58-
59-
// check whether prediction_count passed and is it required for particular endpoint
60-
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
61-
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
62-
}
63-
64-
// check whether face_plugins passed and is it required for particular endpoint
65-
if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
66-
url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
67-
}
68-
69-
// check whether status passed and is it required for particular endpoint
70-
if(uniqueOptions['status'] && required_parameters['status']){
71-
url = `${url}&status=${uniqueOptions['status']}`
72-
}
73-
}
74-
75-
return url;
76-
}
77-
78-
detect(image_path, options){
35+
detect(image_path, localOptions){
36+
const { get_full_url, add_options_to_url } = common_functions;
7937
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
8038
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
8139
let required_url_parameters = {
@@ -85,8 +43,9 @@ class DetectionService {
8543
face_plugins: true,
8644
status: true
8745
};
46+
let full_url = get_full_url(this.base_url, this.server, this.port)
8847
// add parameters to basic url
89-
let url = this.add_options_to_url(this.get_full_url(), options, required_url_parameters);
48+
let url = add_options_to_url(full_url, this.options, localOptions, required_url_parameters);
9049

9150
return new Promise((resolve, reject) => {
9251
detection_endpoints.detect_request(image_path, url, this.key)

services/recognition_service.js

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,16 @@
1414
* permissions and limitations under the License.
1515
*/
1616
import { recognition_endpoints } from '../endpoints/recognition_endpoints.js';
17+
import { common_functions } from '../functions/index.js';
1718

1819
class RecognitionService {
1920
constructor(server, port, options, key){
2021
this.server = server;
2122
this.port = port;
2223
this.options = options;
2324
this.key = key;
24-
}
25-
26-
/**
27-
* Construct full url from given server and port number
28-
* @returns {String}
29-
*/
30-
get_full_url(isUrlForRecognition){
31-
let destination = isUrlForRecognition ? 'api/v1/recognition' : 'api/v1/recognition/faces';
32-
let full_url = `${this.server}:${this.port}/${destination}`;
33-
34-
return full_url;
35-
}
36-
37-
/**
38-
* Add extra options to url
39-
* @param {Object} options
40-
* @returns {String}
41-
*/
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
44-
let uniqueOptions = {...localOptions, ...this.options};
45-
let isThereAnyOptions = Object.keys(uniqueOptions);
46-
47-
// check whether any parameters passed
48-
if(isThereAnyOptions.length > 0){
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']){
51-
url = `${url}?limit=${uniqueOptions['limit']}`
52-
}
53-
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']){
56-
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
57-
}
58-
59-
// check whether prediction_count passed and is it required for particular endpoint
60-
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
61-
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
62-
}
63-
64-
// check whether face_plugins passed and is it required for particular endpoint
65-
if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
66-
url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
67-
}
68-
69-
// check whether status passed and is it required for particular endpoint
70-
if(uniqueOptions['status'] && required_parameters['status']){
71-
url = `${url}&status=${uniqueOptions['status']}`
72-
}
73-
}
74-
75-
return url;
25+
this.base_url = 'api/v1/recognition/faces';
26+
this.recognize_base_url = "api/v1/recognition/recognize";
7627
}
7728

7829
/**
@@ -81,6 +32,7 @@ class RecognitionService {
8132
* @returns {Promise}
8233
*/
8334
recognize(image_path, options){
35+
const{ get_full_url, add_options_to_url } = common_functions;
8436
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
8537
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
8638
let required_url_parameters = {
@@ -92,9 +44,8 @@ class RecognitionService {
9244
};
9345

9446
// add parameters to basic url
95-
let url = this.get_full_url(true);
96-
url = `${url}/recognize`;
97-
url = this.add_options_to_url(url, options, required_url_parameters);
47+
let full_url = get_full_url(this.recognize_base_url, this.server, this.port)
48+
let url = add_options_to_url(full_url, this.options, options, required_url_parameters);
9849

9950
return new Promise((resolve, reject) => {
10051
recognition_endpoints.recognize_face_request(image_path, url, this.key)
@@ -112,7 +63,8 @@ class RecognitionService {
11263
* @returns {Object}
11364
*/
11465
getFaceCollection(){
115-
let url = this.get_full_url();
66+
const{ get_full_url, add_options_to_url } = common_functions;
67+
let url = get_full_url(this.base_url, this.server, this.port)
11668
let key = this.key;
11769
let that = this;
11870

@@ -150,7 +102,7 @@ class RecognitionService {
150102

151103
// add parameters to basic url
152104
url = `${url}?subject=${subject}`
153-
url = that.add_options_to_url(url, options, required_url_parameters);
105+
url = add_options_to_url(url, that.options, options, required_url_parameters);
154106

155107
return new Promise((resolve, reject) => {
156108
if(isUrl){
@@ -192,7 +144,7 @@ class RecognitionService {
192144
};
193145
// add parameters to basic url
194146
url = `${url}/${image_id}/verify`;
195-
url = that.add_options_to_url(url, options, required_url_parameters);
147+
url = add_options_to_url(url, that.options, options, required_url_parameters);
196148

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

services/verification_service.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,19 @@
1515
*/
1616

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

1920
class VerificationService {
2021
constructor(server, port, options, key){
2122
this.server = server;
2223
this.port = port;
2324
this.options = options;
2425
this.key = key;
25-
}
26-
27-
/**
28-
* Construct full url from given server and port number
29-
* @returns {String}
30-
*/
31-
get_full_url(){
32-
let destination = 'api/v1/verification/verify';
33-
let full_url = `${this.server}:${this.port}/${destination}`;
34-
35-
return full_url;
36-
}
37-
/**
38-
* Add extra options to url
39-
* @param {Object} options
40-
* @returns {String}
41-
*/
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
44-
let uniqueOptions = {...localOptions, ...this.options};
45-
let isThereAnyOptions = Object.keys(uniqueOptions);
46-
47-
// check whether any parameters passed
48-
if(isThereAnyOptions.length > 0){
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']){
51-
url = `${url}?limit=${uniqueOptions['limit']}`
52-
}
53-
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']){
56-
url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
57-
}
58-
59-
// check whether prediction_count passed and is it required for particular endpoint
60-
if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
61-
url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
62-
}
63-
64-
// check whether face_plugins passed and is it required for particular endpoint
65-
if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
66-
url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
67-
}
68-
69-
// check whether status passed and is it required for particular endpoint
70-
if(uniqueOptions['status'] && required_parameters['status']){
71-
url = `${url}&status=${uniqueOptions['status']}`
72-
}
73-
}
74-
75-
return url;
26+
this.base_url = 'api/v1/verification/verify';
7627
}
7728

7829
verify(source_image_path, target_image_path, options){
30+
const { get_full_url, add_options_to_url } = common_functions;
7931
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
8032
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
8133
let required_url_parameters = {
@@ -85,8 +37,10 @@ class VerificationService {
8537
face_plugins: true,
8638
status: true
8739
};
40+
41+
let full_url = get_full_url(this.base_url, this.server, this.port)
8842
// add parameters to basic url
89-
let url = this.add_options_to_url(this.get_full_url(), options, required_url_parameters);
43+
let url = add_options_to_url(full_url, this.options, options, required_url_parameters);
9044

9145
return new Promise((resolve, reject) => {
9246
verification_endpoints.verify_face_request(source_image_path, target_image_path, url, this.key)

0 commit comments

Comments
 (0)