Skip to content

Commit 489c50f

Browse files
authored
Merge pull request #21 from exadel-inc/done_changes
done changes
2 parents 52260bd + 753d9cb commit 489c50f

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules
2-
package-lock.json
2+
package-lock.json
3+
.idea/
4+
*.iml

__tests__/isBase64.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { common_functions } from '../functions/index.js';
2+
3+
test("Check passed value is base64 or not", () => {
4+
let base64 = 'cGFzc3dvcmQ=';
5+
let result = true;
6+
7+
expect(common_functions.isBase64(base64)).toEqual(result);
8+
})

endpoints/upload.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const upload = (image_data, url, api_key) => {
88
let imageFromUrl = isUrl(image_data),
99
imageFromPath = isPathRelative(image_data),
1010
imageFromBase64 = isBase64(image_data);
11-
11+
1212
return new Promise((resolve, reject) => {
1313
if(imageFromUrl){
1414
upload_url(image_data, url, api_key)
@@ -18,16 +18,16 @@ const upload = (image_data, url, api_key) => {
1818
.catch(error => {
1919
reject(error)
2020
})
21-
}else if(imageFromPath){
22-
upload_path(image_data, url, api_key)
21+
}else if(imageFromBase64){
22+
upload_base64(image_data, url, api_key)
2323
.then(response => {
2424
resolve(response.data)
2525
})
2626
.catch(error => {
2727
reject(error)
2828
})
29-
}else if(imageFromBase64){
30-
upload_base64(image_data, url, api_key)
29+
}else if(imageFromPath){
30+
upload_path(image_data, url, api_key)
3131
.then(response => {
3232
resolve(response.data)
3333
})

face_founder_demo/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
.idea/
25+
*.iml

face_founder_demo/src/App.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Container, Row, Form, Col, Button, ProgressBar, Spinner } from 'react-b
55
import UploadedImage from './components/UploadedImge';
66
import DisplayFoundImages from './components/DisplayFoundImages';
77
import DisplayCropedImage from './components/DisplayCropedImages';
8-
import { confirmAlert } from 'react-confirm-alert';
8+
import { confirmAlert } from 'react-confirm-alert';
99
import {NotificationContainer, NotificationManager} from 'react-notifications';
1010
import ShowBadImages from './components/ShowBadImages';
1111
import 'react-confirm-alert/src/react-confirm-alert.css';
@@ -26,19 +26,25 @@ function App() {
2626
const [ detError, setDetError ] = useState([]);
2727
const [ showInvalidFiles, setVisibilityInvalidFiles ] = useState(false);
2828
const [ bigImage, setBigImages ] = useState([]);
29-
const [ loading, setLoading ] = useState(false)
30-
29+
const [ loading, setLoading ] = useState(false);
30+
31+
useEffect(() => {
32+
if(imageFolder !== null) {
33+
findImages();
34+
}
35+
}, [imageFolder]);
36+
3137
const canvasElement = useRef(null);
3238
const imageElement = useRef(null);
3339

3440
let server = "http://localhost";
35-
let port = 8000;
41+
let port = 8000;
3642

3743
let core = new CompreFace(server, port);
38-
44+
3945
let recognitionService = core.initFaceRecognitionService(recognitionKey);
4046
let faceCollection = recognitionService.getFaceCollection();
41-
let detectionService = core.initFaceDetectionService(detectionKey)
47+
let detectionService = core.initFaceDetectionService(detectionKey)
4248

4349
const removeDublicates = (array) =>{
4450
let tempArray = [];
@@ -68,12 +74,12 @@ function App() {
6874
setCounter(0)
6975
setRecError(prevArray => [...new Set(prevArray)])
7076
setVisibilityInvalidFiles(true);
71-
77+
7278
const reader = new FileReader();
7379
reader.onload = myEvent => {
7480
setUploadedImage( myEvent.target.result )
7581
let sendata = myEvent.target.result.split(',')[1];
76-
82+
7783
recognitionService.recognize(sendata, { limit: 10, prediction_count: 100, face_plugins: "age,gender"})
7884
.then(res => {
7985
setLoading(false)
@@ -101,7 +107,7 @@ function App() {
101107
}
102108

103109
const onRateChanges = e => {
104-
let number = e.target.value;
110+
let number = e.target.value;
105111
setFoundFaces([]);
106112
setRate(number);
107113

@@ -113,7 +119,7 @@ function App() {
113119
for(let i = 0; i < Object.values(imageFolder).length; i++ ){
114120
if(subject[0] === imageFolder[i]['name']) setFoundFaces(oldFaces => [...oldFaces, imageFolder[i]])
115121
}
116-
})
122+
})
117123
}
118124

119125
const onChangeImageFolder = event => {
@@ -127,15 +133,15 @@ function App() {
127133
return new Promise((resolve, reject) => {
128134
const reader = new FileReader();
129135
reader.readAsDataURL(file);
130-
136+
131137
reader.onload = () => resolve({ base64: reader.result, fileName: file.name });
132138
reader.onerror = error => reject(error);
133139
});
134140
}
135141

136142
const drawFace = (ctx, aface, index, fileName, lengthOfDetectedFaces, callback, itaration) => {
137143
ctx.drawImage(imageElement.current, aface.box.x_min, aface.box.y_min, aface.box.x_max - aface.box.x_min, aface.box.y_max - aface.box.y_min, 0, 0, aface.box.x_max - aface.box.x_min, aface.box.y_max - aface.box.y_min);
138-
144+
139145
canvasElement.current.toBlob( blob => {
140146
faceCollection.add(blob, fileName)
141147
.then(data => {
@@ -172,37 +178,37 @@ function App() {
172178
}, 'image/jpeg', 0.95)
173179
}
174180

175-
const findImages = e => {
181+
const findImages = e => {
176182
setDetError([]);
177183
setRecError([]);
178184
setVisibilityInvalidFiles(false);
179185

180186
let internalCounter = 0;
181187

182-
const uploadRecursivley = (intarator) => {
188+
const uploadRecursivley = (intarator) => {
183189
// stop processing at the end of file
184190
if(!imageFolder.item(intarator)){
185191
NotificationManager.success("Compeleted!", "Image processing completed!", 3000)
186192
return null;
187193
}
188-
189-
// check size of image
194+
195+
// check size of image
190196
if(imageFolder.item(intarator).size < 5000000){
191197
// get base64 format of image
192198
getBase64(imageFolder.item(intarator))
193199
.then(customData => {
194200
setPersonsImage(customData.base64)
195201
let sendata = customData.base64.split(',')[1];
196202
let ctx = canvasElement.current.getContext('2d');
197-
203+
198204
detectionService.detect(sendata)
199205
.then(res => {
200206
res.result.forEach((aface, index) => {
201207
drawFace(ctx, aface, index, customData.fileName, res.result.length, uploadRecursivley, intarator)
202208
})
203209
})
204-
.catch(error => {
205-
setDetError(prevArray => [...prevArray, customData.fileName])
210+
.catch(error => {
211+
setDetError(prevArray => [...prevArray, customData.fileName])
206212
confirmAlert({
207213
title: 'Detection error',
208214
message: `Face not found in ${customData.fileName}`,
@@ -266,20 +272,15 @@ function App() {
266272
</Form.Group>
267273
</Col>
268274
</Row>
269-
<Row className="marginTop-15">
270-
<Col>
271-
<Button className="btn btn-primary btn-md float-right" disabled = { (imageFolder && recognitionKey && detectionKey) ? false : true } onClick={findImages}>Upload folder</Button>
272-
</Col>
273-
</Row>
274275
</Form>
275276
<Row>
276277
<Col md={3}>
277278
{ uploadedImage && <UploadedImage uploadedImage = { uploadedImage } />}
278279
{ showInvalidFiles && <div>
279280
{ recError.length && <ShowBadImages data={recError} badTitle="Recognition error occured in" />}
280-
{ detError.length && <ShowBadImages data={detError} badTitle="Detection error occured in" />}
281-
{ bigImage.length && <ShowBadImages data={bigImage} badTitle="Oversized images" />}
282-
</div> }
281+
{ detError.length && <ShowBadImages data={detError} badTitle="Detection error occured in" />}
282+
{ bigImage.length && <ShowBadImages data={bigImage} badTitle="Oversized images" />}
283+
</div> }
283284
</Col>
284285
<Col md={9}>
285286
{ loading && <div style={{ margin: "20px auto" }} >
@@ -296,11 +297,11 @@ function App() {
296297
<ProgressBar animated now={ ((counter + 1)/Object.values(imageFolder).length) * 100 } />
297298
{ counter === Object.values(imageFolder).length ? <div>
298299
<span>Done you can select persons image</span>
299-
</div> : <div> <span>Processing images</span> </div>}
300+
</div> : <div> <span>Processing images</span> </div>}
300301
</Col>
301-
</Row>}
302+
</Row>}
302303
<Row> { personsImage && <DisplayCropedImage personsImage = { personsImage } imageElement = { imageElement } canvasElement = { canvasElement } />} </Row>
303-
<NotificationContainer/>
304+
<NotificationContainer/>
304305
</Container>
305306
);
306307
}

functions/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const common_functions = {
2626

2727
/**
2828
* Check url
29-
* @param {String} image_url
29+
* @param {String} image_url
3030
* @returns {Boolean}
3131
*/
3232
isUrl(image_url){
@@ -39,8 +39,8 @@ const common_functions = {
3939

4040
/**
4141
* Check whether string is base64
42-
* @param {String} path
43-
* @returns
42+
* @param {String} path
43+
* @returns
4444
*/
4545
isBase64(image_data){
4646
let base64regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;
@@ -51,21 +51,21 @@ const common_functions = {
5151

5252
/**
5353
* Check whether string is relative path or not
54-
* @param {String} path
55-
* @returns
54+
* @param {String} path
55+
* @returns
5656
*/
5757
isPathRelative(path) {
58-
if(typeof path !== 'string') return false
59-
let isAbsolute = /^(?:\/|[a-z]+:\/\/)/.test(path);
58+
if(typeof path !== 'string') return false;
59+
let isAbsolute = /^([A-Za-z]:|\.)/.test(path);
6060

61-
return !isAbsolute;
61+
return isAbsolute;
6262
},
6363

6464
/**
6565
* Add extra options to url
6666
* @param {String} url
6767
* @param {Object} globalOptions
68-
* @param {Object} localOptions
68+
* @param {Object} localOptions
6969
* @param {Object} required_parameters
7070
* @returns {String}
7171
*/
@@ -74,7 +74,7 @@ const common_functions = {
7474
let uniqueOptions = {...localOptions, ...globalOptions};
7575
let isThereAnyOptions = Object.keys(uniqueOptions);
7676
let isLimitOptionExist = false;
77-
77+
7878
// check whether any parameters passed
7979
if(isThereAnyOptions.length > 0){
8080
// check limit parameter passed and it is allowed for particular endpoint (ex: it is not requrid for add())

0 commit comments

Comments
 (0)