纯python实现大漠ocr功能

查看 54|回复 9
作者:18834161486   
分享一套自用的ocr识别及合并算法,字库借用大漠的字库数据,然后使用opencv的模板匹配来实现,代码里面有详细的例子。
[Python] 纯文本查看 复制代码import time
from ctypes import windll
import numpy as np
import cv2
from mss import mss
class Mxbx:
    def __init__(self):
        print('欢迎使用')
        windll.user32.SetProcessDPIAware()
        self.sct = mss()
    def GetCapture(self, x1, y1, x2, y2):
        '''
        截图
        :param x1:
        :param y1:
        :param x2:
        :param y2:
        :return: rgb图像
        '''
        monitor = {'top': y1, 'left': x1, 'width': x2 - x1, 'height': y2 - y1}
        pic = self.sct.grab(monitor)
        pic = cv2.cvtColor(np.array(pic), cv2.COLOR_RGBA2RGB)
        return pic
    def parse_custom_color(self, color_string):
        r, g, b = int(color_string[4:6], 16), int(color_string[2:4], 16), int(color_string[0:2], 16)
        if len(color_string) > 6:
            dr, dg, db = int(color_string[7:9], 16), int(color_string[9:11], 16), int(color_string[11:13], 16)
        else:
            dr, dg, db = 0, 0, 0
        lower_rgb = np.array([max(0, r - dr), max(0, g - dg), max(0, b - db)], dtype=np.uint8)
        upper_rgb = np.array([min(255, r + dr), min(255, g + dg), min(255, b + db)], dtype=np.uint8)
        return lower_rgb, upper_rgb
    def Stressshow(self, x1, y1, x2, y2, colors, types=0):
        '''
        隐藏其他颜色为黑色,只保留选中颜色
        :param x1: 起点x
        :param y1: 起点Y
        :param x2: 终点x
        :param y2: 终点Y
        :param colors: 十六进制颜色,可以从大漠的色彩描述直接获取
        :param types: 0:原来颜色,非零:白色
        :return:
        '''
        image = self.GetCapture(x1, y1, x2, y2)
        colors = colors.split("|")
        masks = np.zeros_like(image[:, :, 0], dtype=np.uint8)
        for color in colors:
            lower_rgb, upper_rgb = self.parse_custom_color(color)
            mask = cv2.inRange(image, lower_rgb, upper_rgb)
            masks += mask
        result = cv2.bitwise_and(image, image, mask=masks)
        if types:
            result[np.where(masks != 0)] = [255, 255, 255]
        return result
    def SetDict(self, lines):
        '''
        初始化字库文件
        :param lines: 字库数组
        :return: 点阵数据
        '''
        des = {}
        for line in lines[1:]:
            parts = line.split("$")
            hex_str = parts[0]
            hex_values = np.array([int(c, 16) for c in hex_str], dtype=np.uint8)
            bin_values = np.unpackbits(hex_values[:, None], axis=1).reshape(-1, 8)[:, 4:]  # 只需要后4位
            num_elements = bin_values.shape[0]
            remainder = num_elements % 11
            if remainder != 0:
                padding = 11 - remainder
                bin_values = np.pad(bin_values, ((0, padding), (0, 0)), mode='constant', constant_values=0)
            bin_values = bin_values.reshape(-1, 11).T
            des.setdefault(parts[1], bin_values)
        return lines[0], des
    def Ocr(self, x1, y1, x2, y2, thd, DIict):
        '''
        点阵文字识别
        :param x1:
        :param y1:
        :param x2:
        :param y2:
        :param thd: 相似度
        :param DIict: 字库
        :return:
        '''
        def filter_points(points, max_distance):
            if points.shape[0]  h and y2 - y1 > 11:
                result = cv2.matchTemplate(value, img, cv2.TM_CCOEFF_NORMED)
                loc = np.column_stack(np.where(result >= thd))
                loc = filter_points(loc, min(6, h))
                if loc.size > 1:
                    py, px = 6 + y1, int(h / 2 + x1)
                    loc += (py, px)
                    loc = loc[:, [1, 0]]
                    res.setdefault(key, loc)
        return res if len(res) > 0 else 0
    def Getocr(self, input_data, x_size=15):
        '''
        解析ocr
        :param input_data:ocr返回值
        :param x_size: x间距,距离小合并到一起
        :return:
        '''
        output = [(key, tuple(row)) for key, arr in input_data.items() for row in arr]
        output.sort(key=lambda x: x[1][1])
        groups = []
        current_group = []
        for i, (char, coord) in enumerate(output):
            if current_group:
                last_coord = current_group[-1][1]
                if abs(coord[1] - last_coord[1])

字库, 大漠

linsixi   

看不懂,感觉很牛逼的样子
Vampiremss   

咋运行不了呢
18834161486
OP
  


Vampiremss 发表于 2025-1-9 14:46
咋运行不了呢

按照格式导入正确的字库数据,选择好范围就好了
lsb2pojie   

感谢分享,可以用来写游戏自动化了
moyc   

不会用,咋办
18834161486
OP
  


moyc 发表于 2025-1-9 15:22
不会用,咋办

B站搜索萌新本炘,有视频教程
xbang   

python可以做这个啊。。。。认知上 就是爬虫啊,。。。。。
18834161486
OP
  


xbang 发表于 2025-1-9 15:31
python可以做这个啊。。。。认知上 就是爬虫啊,。。。。。

嘎嘎好用,现在用python写脚本的挺多的。
longhua23   

解释有点少,没看明白
您需要登录后才可以回帖 登录 | 立即注册

返回顶部