Skip to content

Commit 7e31633

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

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

core/compre_face.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import { RecognitionService } from '../services/recognition_service.js';
1717
import { VerificationService } from '../services/verification_service.js';
18+
import { DetectionService } from '../services/detection_service.js';
1819
// main class
1920
class CompreFace {
2021
constructor(server, port, options){
@@ -37,10 +38,20 @@ class CompreFace {
3738
* @param {String} api_key
3839
* @returns {Object}
3940
*/
40-
initFaceVerificationService(api_key){
41+
initFaceVerificationService(api_key){
4142
let verification_object = new VerificationService(this.server, this.port, this.options, api_key)
4243
return verification_object;
4344
}
45+
46+
/**
47+
* Initialize DetectionService instance
48+
* @param {String} api_key
49+
* @returns {Object}
50+
*/
51+
initFaceDetectionService(api_key){
52+
let detection_object = new DetectionService(this.server, this.port, this.options, api_key)
53+
return detection_object;
54+
}
4455
}
4556

4657
export { CompreFace };

endpoints/detection_endpoints.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 fs from 'fs';
18+
import FormData from 'form-data';
19+
20+
const detection_endpoints = {
21+
/**
22+
* Detect faces from given image
23+
* @param {String} image_path
24+
* @param {String} api_key
25+
* @returns {Promise}
26+
*/
27+
async detect_request(image_path, url, api_key){
28+
var bodyFormData = new FormData();
29+
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
30+
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+
export { detection_endpoints }
50+
51+
52+
53+
54+

services/detection_service.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
import { detection_endpoints } from '../endpoints/detection_endpoints.js';
18+
19+
class DetectionService {
20+
constructor(server, port, options, key){
21+
this.server = server;
22+
this.port = port;
23+
this.options = options;
24+
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/detection/detect';
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;
76+
}
77+
78+
detect(image_path, options){
79+
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
80+
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
81+
let required_url_parameters = {
82+
limit: true,
83+
det_prob_threshold: true,
84+
prediction_count: true,
85+
face_plugins: true,
86+
status: true
87+
};
88+
// add parameters to basic url
89+
let url = this.add_options_to_url(this.get_full_url(), options, required_url_parameters);
90+
91+
return new Promise((resolve, reject) => {
92+
detection_endpoints.detect_request(image_path, url, this.key)
93+
.then(response => {
94+
resolve(response.data)
95+
})
96+
.catch(error => {
97+
reject(error.response.data)
98+
})
99+
})
100+
}
101+
}
102+
103+
export { DetectionService }

0 commit comments

Comments
 (0)