哪位能帮忙解决下,我用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