-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (120 loc) · 4.23 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
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
console.log(Tesseract ? '--- ready' : '--- not ready')
const OAUTH_PUBLIC_KEY = ''; // Your OAuth public key - oauth.io
// Constants
const FRAMES_CAM = 100 // apply filter every X ms, default = 100
const FRAMES_OCR = 400 // recognize text from image every Y ms, default = 400
const WIDTH = 640
const HEIGHT = 480
const CropHEIGHT = 120;
// const DIV = 6;
// const X = WIDTH/DIV, Y = HEIGHT/DIV, W = WIDTH - X*2, H = HEIGHT - Y*2;
// Code
var postBody = {
'status': "Default Tweet, You didn't input anything!",
'lat': 49.9028729,
'long': 8.85785939,
}
let video, canvas, ctx, ctx2, ticks = 0, domResult, img1, i=0, coords, twitterOAuth
const constraints = {
audio: false,
video: true,
advanced: [{
facingMode: "environment"
}]
}
const handleSuccess = (stream) => {
console.log(' --- stream success');
getGeolocation();
setTwitter();
domResult = document.querySelector('#ocr').firstChild;
video = document.querySelector('#video')
canvas = document.querySelector('#canvas')
ctx = canvas.getContext('2d')
canvas.width = WIDTH
canvas.height = CropHEIGHT;
window.stream = stream; // make stream available to browser console
video.srcObject = stream
window.requestAnimationFrame(tick)
}
const recognize = () => {
Tesseract.recognize(canvas, {
lang: 'deu',
tessedit_char_whitelist: 'QWERTYUIOPASDFGHJKLZXCVBNM,.qwertyuiopasdfghjklzxcvbnmÄäÖöÜüß?!:)' ,
}).then((result) => {
//console.log(">RESULT", result.text)
postBody.status = result.text;
domResult.innerText = result.text;
})
}
const setTwitter = () => {
OAUTH_PUBLIC_KEY == '' ? alert('Missing Oauth Public Key in main.js') : OAuth.initialize(OAUTH_PUBLIC_KEY);
OAuth.popup('twitter').then(twitter => {
console.log(twitter);
twitterOAuth = twitter;
})
// Shift + P to post setup
window.addEventListener("keydown", event => {
if (event.key == 'p') console.log(postBody);
if (event.key == "P") {
twitterOAuth.post({
//url: "/1.1/statuses/update.json?status=" + encodeURI(postBody)
url: "/1.1/statuses/update.json",
data: postBody,
}).then(tweet => console.log('Posted tweet: "' + tweet.text + '"!'))
.fail(err => console.log("Error: " + JSON.parse(err.responseText).errors[0].message));
}});
}
const getGeolocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
coords = position.coords;
postBody.lat = coords.latitude;
postBody.long = coords.longitude;
});
} else {
console.log(" --- Geolocation is not supported by this browser.");
}
}
const tick = () => {
ticks++
window.requestAnimationFrame(tick)
if (ticks % FRAMES_CAM == 0){
console.log("--- tick");
//var myImage = new Image();
//myImage.src = 'test1.jpg';
//ctx.drawImage(myImage, 0, 0 , W, H, 0, 0, W, H); // from Image File
//ctx.drawImage(video, X, Y, W, H, 0, 0, W, H); // from cropped size video
ctx.drawImage(video, 0, (HEIGHT - CropHEIGHT) / 2, WIDTH, CropHEIGHT, 0, 0, WIDTH, CropHEIGHT);
filter();
}
if (ticks % FRAMES_OCR == 0) {
recognize();
}
}
const filter = (s) => {
var imageData = ctx.getImageData(0, 0, WIDTH, CropHEIGHT);
imageData = Filters.grayscale(imageData);
imageData = Filters.filterImage(Filters.convolute, imageData,
[ 0, -1, 0,
-1, 5, -1,
0, -1, 0 ]
// [-1, -1, -1, // different kind of filter, the center of matrix can 9, 10, 11
// -1, 9, -1,
// -1, -1, -1]
);
if (document.querySelector("#range1").innerText !== document.querySelector("#threshold").value)
document.querySelector("#range1").innerText = document.querySelector("#threshold").value;
Filters.threshold(imageData, document.querySelector("#threshold").value);
ctx.putImageData(imageData, 0, 0);
}
const handleError = (error) => {
console.log('navigator.getUserMedia error: ', error);
}
window.Tesseract = Tesseract.create({
langPath: 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/',
corePath: 'https://cdn.rawgit.com/naptha/tesseract.js-core/0.1.0/index.js',
})
navigator.mediaDevices
.getUserMedia(constraints)
.then(handleSuccess)
.catch(handleError)