【问题标题】:how to perform OCR on running program window如何在正在运行的程序窗口上执行 OCR
【发布时间】:2020-11-19 01:07:11
【问题描述】:

我需要从正在运行的程序(比如游戏)中提取信息、文本和数字。

所以我想知道是否有一种方法可以在程序窗口上执行 OCR,连续“观察”它,如果满足某些条件,则在窗口的特定区域执行 OCR 以提取文本。

我承认这听起来可能很奇怪,但这就是我需要做的! :)

感谢您提供有关此主题的任何提示。 G.

【问题讨论】:

    标签: ocr


    【解决方案1】:

    虽然我不确定实时全屏这样做是否可行,但如果窗口小,文本大,那么它可以做得更快,更准确。

    您需要设置一个计时器来跳过帧、重新捕获并重新运行 OCR 模型以连续处理帧。要获得更多accurate results,请使用更大的图像或调整 ocr 参数。如果捕获时窗口在后台,则需要将其放在前面,否则我的代码实现会出现错误的窗口。

    一个有用的实现,Can python get the screen shot of a specific window?

    这是我抓取单个指定窗口并使用EasyOCR 处理的简单实现。文本和输出位置将打印在控制台上。

    代码

    import pyscreenshot as ImageGrab
    import pygetwindow as gw
    import numpy as np
    import easyocr
    import cv2
    
    
    # need to run only once to load model into memory
    print("Loading Model")
    reader = easyocr.Reader(['ch_sim','en'], gpu = True)
    
    
    # Get by either window title, active window or window directly
    print(gw.getAllTitles())
    print(gw.getActiveWindow())
    print(gw.getAllWindows())
    
    
    # Provide desired window title manually or with title index of getAllTitles
    tmp = gw.getWindowsWithTitle('untitled1 – test2.py PyCharm')
    
    
    # Print window location, title
    print(int(np.abs(tmp[0].left)), int(np.abs(tmp[0].top)), int(np.abs(tmp[0].right)), int(np.abs(tmp[0].bottom)))
    print(tmp[0])
    
    
    # grab fullscreen
    #im = ImageGrab.grab()
    
    
    # grab certain portion of selected window
    print("Grabbing Window")
    im = ImageGrab.grab(bbox=(int(np.abs(tmp[0].left)), int(np.abs(tmp[0].top)), int(np.abs(tmp[0].right)), int(np.abs(tmp[0].bottom))))  # X1,Y1,X2,Y2
    
    
    
    # save image file and process that image
    #im.save("window1.jpg")
    #result = reader.readtext('window1.jpg')
    
    
    
    # Resize the image to certain percent of original image
    print("Resizing Image")
    im_np = np.array(im)
    scale_percent = 50
    width = int(im_np.shape[1] * scale_percent / 100)
    height = int(im_np.shape[0] * scale_percent / 100)
    dim = (width, height)
    im_resized = cv2.resize(im_np, dim, interpolation = cv2.INTER_AREA)
    
    
    
    # process the grabbed image directly as numpy array
    print("Processing with OCR")
    result = reader.readtext(np.array(im_resized))
    print(result)
    
    
    
    # For each detected text draw bounding box in image and print text, location
    i = 0
    for r in result:
        print("################################")
        print(r[0][0], r[0][1], r[0][2], r[0][3])
        print("FOUND:")
        print(r[1])
        print("################################")
    
        x1=min(r[0][0][0], r[0][1][0], r[0][2][0], r[0][3][0])
        x2=max(r[0][0][0], r[0][1][0], r[0][2][0], r[0][3][0])
        y1=min(r[0][0][1], r[0][1][1], r[0][2][1], r[0][3][1])
        y2=max(r[0][0][1], r[0][1][1], r[0][2][1], r[0][3][1])
    
        image = cv2.rectangle(im_resized, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 255), 3)
    
        cv2.imshow("window_name", image)
        cv2.waitKey(0)
        cv2.imwrite("images/img_" + str(i) + ".jpg", image)
        i += 1
    
    

    部分控制台输出

    ################################
    [409, 117] [465, 117] [465, 129] [409, 129]
    FOUND:
    r[u][1][u],
    ################################
    ################################
    [469, 117] [525, 117] [525, 129] [469, 129]
    FOUND:
    r[u][2][0],
    ################################
    ################################
    [529, 117] [587, 117] [587, 129] [529, 129]
    FOUND:
    r[u][][0]1
    ################################
    

    图像输出

    没有提及平台或编程语言实现。这里的大多数库都声称是跨平台的。查看这些以获取窗口位置、选定窗口、窗口控件、屏幕截图。

    https://github.com/asweigart/pyautogui

    https://github.com/asweigart/pygetwindow

    https://github.com/ponty/pyscreenshot

    一个简单的基于 Python 的 OCR,

    https://github.com/JaidedAI/EasyOCR

    【讨论】:

      【解决方案2】:

      您需要在流媒体中这样做吗?

      如果您可以录制屏幕,则可以将视频分割为帧,然后每帧 ocr 帧将完成剩下的工作。

      【讨论】:

      • 这对我来说是最好的选择,因为如果满足某些“屏幕上”条件,我需要采取行动。前任。如果在屏幕的特定区域找到“下一个”一词,我需要执行操作 A
      猜你喜欢
      • 2011-02-20
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多