26
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでタイピングゲームをプレイしてみた

Last updated at Posted at 2017-06-05

#概要
pytesseractによる光学文字認識と,PyAutoGUIによる自動キーボード入力を利用して,英単語タイピングゲームをプレイしてみた.

Typing Test English : https://10fastfingers.com/typing-test/english

#環境
OS:Windows10 64bit
Python version : 3.5.3

#ゲームの概要
capture2.jpg

赤枠で囲んだ領域に英単語が表示されるので,それをひたすら入力フォームに打ち込んで行き,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()


  1. Selenium でTyping Test English にアクセス
  2. Pillow で赤枠内の領域をキャプチャ
  3. pytesseract でキャプチャした画像から英単語を抽出
  4. PyAutoGUI で抽出した英単語を自動入力

↓こんな感じで動作します
video.gif

#結論
pytesseractによる光学文字認識や,PyAutoGUIによる自動キーボード入力を組み合わせることで,ブラウザ自動化の幅が大きく広がる.

26
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?