forked from cassidoo/found
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain-people.js
190 lines (168 loc) · 4.5 KB
/
train-people.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var clarifai;
var contact = 'contact';
var email = 'example@example.com';
var nombre = 'example';
$(document).ready(function() {
clarifai = new Clarifai({
'clientId': CLARIFAI_KEY,
'clientSecret': CLARIFAI_SECRET
});
$('#file-input').change(function(e) {
var file = e.target.files[0],
imageType = /image.*/;
if (!file.type.match(imageType))
return;
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var $img = $('<img>', { src: e.target.result });
var canvas = $('#canvas')[0];
var context = canvas.getContext('2d');
$img.load(function() {
context.drawImage(this, 0, 0);
});
}
});
function tutorial() {
swal({
title: 'How this works',
text: 'We know how hard it can be to lose a loved one in a crisis. We are here to help. <br> First, <span style="color:#F8BB86">Type in the name</span> of your loved one. Then, upload a photo of them, and hit, <span style="color:#F8BB86">Add Loved One</span>. You can repeat this as many times as you would like. <br> If you have found someone, add their name to the form, and hit, <span style="color:#F8BB86">Found Loved One</span>. If they are in the system, we will send an email indicating to their loved ones that they have been found!',
html: true
});
}
function nameSubmit() {
nombre = $("#name").val();
}
function emailSubmit() {
email = $("#email").val();
contact = $("#contactname").val();
}
function positive(imgurl) {
clarifai.positive(imgurl, nombre, callback).then(
promiseResolved,
promiseRejected
);
}
function negative(imgurl) {
clarifai.negative(imgurl, nombre, callback).then(
promiseResolved,
promiseRejected
);
}
function train() {
clarifai.train(nombre, callback).then(
promiseResolved,
promiseRejected
);
}
function posTrain(imgurl) {
clarifai.positive(imgurl, nombre, callback)
.then(function() {
train();
});
}
function predict(imgurl) {
clarifai.predict(imgurl, nombre, callback)
.then(function(obj) {
if (obj.score < 0.45) {
swal({
title: 'We are sorry.',
text: 'That person has not been found yet.',
imageUrl: obj.url
});
} else {
swal({
title: 'This person, '+ nombre +', has been found!',
text: 'Thank you so much for your efforts.'
});
var message = 'Hi '+contact+'! We have exciting news for you. It seems that we located your loved one, '+nombre+'!';
$.ajax({
type: "POST",
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': EMAIL_SECRET,
'message': {
'from_email': APP_EMAIL,
'to': [
{
'email': email,
'name': contact,
'type': 'to'
},
],
'autotext': 'true',
'subject': 'Your loved one has been found!',
'html': message
}
}
}).done(function(response) {
console.log(response);
});
}
},
promiseRejected
);
}
function promiseResolved(obj){
console.log('Promise resolved', obj);
}
function promiseRejected(obj){
console.log('Promise rejected', obj);
}
function callback(obj){
console.log('callback', obj);
}
function trainPerson() {
var img;
try {
img = document.getElementById('canvas').toDataURL('image/jpeg', 0.9).split(',')[1];
} catch(e) {
img = document.getElementById('canvas').toDataURL().split(',')[1];
}
var imageURL = '';
$.ajax({
url: 'https://api.imgur.com/3/image',
type: 'post',
headers: {
Authorization: 'Client-ID ' + IMGUR_KEY
},
data: {
image: img
},
dataType: 'json',
success: function(response) {
if(response.success) {
imageURL = response.data.link;
posTrain(imageURL);
}
}
});
}
function testPerson() {
var img;
try {
img = document.getElementById('canvas').toDataURL('image/jpeg', 0.9).split(',')[1];
} catch(e) {
img = document.getElementById('canvas').toDataURL().split(',')[1];
}
var imageURL = '';
$.ajax({
url: 'https://api.imgur.com/3/image',
type: 'post',
headers: {
Authorization: 'Client-ID ' + IMGUR_KEY
},
data: {
image: img
},
dataType: 'json',
success: function(response) {
if(response.success) {
imageURL = response.data.link;
predict(imageURL);
}
}
});
}