回答編集履歴
1
質問文のソースコードが削除されたので、回答文に記載。
test
CHANGED
@@ -20,6 +20,78 @@
|
|
20
20
|
|
21
21
|
```JavaScript
|
22
22
|
|
23
|
+
<!DOCTYPE html>
|
24
|
+
|
25
|
+
<html>
|
26
|
+
|
27
|
+
<head>
|
28
|
+
|
29
|
+
<meta charset="UTF-8">
|
30
|
+
|
31
|
+
<base target="_top">
|
32
|
+
|
33
|
+
<title>Tesseract.js</title>
|
34
|
+
|
35
|
+
<script src='https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js'></script>
|
36
|
+
|
37
|
+
<script>
|
38
|
+
|
39
|
+
// ユーザ指定のファイルを読み込み、画像認識の関数を呼び出す。
|
40
|
+
|
41
|
+
function read_in() {
|
42
|
+
|
43
|
+
const reader = new FileReader();
|
44
|
+
|
45
|
+
reader.onload = (e) => {
|
46
|
+
|
47
|
+
//選択された画像をimg要素として表示する。
|
48
|
+
|
49
|
+
document.getElementById("read_image").src = e.target.result;
|
50
|
+
|
51
|
+
//画像認識を行う
|
52
|
+
|
53
|
+
recognize_image();
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
// 画像ファイルをdata URLとして読み込むように指示しておく。
|
58
|
+
|
59
|
+
reader.readAsDataURL(document.getElementById("input_image_file").files[0]);
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// 画像認識を行う。
|
68
|
+
|
69
|
+
function recognize_image() {
|
70
|
+
|
71
|
+
//結果出力先の要素を取得する。
|
72
|
+
|
73
|
+
const txt_out = document.getElementById("text_of_read_image");
|
74
|
+
|
75
|
+
//複数回、連続して実行するときのために、最初に中身を捨てる。
|
76
|
+
|
77
|
+
txt_out.innerHTML = "";
|
78
|
+
|
79
|
+
//指定された言語のコードを取得する。
|
80
|
+
|
81
|
+
const lang_list = document.getElementById("lang_options");
|
82
|
+
|
83
|
+
const selected_lang = lang_list.options[lang_list.selectedIndex].value;
|
84
|
+
|
85
|
+
console.log(selected_lang + "が選択されました。");
|
86
|
+
|
87
|
+
const msg = document.getElementById("msg");
|
88
|
+
|
89
|
+
msg.textContent = "[処理を開始しました。]\n";
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
//画像認識
|
94
|
+
|
23
95
|
Tesseract.recognize(
|
24
96
|
|
25
97
|
document.getElementById("read_image").src,
|
@@ -30,16 +102,108 @@
|
|
30
102
|
|
31
103
|
})
|
32
104
|
|
33
|
-
.progress(
|
105
|
+
.progress(message => { console.log('progress is: ', message); })
|
34
|
-
|
106
|
+
|
35
|
-
.catch(
|
107
|
+
.catch(e => {
|
36
|
-
|
108
|
+
|
37
|
-
|
109
|
+
msg.textContent += "[エラーです: " + e + "]";
|
38
110
|
|
39
111
|
console.log("エラーです: " + e);
|
40
112
|
|
41
113
|
})
|
42
114
|
|
115
|
+
.then(result =>{
|
116
|
+
|
117
|
+
txt_out.innerHTML = result.text;
|
118
|
+
|
119
|
+
})
|
120
|
+
|
121
|
+
.finally(r => {
|
122
|
+
|
123
|
+
msg.textContent += "[処理が終わりました]\n";
|
124
|
+
|
125
|
+
});
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
</script>
|
132
|
+
|
133
|
+
</head>
|
134
|
+
|
135
|
+
<body class="main-text-only">
|
136
|
+
|
137
|
+
<form name="f">
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
<ol>
|
142
|
+
|
143
|
+
<li>言語を指定し (
|
144
|
+
|
145
|
+
<select name="lang_options" id="lang_options">
|
146
|
+
|
147
|
+
<option value="eng">English</option>
|
148
|
+
|
149
|
+
<option value="jpn" selected>Japanese</option>
|
150
|
+
|
151
|
+
<option value="kor">Korean</option>
|
152
|
+
|
153
|
+
</select>
|
154
|
+
|
155
|
+
)、
|
156
|
+
|
157
|
+
</li>
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
<li>
|
162
|
+
|
163
|
+
画像ファイルを選択し
|
164
|
+
|
165
|
+
(<input type="file" id="input_image_file" name="input_image_file" />)、
|
166
|
+
|
167
|
+
</li>
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
<li>
|
172
|
+
|
173
|
+
そのファイルを
|
174
|
+
|
175
|
+
<input type="button" value="読み込んで" onclick="read_in()">
|
176
|
+
|
177
|
+
ください。
|
178
|
+
|
179
|
+
</li>
|
180
|
+
|
181
|
+
</ol>
|
182
|
+
|
183
|
+
</form>
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
<table style="margin: auto;">
|
188
|
+
|
189
|
+
<tr><th>認識対象画像</th><th>読み取り結果</th></tr>
|
190
|
+
|
191
|
+
<tr>
|
192
|
+
|
193
|
+
<td style="overflow: auto;"><img id="read_image" style="max-width: 450px; max-height: 450px;" /></td>
|
194
|
+
|
195
|
+
<td id="text_of_read_image" style="white-space: pre; font-family: monospace;"></td>
|
196
|
+
|
197
|
+
</tr>
|
198
|
+
|
199
|
+
</table>
|
200
|
+
|
201
|
+
<p id="msg" style="color: darkred; font-style: italic; overflow: auto; max-height: 10ex;"></p>
|
202
|
+
|
203
|
+
</body>
|
204
|
+
|
205
|
+
</html>
|
206
|
+
|
43
207
|
```
|
44
208
|
|
45
209
|
![イメージ説明](e6964fcc31d2af268078fbef065450e6.png)
|