OCR on screenshots with tesseract - Windows only
Project description
Scan screenshots with Tesseract
Example 1 (screenshots from BlueStacks using ADB)
import keyboard
import cv2
import pandas as pd
import numpy as np
from time import sleep
from windows_adb_screen_capture import ScreenShots
from tesseract_window_scanner import pd_add_tesseract, sub_color_in_image, \
substitute_colors_with_equal_rgb_values, draw_tesseract_results, get_tesseractdf
def activate_stop():
global stop
stop = True
tesseractpath = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
pd_add_tesseract(tesseractpath)
languages = "de+pt+deu"
sc2 = ScreenShots(
hwnd=None, adb_path=r"C:\ProgramData\adb\adb.exe", adb_serial="localhost:5735"
)
quit_key = "q"
sc2.imshow_adb(sleep_time=0.05, quit_key=quit_key)
sleep(1)
sc2.enable_show_edited_images()
stop = False
keyboard.add_hotkey(quit_key, activate_stop)
showresults = True
while not stop:
screenshot_window = sc2.imget_adb()
# optional filter
screenshot_window = sub_color_in_image(
img=screenshot_window,
conditions=(("r", ">", 200), "|", ("g", ">", 200), "|", ("b", ">", 200)),
newcolor=(255, 255, 255),
)
df = get_tesseractdf(
screenshot_window, lang=languages, drop_empty_strings=True, conf_thresh=60
)
tesserresults = draw_tesseract_results(
dft=df, img=screenshot_window, conf_thresh=60
)
if showresults:
sc2.show_edited_image(tesserresults) # show the edited pic
print(df[["text", "conf"]])
Example 2 (screenshots from BlueStacks using hwnd)
import keyboard
import cv2
import pandas as pd
import numpy as np
from time import sleep
from windows_adb_screen_capture import ScreenShots
from tesseract_window_scanner import pd_add_tesseract, sub_color_in_image, \
substitute_colors_with_equal_rgb_values, draw_tesseract_results, get_tesseractdf
def activate_stop():
global stop
stop = True
tesseractpath = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
languages = "de+pt+deu"
pd_add_tesseract(tesseractpath)
sc2 = ScreenShots()
sc2.find_window_with_regex("[bB]lue[sS]tacks.*")
quit_key = "e"
sc2.imshow_hwnd(sleep_time=0.05, quit_key=quit_key)
sleep(1)
sc2.enable_show_edited_images()
stop = False
keyboard.add_hotkey(quit_key, activate_stop)
showresults = True
while not stop:
screenshot_window = sc2.imget_hwnd()
# screenshot_window=sub_color_in_image(img=screenshot_window, conditions=(('r' ,'>', 200) ,'|' ,('g' ,'>', 200), '|', ('b' ,'>', 200)), newcolor=(255,255,255))
df = get_tesseractdf(
screenshot_window, lang=languages, drop_empty_strings=True, conf_thresh=60
)
tesserresults = draw_tesseract_results(
dft=df, img=screenshot_window, conf_thresh=60
)
if showresults:
sc2.show_edited_image(tesserresults) # show the edited pic
print(df[["text", "conf"]])
Example 3 (screenshots from BlueStacks using hwnd without showing the results)
import keyboard
import cv2
import pandas as pd
import numpy as np
from time import sleep
from windows_adb_screen_capture import ScreenShots
from tesseract_window_scanner import pd_add_tesseract, sub_color_in_image, \
substitute_colors_with_equal_rgb_values, draw_tesseract_results, get_tesseractdf
def activate_stop():
global stop
stop = True
tesseractpath = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
languages = "de+pt+deu"
pd_add_tesseract(tesseractpath)
sc2 = ScreenShots()
sc2.find_window_with_regex("[bB]lue[sS]tacks.*")
quit_key = "x"
stop = False
keyboard.add_hotkey(quit_key, activate_stop)
while not stop:
screenshot_window = sc2.imget_hwnd()
#optional filter
screenshot_window = sub_color_in_image(
img=screenshot_window,
conditions=(("r", ">", 200), "|", ("g", ">", 200), "|", ("b", ">", 200)),
newcolor=(255, 255, 255),
)
df = get_tesseractdf(
screenshot_window, lang=languages, drop_empty_strings=True, conf_thresh=60
)
print(df[["text", "conf"]])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for tesseract_window_scanner-0.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 841b6e6a73a33b0d9d2f3c5d13fc8b1f80fffce6feb42c31c4d52faa1a229ede |
|
MD5 | 31160990bdd758433f810e9b8d5c2783 |
|
BLAKE2b-256 | 35c15ff33283299fa1eff10d98ba027dd0b1d366d4c84797d5c6e8d7f3bb3f60 |
Close
Hashes for tesseract_window_scanner-0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba206400c791b95e941ad95b06fc049b904b8abab07ae2234b1822f1f81a61bb |
|
MD5 | 44e8709aae5007d12e7c517edd4caa9b |
|
BLAKE2b-256 | 02acc74ce93ebb5b6122fc28afaf1611df7ac5fb2e398dcbff23b071fd11ed33 |