关于#OPENCV# #OCR#设定区域无效的问题

哪位能帮忙解决下,我用OPENCV 和paddleOCR尝试写一个 识别游戏画面的文字,我设定的区域是屏幕右上角的一小块,label = img_src[6:20, 861:938],但是运行中貌似这个命令没有用,每次OCR都识别的是全图的所有文字,哪位能帮忙看下是哪里出错了,谢谢啦

import cv2
import numpy as np
from PIL import ImageGrab
from win32 import win32gui
import mss
from paddleocr import PaddleOCR, draw_ocr
import threading
from PIL import ImageGrab, Image

global label
global img_src
global cut
global bboxes


img_src = np.zeros((960, 575, 3), np.uint8)
label = np.zeros((160, 50), np.uint8)
bboxes = np.array([])
cut = False


def getScreenshot():
    hwnd = win32gui.FindWindow("LDPlayerMainFrame", "雷电模拟器")
    x0, y0, x1, y1 = win32gui.GetWindowRect(hwnd)
    mtop, mbot = 30, 1
    # print(x0, y0, x1, y1)
    monitor = {"left": x0, "top": y0, "width": x1-x0, "height": y1-y0}
    img_src = np.array(mss.mss().grab(monitor))
    img_src = img_src[:, :, :3]
    img_src = img_src[mtop:-mbot]
    return img_src, [x0, y0, x1, y1, mtop, mbot]

# ocr-----------------------------------------
ocr = PaddleOCR(use_angle_cls=False, lang="ch", show_log=False)

def getMonitor():
    global img_src, label, cut
    while True:
        img_src, _ = getScreenshot()
        label = img_src[6:20, 861:938]
        
def getOcrText(img):
    img = img_src.copy()
    img, _ = getScreenshot()
    print("img::::" + str(img))
    result = ocr.ocr(img, cls=False)
    return result


def getLabelExist(img,name):
    result = getOcrText(img)
    print(result)
    for re in result:
        text = re[1][0]
        if name == text:
            return True
    return False

def checkLabel():
    global label
    global cut
   
    while True:
        cut = getLabelExist(label, '一层')
        if cut:
            print("找到")
         
        else:
            print("未找到")
       




if __name__ == '__main__':
    t1 = threading.Thread(target=getMonitor,args=(),daemon=True)
    t1.start()
    t2 = threading.Thread(target=checkLabel,args=(),daemon=True)
    t2.start()
   

while True:
        img = img_src.copy()
        img, _ = getScreenshot()
        # 按比例缩小-------------------------------------------
        x, y = img.shape[0:2]
        imgs = cv2.resize(img, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
        # ------------------------------------------------
        # bboxes = getDetection(img)
        # img = drawBBox(img.copy(),bboxes)
        cv2.imshow("1234", imgs)
        cv2.imshow("",label)
        if cv2.waitKey(1) & 0xFF == 27:
            cv2.destroyAllWindows()
            break

img_src 是 3通道,label 是 单通道,根本就不能复制

img_src = np.zeros((960, 575, 3), np.uint8)
label = np.zeros((160, 50), np.uint8)

label = img_src[6:20, 861:938]

望采纳!点击该回答右侧的“采纳”按钮即可采纳!!!
我觉得问题可能是你定义 label 的时候,可能会每次都会把 img_src 的一部分赋值给 label,但是你在 getOcrText 函数中并没有使用 label 变量,而是使用了全局变量 img_src。

你可以在 getOcrText 函数中加入一个参数 img,并且在函数内部使用 img 进行 OCR 识别。例如:

def getOcrText(img):
result = ocr.ocr(img, cls=False)
return result


然后在调用 getOcrText 函数时,使用 label 作为参数即可。例如:

cut = getLabelExist(label, '一层')

这样就可以解决你的问题了。

希望这能帮到你!