Created
July 29, 2020 06:40
-
-
Save wolfmanstout/5e8a286176f432d006640e3c1c4b45c1 to your computer and use it in GitHub Desktop.
Demonstrates using Python winrt to run OCR. Requires Python 3.7+.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import os | |
import winrt | |
from PIL import Image | |
from winrt.windows.graphics.imaging import BitmapDecoder, BitmapPixelFormat, SoftwareBitmap | |
from winrt.windows.media.ocr import OcrEngine | |
from winrt.windows.storage import StorageFile, FileAccessMode | |
import winrt.windows.storage.streams as streams | |
async def load_image_file_native(file_path): | |
file = await StorageFile.get_file_from_path_async(os.fspath(file_path)) | |
stream = await file.open_async(FileAccessMode.READ) | |
decoder = await BitmapDecoder.create_async(stream) | |
return await decoder.get_software_bitmap_async() | |
def load_image_file_pillow(file_path): | |
image = Image.open(file_path).convert("RGBA") | |
data_writer = streams.DataWriter() | |
bytes = image.tobytes() | |
data_writer.write_bytes(list(bytes)) | |
bitmap = SoftwareBitmap(BitmapPixelFormat.RGBA8, image.width, image.height) | |
bitmap.copy_from_buffer(data_writer.detach_buffer()) | |
return bitmap | |
async def run_ocr(bitmap): | |
engine = OcrEngine.try_create_from_user_profile_languages() | |
return await engine.recognize_async(bitmap) | |
async def async_main(): | |
screenshot_path = r"C:\Users\james\Documents\OCR\logs\failure_1595912159.34.png" | |
# bitmap = await load_image_file_native(screenshot_path) | |
bitmap = load_image_file_pillow(screenshot_path) | |
result = await run_ocr(bitmap) | |
print(result.text) | |
asyncio.run(async_main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment