-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
261 lines (203 loc) · 7.24 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<html>
<head>
<title>zutomayo_OCR</title>
<meta charset="utf-8">
<script src="https://unpkg.com/tesseract.js@2.0.0-beta.1/dist/tesseract.min.js"></script>
<meta name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" href="./index.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
const { createWorker } = Tesseract;
const worker = createWorker({
langPath: './',
gzip: false,
});
//最初に読み込むといいらしい?
(async () => {
await worker.load();
await worker.loadLanguage('ztmy');
await worker.initialize('ztmy');
var result = await worker.recognize('./sample.png')
$("#status").text('準備完了')
console.log(result.data.text);
await worker.terminate();
})();
</script>
</head>
<body>
<div align="center">
<!-- あたまのところ -->
<img id="demo_img" src="./sample.png" />
<br><br>
<div id="video-container">
<!-- びでお(実はめっっちゃちっちゃいのが隠れてる) -->
<video id="player" muted autoplay playsinline></video>
<canvas id="canvas" width="300" height="100"></canvas>
</div>
<div>
<button class="button" id="capture"> カメラ認識! </button>
</div>
<br>
<div>
<input type="file" id="file-input">
</div>
<div>
<button class="button" id="ocr-button"> 画像認識! </button>
</div>
<div>
<p>status:<span id="status">準備中</span></p>
</div>
<br>
<!-- けっかのところ -->
<div id="results" align="center"></div>
<canvas id="snapshot" width="300" height="100"></canvas>
<br>
<br>
<br>
<br>
<br>
<script>
var video = document.getElementById("player"),
canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
video.autoplay = true;
video.muted = true;
video.playsInline = true;
canvas.width = 300;
canvas.height = 100;
requestAnimationFrame(draw);
function draw() {
ctx.drawImage(video, 100, 300, 600, 200,
0, 0, 300, 100);
imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
data = imgData.data;
for (let i = 0; i < data.length; i += 4) {
ave = (data[i + 0] + data[i + 1] + data[i + 2]) / 3;
data[i + 0] =
data[i + 1] =
data[i + 2] = (ave > 128) ? 255 : 0;
data[i + 3] = 255;
}
ctx.putImageData(imgData, 0, 0);
requestAnimationFrame(draw);
}
//ビデオとかの設定(俺もよくわからん)
const snapshotCanvas = document.getElementById('snapshot');
const captureButton = document.getElementById('capture');
const handleSuccess = function (stream) {
video.srcObject = stream;
video.play();
};
const handleError = function (error) {
const captureButton = document.getElementById("capture");
captureButton.disabled = true;
const videoContainer = document.getElementById("video-container");
const messages = ["カメラが使えないよ🥹"];
if (error.name === "NotAllowedError") {
messages.push("カメラのアクセスを許可してね");
}
const errorMessage = `
<div class="error-message">
<p>
${messages.join("<br />")}
</p>
</div>
`;
videoContainer.insertAdjacentHTML("beforeend", errorMessage);
console.error('navigator.getUserMedia error: ', error);
};
const constraints = {
audio: false,
video: {
width: 1920,
height: 1080,
facingMode: 'environment'
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(handleSuccess)
.catch(handleError);
var num = 1;
//認識!
captureButton.addEventListener('click', async function () {
$("#status").text('認識中....')
var context = snapshot.getContext('2d');
context.drawImage(video, 100, 300, 600, 200, 0, 0, snapshotCanvas.width,
snapshotCanvas.height);
const url = snapshot.toDataURL("image/png");
const { createWorker } = Tesseract;
const worker = createWorker({
langPath: './',
gzip: false,
});
(async () => {
await worker.load();
await worker.loadLanguage('ztmy');
await worker.initialize('ztmy');
var result = await worker.recognize(url)
console.log(result);
var elem = document.createElement('div');
elem.innerHTML = "<div id=" + num + " style='width:300px; background-color:rgb(215,213,207)'><img src=" + url + " /></div><br>"
var parent = document.getElementById("results");
parent.insertBefore(elem, parent.firstChild);
var numdiv = document.getElementById(num);
var msg = document.createElement('div');
msg.innerHTML = result.data.text;
numdiv.appendChild(msg);
num += 1;
$("#status").text('処理完了');
await worker.terminate();
})();
});
//アップロード!
const fileInput = document.getElementById('file-input');
fileInput.type = 'file';
fileInput.addEventListener('change', function () {
const file = fileInput.files[0];
$("#status").text('アップロード完了');
});
//画像認識!
const ocrButton = document.getElementById('ocr-button');
ocrButton.addEventListener('click', function () {
if (!fileInput.files[0]) {
$("#status").text('あっぷろーどしてね');
return;
}
tryOcr().then(() => {
// OCRが完了
$("#status").text('処理完了')
});
});
const tryOcr = async () => {
$("#status").text('認識中......')
const { createWorker } = Tesseract;
const worker = createWorker({
langPath: './',
gzip: false,
//logger: m => console.log(m)
});
await worker.load();
await worker.loadLanguage('ztmy');
await worker.initialize('ztmy');
var result = await worker.recognize(fileInput.files[0])
console.log(result);
//$("#res_tx").text(result.data.text)
var elem = document.createElement('div');
// Blob URLを作成する
const imageUrl = URL.createObjectURL(fileInput.files[0]);
elem.innerHTML = "<div id=" + num + " style='width:300px; background-color:rgb(215,213,207)'><img src=" + imageUrl + " width='300'; height='100' /></div><br>"
var parent = document.getElementById("results");
parent.insertBefore(elem, parent.firstChild);
var numdiv = document.getElementById(num);
var msg = document.createElement('div');
msg.innerHTML = result.data.text;
numdiv.appendChild(msg);
num += 1;
await worker.terminate();
}
</script>
</div>
</body>
</html>