-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.html
69 lines (66 loc) · 2.13 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tesseract.js Video Streaming Recognition</title>
<link rel="stylesheet" href="./css/style.css">
<script src='https://unpkg.com/tesseract.js@v2.0.0-beta.1/dist/tesseract.min.js'></script>
</head>
<body>
<div id="root">
<video id="poem-video" width="640" height="360" crossorigin="anonymous">
<source src="./do-not-go-gentle.mp4" type="video/mp4">
</video>
<div id="sep"></div>
<div id="messages">
</div>
</div>
<script>
const { createWorker, createScheduler } = Tesseract;
const scheduler = createScheduler();
const video = document.getElementById('poem-video');
const messages = document.getElementById('messages');
let timerId = null;
const addMessage = (m, bold) => {
let msg = `<p>${m}</p>`;
if (bold) {
msg = `<p class="bold">${m}</p>`;
}
messages.innerHTML += msg;
messages.scrollTop = messages.scrollHeight;
}
const doOCR = async () => {
const c = document.createElement('canvas');
c.width = 640;
c.height = 360;
c.getContext('2d').drawImage(video, 0, 0, 640, 360);
const start = new Date();
const { data: { text } } = await scheduler.addJob('recognize', c);
const end = new Date()
addMessage(`[${start.getMinutes()}:${start.getSeconds()} - ${end.getMinutes()}:${end.getSeconds()}], ${(end - start) / 1000} s`);
text.split('\n').forEach((line) => {
addMessage(line);
});
};
(async () => {
addMessage('Initializing Tesseract.js');
for (let i = 0; i < 4; i++) {
const worker = createWorker();
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
scheduler.addWorker(worker);
}
addMessage('Initialized Tesseract.js');
video.addEventListener('play', () => {
timerId = setInterval(doOCR, 1000);
});
video.addEventListener('pause', () => {
clearInterval(timerId);
});
addMessage('Now you can play the video. :)');
video.controls = true;
})();
</script>
</body>
</html>