質問編集履歴

3

自分で調べたことの追加

2018/07/11 00:44

投稿

jun_endo
jun_endo

スコア56

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,13 @@
12
12
 
13
13
  認識したい文字列の特徴が
14
14
 
15
+ ```
16
+
15
- ```0~9999の数字+末尾にアルファベット```
17
+ 0~9999の数字+末尾にアルファベット
18
+
19
+ ```
20
+
21
+
16
22
 
17
23
  という特徴をしています。
18
24
 
@@ -34,7 +40,7 @@
34
40
 
35
41
 
36
42
 
37
- ****2018年7月11日追記****
43
+ ###****2018年7月11日追記****
38
44
 
39
45
 
40
46
 
@@ -46,17 +52,23 @@
46
52
 
47
53
 
48
54
 
55
+ ただ、**認識させたい文字列は数が異常に多く**、画像データは準備できても、
56
+
57
+ **ラベルの作成の時**に、単純に計算しただけでも、
58
+
59
+ **260000次元の配列を必要**としています。
60
+
61
+ (下記で質問しています)
62
+
63
+
64
+
49
-
65
+ 機械学習でラベルの作り方
50
66
 
51
67
  [https://teratail.com/questions/135207](https://teratail.com/questions/135207)
52
68
 
53
69
 
54
70
 
55
- ただ、**認識させたい文字列は数が異常に多く**、画像データは準備できても、
71
+
56
-
57
- **ラベルの作成の時**に、単純に計算しただけでも、
58
-
59
- **260000次元の配列を必要**としています。
60
72
 
61
73
  それは実に、無茶であるために、
62
74
 
@@ -66,8 +78,248 @@
66
78
 
67
79
  そこで、初めて機械学習で認識にかけることができるということです。
68
80
 
81
+ (下記で質問しています)
82
+
83
+
84
+
85
+ ↓機械学習 数字列の桁ごとに画像認識させたい
86
+
69
87
  [https://teratail.com/questions/135292](https://teratail.com/questions/135292)
70
88
 
71
89
 
72
90
 
91
+ ###数字のみの桁ごとに認識するものはあった
92
+
93
+ 桁ごとの数字認識
94
+
95
+ [https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python](https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python)
96
+
97
+
98
+
99
+ 上記のプログラムを、今のバージョン用に書き換えて実行したところ、
100
+
101
+ うまく動きました。
102
+
103
+
104
+
105
+
106
+
107
+ **学習プログラム**
108
+
109
+ ```lang-python
110
+
111
+ import sys
112
+
113
+
114
+
115
+ import numpy as np
116
+
117
+ import cv2
118
+
119
+
120
+
121
+ im = cv2.imread('pitrain.png')
122
+
123
+ im3 = im.copy()
124
+
125
+
126
+
127
+ gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
128
+
129
+ blur = cv2.GaussianBlur(gray,(5,5),0)
130
+
131
+ thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
132
+
133
+
134
+
135
+ ################# Now finding Contours ###################
136
+
137
+
138
+
139
+ image, cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
140
+
141
+
142
+
143
+ samples = np.empty((0,100))
144
+
145
+ responses = []
146
+
147
+ keys = [i for i in range(48,58)]
148
+
149
+
150
+
151
+ for cnt in cnts:
152
+
153
+ if cv2.contourArea(cnt)>50:
154
+
155
+ [x,y,w,h] = cv2.boundingRect(cnt)
156
+
157
+
158
+
159
+ if h>28:
160
+
161
+ cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
162
+
163
+ roi = thresh[y:y+h,x:x+w]
164
+
165
+ roismall = cv2.resize(roi,(10,10))
166
+
167
+ cv2.imshow('norm',im)
168
+
169
+ key = cv2.waitKey(0)
170
+
171
+
172
+
173
+ if key == 27: # (escape to quit)
174
+
175
+ sys.exit()
176
+
177
+ elif key in keys:
178
+
179
+ responses.append(int(chr(key)))
180
+
181
+ sample = roismall.reshape((1,100))
182
+
183
+ samples = np.append(samples,sample,0)
184
+
185
+
186
+
187
+ responses = np.array(responses,np.float32)
188
+
189
+ responses = responses.reshape((responses.size,1))
190
+
191
+ print("training complete")
192
+
193
+
194
+
195
+ np.savetxt('generalsamples.data',samples)
196
+
197
+ np.savetxt('generalresponses.data',responses)
198
+
199
+ ```
200
+
201
+ pitrain.png
202
+
203
+ ![pitrain.png](69d6761129d6a5c7cbee8de2a1ec50b9.png)
204
+
205
+
206
+
207
+ テスト用
208
+
209
+ ```lang-python
210
+
211
+ import cv2
212
+
213
+ import numpy as np
214
+
215
+
216
+
217
+ ####### training part ###############
218
+
219
+ samples = np.loadtxt('generalsamples.data',np.float32)
220
+
221
+ responses = np.loadtxt('generalresponses.data',np.float32)
222
+
223
+ responses = responses.reshape((responses.size,1))
224
+
225
+
226
+
227
+ model = cv2.ml.KNearest_create()
228
+
229
+ model.train(samples, cv2.ml.ROW_SAMPLE, responses)
230
+
231
+
232
+
233
+ ############################# testing part #########################
234
+
235
+
236
+
237
+ im = cv2.imread('PS.png')
238
+
239
+ out = np.zeros(im.shape,np.uint8)
240
+
241
+ gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
242
+
243
+ thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
244
+
245
+
246
+
247
+ imgs, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
248
+
249
+
250
+
251
+ for cnt in contours:
252
+
253
+ if cv2.contourArea(cnt)>50:
254
+
255
+ [x,y,w,h] = cv2.boundingRect(cnt)
256
+
257
+ if h>28:
258
+
259
+ cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
260
+
261
+ roi = thresh[y:y+h,x:x+w]
262
+
263
+ roismall = cv2.resize(roi,(10,10))
264
+
265
+ roismall = roismall.reshape((1,100))
266
+
267
+ roismall = np.float32(roismall)
268
+
269
+ retval, results, neigh_resp, dists = model.findNearest(roismall, k = 1)
270
+
271
+ string = str(int((results[0][0])))
272
+
273
+ cv2.putText(out,string,(x,y+h),0,1,(0,255,0))
274
+
275
+
276
+
277
+ cv2.imshow('im',im)
278
+
279
+ cv2.imshow('out',out)
280
+
281
+ cv2.waitKey(10000)
282
+
283
+ ```
284
+
285
+ pi.png
286
+
287
+ ![pi.png](67b652489feda92c62077f820a65e03d.png)
288
+
289
+
290
+
291
+ **結果は上記のサイトにありますのでそちらを見てください。**
292
+
293
+
294
+
295
+ ###画像の数字にアルファベットを混ぜてみた
296
+
297
+ 上記のプログラムを全く変えないで、
298
+
299
+ 画像を以下のものにしました。
300
+
301
+
302
+
303
+ PS.png
304
+
305
+ ![イメージ説明](c3efd715601900962bdf6b8d334c496c.png)
306
+
307
+ それで実行してみたところ、
308
+
73
- **数字+アルファベット**の混合文列を認識させた
309
+ 案の定、アルファベットも数認識してしま
310
+
311
+ 失敗に終わりました。
312
+
313
+
314
+
315
+ ###
316
+
317
+ 上記のプログラムに何か付け足すことで、
318
+
319
+ アルファベットを認識できる方法があるのであれば、
320
+
321
+ 知りたいです。
322
+
323
+
324
+
325
+ そのほか、何か方法があれば教えてください。

2

へんしゅう1

2018/07/11 00:44

投稿

jun_endo
jun_endo

スコア56

test CHANGED
File without changes
test CHANGED
@@ -44,6 +44,14 @@
44
44
 
45
45
  **本来は、機械学習で文字認識をしたい**と検討していました。
46
46
 
47
+
48
+
49
+
50
+
51
+ [https://teratail.com/questions/135207](https://teratail.com/questions/135207)
52
+
53
+
54
+
47
55
  ただ、**認識させたい文字列は数が異常に多く**、画像データは準備できても、
48
56
 
49
57
  **ラベルの作成の時**に、単純に計算しただけでも、
@@ -58,7 +66,7 @@
58
66
 
59
67
  そこで、初めて機械学習で認識にかけることができるということです。
60
68
 
61
-
69
+ [https://teratail.com/questions/135292](https://teratail.com/questions/135292)
62
70
 
63
71
 
64
72
 

1

自分の中で考えたことの追加

2018/07/11 00:16

投稿

jun_endo
jun_endo

スコア56

test CHANGED
File without changes
test CHANGED
@@ -31,3 +31,35 @@
31
31
 
32
32
 
33
33
  どなたか教えてください。
34
+
35
+
36
+
37
+ ****2018年7月11日追記****
38
+
39
+
40
+
41
+ なぜ、**文字を物体認識**にかけたいかというと、
42
+
43
+
44
+
45
+ **本来は、機械学習で文字認識をしたい**と検討していました。
46
+
47
+ ただ、**認識させたい文字列は数が異常に多く**、画像データは準備できても、
48
+
49
+ **ラベルの作成の時**に、単純に計算しただけでも、
50
+
51
+ **260000次元の配列を必要**としています。
52
+
53
+ それは実に、無茶であるために、
54
+
55
+ **前処理の段階で、
56
+
57
+ 文字列を桁ごとに分割して、仮想的に画像化**することで、
58
+
59
+ そこで、初めて機械学習で認識にかけることができるということです。
60
+
61
+
62
+
63
+
64
+
65
+ 1、**数字+アルファベット**の混合文字列を認識させたい