質問編集履歴

2

認識時間の検証結果を追加

2021/02/24 17:10

投稿

python01
python01

スコア11

test CHANGED
File without changes
test CHANGED
@@ -179,3 +179,69 @@
179
179
 
180
180
 
181
181
  ![イメージ説明](0bfc2a4ffdb0df2b32785fc3daca8be8.png)
182
+
183
+
184
+
185
+ ### 追記(画像処理方法と文字認識時間検証)
186
+
187
+ 下記3種類の画像認識方法について、
188
+
189
+ 認識画像と認識時間を追記しました。
190
+
191
+
192
+
193
+ sakuramochi_py 様の見解通り3種類の組み合わせが一番認識時間が
194
+
195
+ 短い結果となりました。
196
+
197
+
198
+
199
+ ①:フルカラー
200
+
201
+ ②:①+グレースケール
202
+
203
+ ③:②+2値化
204
+
205
+ ④:③+反転
206
+
207
+
208
+
209
+ ![イメージ説明](da9027ce3fad6fb030d9fc7b8f553294.png)
210
+
211
+
212
+
213
+ ```Python
214
+
215
+ img = cv2.imread("./img/eng.png")
216
+
217
+
218
+
219
+ #グレースケールに変換
220
+
221
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
222
+
223
+ cv2.imwrite("./img/eng.png",gray)
224
+
225
+
226
+
227
+ #2値化
228
+
229
+ img = cv2.imread("./img/eng.png")
230
+
231
+ threshold = 105
232
+
233
+ ret,img_thresh = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
234
+
235
+ cv2.imwrite("./img/eng.png",img_thresh)
236
+
237
+
238
+
239
+ #色反転
240
+
241
+ img = cv2.imread("./img/eng.png")
242
+
243
+ img_invert = cv2.bitwise_not(img)
244
+
245
+ cv2.imwrite("./img/eng.png",img_invert)
246
+
247
+ ```

1

解決方法を追記

2021/02/24 17:10

投稿

python01
python01

スコア11

test CHANGED
File without changes
test CHANGED
@@ -87,3 +87,95 @@
87
87
  pyocr:0.8
88
88
 
89
89
  PyAutoGUI:0.9.52
90
+
91
+
92
+
93
+
94
+
95
+ ### 解決方法
96
+
97
+ sakuramochi_py 様にご教授頂き、下記コードで実現できました。
98
+
99
+
100
+
101
+ ```Python
102
+
103
+ import pyautogui as pg
104
+
105
+ import time
106
+
107
+ import cv2
108
+
109
+ import pyautogui as pg
110
+
111
+
112
+
113
+ pg.press('win')
114
+
115
+ time.sleep(2)
116
+
117
+
118
+
119
+ sc = pg.screenshot(region=(50, 100, 500, 700)) #始点x,y、幅、高さ
120
+
121
+ sc.save('./img/img.png')
122
+
123
+
124
+
125
+ lang = 'eng'
126
+
127
+ img_path = './img/{}.png'.format(lang)
128
+
129
+ img = Image.open(img_path)
130
+
131
+ out_path = './img/{}_{}.png'
132
+
133
+
134
+
135
+ word_boxes = tool.image_to_string(
136
+
137
+ img,
138
+
139
+ lang=lang,
140
+
141
+ builder=pyocr.builders.WordBoxBuilder(tesseract_layout=6)
142
+
143
+ )
144
+
145
+
146
+
147
+ out = cv2.imread(img_path)
148
+
149
+
150
+
151
+ for d in word_boxes:
152
+
153
+ print(d.content)
154
+
155
+ print(d.position)
156
+
157
+ cv2.rectangle(out, d.position[0], d.position[1], (0, 0, 255), 2) #d.position[0]は認識した文字の左上の座標,[1]は右下
158
+
159
+ cv2.imwrite(out_path.format(lang, 'word_boxes'), out)
160
+
161
+ x1,y1 = d.position[0]
162
+
163
+ x2,y2 = d.position[1]
164
+
165
+ if(d.content=='Anaconda3'): #Anacondaのアイコンを認識したらクリックする
166
+
167
+ x3 = (x1+x2)/2+50
168
+
169
+ y3 = (y1+y2)/2+100
170
+
171
+ pg.click(x3,y3)
172
+
173
+ ```
174
+
175
+
176
+
177
+ 認識した画像を確認するとこんな感じです。
178
+
179
+
180
+
181
+ ![イメージ説明](0bfc2a4ffdb0df2b32785fc3daca8be8.png)