I tried playing a typing game in Python

Overview

I played an English word typing game using optical character recognition by pytesseract and automatic keyboard input by PyAutoGUI.

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

environment

OS:Windows10 64bit Python version : 3.5.3

Game overview

capture2.jpg

English words are displayed in the area surrounded by a red frame, so you can simply type them into the input form and compete for how many words you can enter in one minute.

Main libraries used

selenium, PyAutoGUI, pytesseract, Pillow

Program behavior

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. Access Typing Test English on Selenium
  2. Capture the area inside the red frame with Pillow
  3. Extract English words from images captured by pytesseract
  4. Automatically enter English words extracted by PyAutoGUI

↓ It works like this video.gif

Conclusion

By combining optical character recognition with pytesseract and automatic keyboard input with PyAutoGUI, the range of browser automation can be greatly expanded.

Recommended Posts

I tried playing a typing game in Python
I tried to implement a card game of playing cards in Python
I made a simple typing game with tkinter in Python
I tried playing a ○ ✕ game using TensorFlow
I created a class in Python and tried duck typing
I tried to implement a misunderstood prisoner's dilemma game in Python
[Memo] I tried a pivot table in Python
I tried adding a Python3 module in C
I tried to implement a pseudo pachislot in Python
I tried Line notification in Python
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
I tried a stochastic simulation of a bingo game with Python
I tried to implement blackjack of card game in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I made a payroll program in Python!
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I tried to implement ADALINE in Python
I tried a functional language with Python
I tried to implement PPO in Python
I created a password tool in Python.
I made a roguelike game with Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
A memo that I wrote a quicksort in Python
I want to create a window in Python
I tried Python> autopep8
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I want to make a game with Python
I wrote a class in Python3 and Java
Make a rock-paper-scissors game in one line (python)
I tried to implement TOPIC MODEL in Python
I tried reading a CSV file using Python
I tried to develop a Formatter that outputs Python logs in JSON
I tried non-blocking I / O Eventlet behavior in Python
I tried playing mahjong with Python (single mahjong edition)
I tried running alembic, a Python migration tool
I tried to implement selection sort in python
I tried Python> decorator
I made a Caesar cryptographic program in Python.
I tried to make a ○ ✕ game using TensorFlow
I made a bin picking game with Python
I tried to create a class that can easily serialize Json in Python
I tried to implement what seems to be a Windows snipping tool in Python
Draw a graph in Julia ... I tried a little analysis
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I made a prime number generation program in Python
I made a Christmas tree lighting game with Python
I tried using TradeWave (BitCoin system trading in Python)
Playing with a user-local artificial intelligence API in Python
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I tried running python etc. from a bat file
I want to randomly sample a file in Python
I tried to implement Dragon Quest poker in Python
I want to work with a robot in python.