-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
102 lines (90 loc) · 2.86 KB
/
main.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
// Copyright 2015, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var CV_URL = 'https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBW_ZrfNA2qLQ29MxLzM8U8_WGU1Q2IT8w';
$(function () {
$('#fileform').on('submit', uploadFiles);
});
/**
* 'submit' event handler - reads the image bytes and sends it to the Cloud
* Vision API.
*/
function uploadFiles (event) {
event.preventDefault(); // Prevent the default form post
// Grab the file and asynchronously convert to base64.
var file = $('#fileform [name=fileField]')[0].files[0];
var reader = new FileReader();
reader.onloadend = processFile;
reader.readAsDataURL(file);
}
/**
* Event handler for a file's data url - extract the image data and pass it off.
*/
// function processFile (event) {
// var content = event.target.result;
// sendFileToCloudVision(content.replace('data:image/jpeg;base64,', ''));
// }
function processFile (content) {
// console.log(content);
sendFileToCloudVision(content.replace('data:image/png;base64,', ''));
}
/**
* Sends the given file contents to the Cloud Vision API and outputs the
* results.
*/
function sendFileToCloudVision (content) {
// var type = $('#fileform [name=type]').val();
var type = "LABEL_DETECTION";
// Strip out the file prefix when you convert to json.
var request = {
requests: [{
image: {
content: content
},
features: [{
type: type,
maxResults: 200
}]
}]
};
$('#results').text('Loading...');
$.post({
url: CV_URL,
data: JSON.stringify(request),
contentType: 'application/json'
}).fail(function (jqXHR, textStatus, errorThrown) {
$('#results').text('ERRORS: ' + textStatus + ' ' + errorThrown);
}).done(displayJSON);
}
/**
* Displays the results.
*/
function displayJSON (data) {
// console.log("end mei to aa gaya");
var contents = JSON.stringify(data, null, 4);
//$('#results').text(contents);
//var evt = new Event('results-displayed');
//evt.results = contents;
//document.dispatchEvent(evt);
var a = JSON.parse(contents);
a.responses[0].labelAnnotations.forEach(function(response){
if(response.score>0.7){
console.log(response.description);
isko_bolo(response.description);
}
});
}
function isko_bolo(line){
var msg = new SpeechSynthesisUtterance(line);
window.speechSynthesis.speak(msg);
}