質問編集履歴
2
認識時間の検証結果を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -88,4 +88,37 @@
|
|
88
88
|
|
89
89
|
認識した画像を確認するとこんな感じです。
|
90
90
|
|
91
|
-

|
91
|
+

|
92
|
+
|
93
|
+
### 追記(画像処理方法と文字認識時間検証)
|
94
|
+
下記3種類の画像認識方法について、
|
95
|
+
認識画像と認識時間を追記しました。
|
96
|
+
|
97
|
+
sakuramochi_py 様の見解通り3種類の組み合わせが一番認識時間が
|
98
|
+
短い結果となりました。
|
99
|
+
|
100
|
+
①:フルカラー
|
101
|
+
②:①+グレースケール
|
102
|
+
③:②+2値化
|
103
|
+
④:③+反転
|
104
|
+
|
105
|
+

|
106
|
+
|
107
|
+
```Python
|
108
|
+
img = cv2.imread("./img/eng.png")
|
109
|
+
|
110
|
+
#グレースケールに変換
|
111
|
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
112
|
+
cv2.imwrite("./img/eng.png",gray)
|
113
|
+
|
114
|
+
#2値化
|
115
|
+
img = cv2.imread("./img/eng.png")
|
116
|
+
threshold = 105
|
117
|
+
ret,img_thresh = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
|
118
|
+
cv2.imwrite("./img/eng.png",img_thresh)
|
119
|
+
|
120
|
+
#色反転
|
121
|
+
img = cv2.imread("./img/eng.png")
|
122
|
+
img_invert = cv2.bitwise_not(img)
|
123
|
+
cv2.imwrite("./img/eng.png",img_invert)
|
124
|
+
```
|
1
解決方法を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,4 +42,50 @@
|
|
42
42
|
pip:21.0.1
|
43
43
|
opencv-python:4.5.1.48
|
44
44
|
pyocr:0.8
|
45
|
-
PyAutoGUI:0.9.52
|
45
|
+
PyAutoGUI:0.9.52
|
46
|
+
|
47
|
+
|
48
|
+
### 解決方法
|
49
|
+
sakuramochi_py 様にご教授頂き、下記コードで実現できました。
|
50
|
+
|
51
|
+
```Python
|
52
|
+
import pyautogui as pg
|
53
|
+
import time
|
54
|
+
import cv2
|
55
|
+
import pyautogui as pg
|
56
|
+
|
57
|
+
pg.press('win')
|
58
|
+
time.sleep(2)
|
59
|
+
|
60
|
+
sc = pg.screenshot(region=(50, 100, 500, 700)) #始点x,y、幅、高さ
|
61
|
+
sc.save('./img/img.png')
|
62
|
+
|
63
|
+
lang = 'eng'
|
64
|
+
img_path = './img/{}.png'.format(lang)
|
65
|
+
img = Image.open(img_path)
|
66
|
+
out_path = './img/{}_{}.png'
|
67
|
+
|
68
|
+
word_boxes = tool.image_to_string(
|
69
|
+
img,
|
70
|
+
lang=lang,
|
71
|
+
builder=pyocr.builders.WordBoxBuilder(tesseract_layout=6)
|
72
|
+
)
|
73
|
+
|
74
|
+
out = cv2.imread(img_path)
|
75
|
+
|
76
|
+
for d in word_boxes:
|
77
|
+
print(d.content)
|
78
|
+
print(d.position)
|
79
|
+
cv2.rectangle(out, d.position[0], d.position[1], (0, 0, 255), 2) #d.position[0]は認識した文字の左上の座標,[1]は右下
|
80
|
+
cv2.imwrite(out_path.format(lang, 'word_boxes'), out)
|
81
|
+
x1,y1 = d.position[0]
|
82
|
+
x2,y2 = d.position[1]
|
83
|
+
if(d.content=='Anaconda3'): #Anacondaのアイコンを認識したらクリックする
|
84
|
+
x3 = (x1+x2)/2+50
|
85
|
+
y3 = (y1+y2)/2+100
|
86
|
+
pg.click(x3,y3)
|
87
|
+
```
|
88
|
+
|
89
|
+
認識した画像を確認するとこんな感じです。
|
90
|
+
|
91
|
+

|