-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionality.js
98 lines (77 loc) · 2.34 KB
/
functionality.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
$( document ).ready(function () {
"use strict";
var $upload = $('#upload');
var $receipt = $('#receipt');
var $progress = $('#progress');
var $food = $('#food');
var $loading = $('#loading');
var expirations = {
"grape-tomatoes": 10,
"strawberries": 3,
"green-grapes": 7,
"red-delic-apples": 7,
"broccoli-crowns": 14,
"cauliflower": 21,
"iceburg-lettuce": 10,
"cucumbers": 10,
"banana": 5,
"restaur-tortilla": 7,
"pretzelsticks/mini": 14
};
function getExpiration( food ) {
var key = food.toLowerCase().replace( /\s/g, '-' );
var expiration = expirations[ key ];
console.log( key );
if ( typeof expiration == 'undefined' || expiration <= 0 ) {
expiration = 'Not enough data';
} else {
expiration = expiration.toString() + ' day(s)';
}
return expiration;
}
function ocr() {
Tesseract.recognize( $receipt[0] )
.progress( function ( message ) { console.log( message ); } )
.catch( function ( err ) { console.error( err ); } )
.then( function ( result ) { console.log( result ); } )
.finally( function ( resultOrError ) {
console.log( resultOrError );
$loading.attr( 'hidden', '' );
$receipt.attr( 'hidden', '' );
var regex = /([^\n]+)\s+[0-9]+\.[0-9]{2}\sF3/g;
var array = resultOrError.text.match( regex );
var html = '';
console.log( 'array', array );
array.forEach( function ( element ) {
var foodName = element.replace( regex, '$1' ).replace( /\sWE/, '' );
html += '<li><span class="food-name">'
+ foodName
+ '</span>'
+ '<span class="food-expiration">'
+ getExpiration( foodName )
+ '</span></li>'
;
} );
$food.html( html );
} )
;
}
function readURL( input ) {
// console.log( 'readURL' );
if ( input.files && input.files[0] ) {
var reader = new FileReader();
reader.onload = function ( event ) {
$receipt.attr( 'src', event.target.result );
$receipt.on( 'load', function ( event ) {
ocr();
} );
}
reader.readAsDataURL( input.files[0] );
}
}
$upload.on( 'change', function () {
$loading.attr( 'hidden', null );
$receipt.attr( 'hidden', null );
readURL( this );
} );
} );