PythONBuGs 2022-12-27 22:11 采纳率: 100%
浏览 44
已结题

关于#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

  • 写回答

2条回答 默认 最新

  • youcans_ 人工智能领域优质创作者 2022-12-28 12:10
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 修改了问题 12月27日
  • 创建了问题 12月27日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错