Pythonでタイピングゲームをプレイしてみた
概要
pytesseractによる光学文字認識と,PyAutoGUIによる自動キーボード入力を利用して,英単語タイピングゲームをプレイしてみた.
Typing Test English : https://10fastfingers.com/typing-test/english
環境
OS:Windows10 64bit
Python version : 3.5.3
ゲームの概要
赤枠で囲んだ領域に英単語が表示されるので,それをひたすら入力フォームに打ち込んで行き,1分間に何単語入力できたかを競う単純なゲーム.
使用した主なライブラリ
selenium, PyAutoGUI, pytesseract, Pillow
プログラムの動作
main.py
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageGrab
import pytesseract
import numpy as np
import matplotlib.pyplot as plt
import pyautogui as pyag
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import cv2
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
fig, ax = plt.subplots()
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(10,800,1300,250)
plt.pause(0.001)
url = "https://10fastfingers.com/typing-test/english"
chrome_driver_path = "chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument("--window-position=0,0");
chrome_options.add_argument("--window-size=1000,600");
browser = webdriver.Chrome(chrome_driver_path, chrome_options=chrome_options)
print("Loading...")
browser.get(url)
time.sleep(3)
while(True):
print("-----------------------------------------")
print("Extracted words")
print("-----------------------------------------")
img = ImageGrab.grab(bbox=(120,308,1213,430))
ax.imshow(img)
plt.pause(0.001)
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = cv2.threshold(img, 220, 255, cv2.THRESH_BINARY)[1]
img = Image.fromarray(img)
words = pytesseract.image_to_string(img).split(" ")
for word1 in words:
if ("\n\n" in word1):
word1 = word1.split("\n\n")
elif ("\n" in word1):
word1 = word1.split("\n")
else:
word1 = [word1]
for word2 in word1:
print(word2)
pyag.typewrite(word2.replace(" ", "") + " ")
time.sleep(0.2)
if len(words) < 10:
print("Done!")
break
time.sleep(3)
browser.quit()
- Selenium でTyping Test English にアクセス
- Pillow で赤枠内の領域をキャプチャ
- pytesseract でキャプチャした画像から英単語を抽出
- PyAutoGUI で抽出した英単語を自動入力
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageGrab
import pytesseract
import numpy as np
import matplotlib.pyplot as plt
import pyautogui as pyag
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import cv2
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
fig, ax = plt.subplots()
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(10,800,1300,250)
plt.pause(0.001)
url = "https://10fastfingers.com/typing-test/english"
chrome_driver_path = "chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument("--window-position=0,0");
chrome_options.add_argument("--window-size=1000,600");
browser = webdriver.Chrome(chrome_driver_path, chrome_options=chrome_options)
print("Loading...")
browser.get(url)
time.sleep(3)
while(True):
print("-----------------------------------------")
print("Extracted words")
print("-----------------------------------------")
img = ImageGrab.grab(bbox=(120,308,1213,430))
ax.imshow(img)
plt.pause(0.001)
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = cv2.threshold(img, 220, 255, cv2.THRESH_BINARY)[1]
img = Image.fromarray(img)
words = pytesseract.image_to_string(img).split(" ")
for word1 in words:
if ("\n\n" in word1):
word1 = word1.split("\n\n")
elif ("\n" in word1):
word1 = word1.split("\n")
else:
word1 = [word1]
for word2 in word1:
print(word2)
pyag.typewrite(word2.replace(" ", "") + " ")
time.sleep(0.2)
if len(words) < 10:
print("Done!")
break
time.sleep(3)
browser.quit()
結論
pytesseractによる光学文字認識や,PyAutoGUIによる自動キーボード入力を組み合わせることで,ブラウザ自動化の幅が大きく広がる.
Author And Source
この問題について(Pythonでタイピングゲームをプレイしてみた), 我々は、より多くの情報をここで見つけました https://qiita.com/harupy/items/4b5c6ba5e73031f213b4著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .