-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (47 loc) · 1.67 KB
/
script.js
File metadata and controls
59 lines (47 loc) · 1.67 KB
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
const video = document.querySelector("video")
const textElem = document.querySelector("[data-text]")
async function setup() {
const stream = await navigator.mediaDevices.getUserMedia({ video: true })
video.srcObject = stream
video.addEventListener("playing", async () => {
const worker = Tesseract.createWorker()
await worker.load()
await worker.loadLanguage("eng")
await worker.initialize("eng")
const canvas = document.createElement("canvas")
canvas.width = video.width
canvas.height = video.height
document.addEventListener("touchstart", async e => {
if (e.code == "Space") return
canvas.getContext("2d").drawImage(video, 0, 0, video.width, video.height)
const {
data: { text },
} = await worker.recognize(canvas)
speechSynthesis.speak(
new SpeechSynthesisUtterance(text.replace(/\s/g, " "))
)
textElem.textContent = text
})
})
video.addEventListener("playing", async () => {
const worker = Tesseract.createWorker()
await worker.load()
await worker.loadLanguage("eng")
await worker.initialize("eng")
const canvas = document.createElement("canvas")
canvas.width = video.width
canvas.height = video.height
document.addEventListener("keypress", async e => {
if (e.code == "Space") return
canvas.getContext("2d").drawImage(video, 0, 0, video.width, video.height)
const {
data: { text },
} = await worker.recognize(canvas)
speechSynthesis.speak(
new SpeechSynthesisUtterance(text.replace(/\s/g, " "))
)
textElem.textContent = text
})
})
}
setup()