aardio 与python交互进行OCR识别

文周周的 18小时前 19

 

尝试用 Aardio 编写了一个简单的 OCR 图片识别工具,主要功能是选择图片后将其转为灰度图,再进行文字识别并显示结果。整个过程涉及 Aardio 的界面设计、Python 交互以及 OCR 识别功能的整合。

一、代码

界面设计模块

import win.ui;
/*DSG{{*/
var winform = win.form(text="与python交互OCR识别";right=770;bottom=470)
winform.add(
Activate_OCR={cls="button";text="开始识别";left=600;top=390;right=700;bottom=440;z=4};
Image_Selection={cls="button";text="选择图片";left=260;top=310;right=360;bottom=350;z=3};
edit={cls="edit";left=3;top=3;right=760;bottom=240;edge=1;multiline=1;vscroll=1;z=1};
edit2={cls="edit";left=2;top=310;right=230;bottom=350;edge=1;multiline=1;z=2}
)
/*}}*/

winform.show();

选择图片模块

//选择图片文件
import fsys.dlg 
winform.Image_Selection.oncommand = function(id,event){
    winform.edit2.text = fsys.dlg.open("图片文件|*.jpg;*.png;*.jpeg;*.gif;*.bmp||","请选择要识别的图片");
}

创建python方法模块

import py3.10
var path = winform.edit2.text;

//创建python方法
var interact = function(){

    
    var pyCode = /** 
import cv2
import os
import pathlib

def Img():
    input_path =  r'_PATH_'
    
    # 检查输入文件是否存在
    if not os.path.exists(input_path):
        return (f"错误:输入文件不存在 - {input_path}")
    
    # 动态生成输出路径(在原文件名后添加_gray后缀)
    input_file = pathlib.Path(input_path)
    output_path = str(input_file.parent / (input_file.stem + '_gray' + input_file.suffix))

    # 读取图像
    image = cv2.imread(input_path)

    # 检查图像是否成功加载
    if image is None:
        return (f"错误:无法加载图像 - {input_path}")

    # 转为灰度图
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # 保存灰度图
    save_success = cv2.imwrite(output_path, gray)

    # 检查保存是否成功
    if not save_success:
        return ("错误:无法保存图像")

    return output_path

    
**/
    // 正确替换占位符为实际路径值
    var path = winform.edit2.text;
    
    path = string.replace(path, "\\", "\\\\");
    
    pyCode = string.replace(pyCode, "_PATH_", path);
  
    return pyCode; 
}

OCR识别方法模块

//创建OCR识别方法
var OCRfunction = function(){
    if(winform.edit.text == 0){
        import string.ocrLite
        import string.ocrLite.defaultModels;
        
        py3.exec(interact()) 
        var grayImagePath = py3.main.Img();
        
        var ocr = string.ocrLite ();
        
        if (grayImagePath) {
            var bit = gdip.bitmap(grayImagePath);
            
            if (bit) {
                var text = ocr.detectBitmap(bit);
                
                for(i, value in table.eachIndex(text.blocks)){
                    winform.edit.print(value.text)
                }
                
                bit.dispose();
            }
        }
    } else {
        if (grayImagePath) {
            fsys.remove(grayImagePath);
        }
        
        winform.edit.text = '';
    }
}

事件绑定与校验模块

//开始识别图片文字
winform.Activate_OCR.oncommand = function(id,event){
    
    import fsys;
    if(winform.edit2.text == 0){
        return winform.edit2.showErrorTip("请指定正确的图片路径","OCR识别程序") 
    }
    else {

        if(!fsys.isFile(winform.edit2.text)){
            return winform.edit2.showErrorTip("图片文件不存在,请检查路径","OCR识别程序") 
        }
        
        var fileExt = fsys.getExtensionName(winform.edit2.text);
        var fileExt = '.' + fileExt; 
        var imageExtensions = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp"};
        if( ! table.find(imageExtensions, fileExt)){
            return winform.edit2.showErrorTip("请指定正确的图片格式","OCR识别程序")     
        }
            
    }
    OCRfunction();        
}

win.loopMessage();

功能展示

截图
截图

aardio 新手交流学习群,一起学习的进

qq群号:697197055
微信群加我 _825720xw 拉你

 


最新回复 (0)
返回