This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision-for-snap.js
140 lines (140 loc) · 6.16 KB
/
vision-for-snap.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
var video = document.createElement('video');
var canvas = document.createElement('canvas');
var get_url_parameter = function (name, default_value) {
var parts = window.location.search.substr(1).split('&');
var value = default_value;
parts.some(function (part) {
var name_and_value = part.split('=');
if (name_and_value[0] === name) {
value = name_and_value[1];
return true;
}
});
return value;
};
var key = get_url_parameter("key");
if (!key) {
alert("No key provided in URL query");
}
var provider = get_url_parameter("provider", "Watson");
var startup = function startup() {
var callback = function(stream) {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL.createObjectURL(stream);
video.play();
};
var error_callback = function(err) {
console.log("An error occured! " + err);
};
var constraints = {video: true,
audio: false};
video.style.display = 'none';
canvas.style.display = 'none';
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
document.body.appendChild(video);
document.body.appendChild(canvas);
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia(constraints)
.then(callback)
.catch(error_callback);
} else {
navigator.getMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.msGetUserMedia);
navigator.mediaDevices.getUserMedia(constraints, callback, error_callback);
}
};
// Capture a photo by fetching the current contents of the video
// and drawing it into a canvas, then converting that to a PNG
// format data URL. By drawing it on an offscreen canvas and then
// drawing that to the screen, we can change its size and/or apply
// other changes before drawing it.
// available for external use by attaching it to window
window.take_picture_and_analyse = function take_picture_and_analyse(show_photo, callback) {
var context, photo;
if (!callback) {
callback = console.log;
}
context = canvas.getContext('2d');
context.drawImage(video, 0, 0, width, height);
if (show_photo) {
photo = document.createElement('img');
photo.src = canvas.toDataURL('image/png');
photo.setAttribute('width', width);
photo.setAttribute('height', height);
document.getElementById("world").style.display = "none";
document.body.appendChild(photo);
document.body.title = "Click me to restore Snap!";
video.style.display = ''; // display video
document.body.addEventListener('click',
function () {
document.getElementById("world").style.display = '';
video.style.display = 'none';
if (photo.parentElement) {
document.body.removeChild(photo);
}
document.body.title = "";
});
switch (provider) {
case "Watson":
canvas.toBlob(function (blob) {
post_image(blob,
function (event) {
callback(event.currentTarget.response)
});
},
"image/png");
break;
case "Google":
post_image(canvas.toDataURL('image/png'),
function (event) {
callback(event.currentTarget.response);
});
break;
}
}
}
var post_image = function post_image(image, callback, error_callback) {
// base upon https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_forms_through_JavaScript
var XHR = new XMLHttpRequest();
var formData;
XHR.addEventListener('load', function(event) {
callback(event);
});
if (!error_callback) {
error_callback = function (event) {
console.error(event);
}
}
XHR.addEventListener('error', error_callback);
switch (provider) {
case "Watson":
formData = new FormData();
formData.append("images_file", image, "blob.png");
XHR.open('POST', "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-19&api_key=" + key);
XHR.send(formData);
break;
case "Google":
XHR.open('POST', "https://vision.googleapis.com/v1/images:annotate?key=" + key);
XHR.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
XHR.send(JSON.stringify({"requests":[{"image":{"content": image.substring("data:image/png;base64,".length)},
"features":[{"type":"LABEL_DETECTION",
"maxResults":1},
{"type": "TEXT_DETECTION",
"maxResults":3},
{"type": "FACE_DETECTION",
"maxResults":1},
{"type": "IMAGE_PROPERTIES",
"maxResults":2}]}]
}));
break;
}
};
if (document.body) {
startup();
} else {
window.addEventListener('load', startup, false);
}