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

关于#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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题