当前位置: 首页 > backend >正文

Python文字识别OCR

  一.引言

文字识别,也称为光学字符识别(Optical Character Recognition, OCR),是一种将不同形式的文档(如扫描的纸质文档、PDF文件或数字相机拍摄的图片)中的文字转换成可编辑和可搜索的数据的技术。随着技术的发展,文字识别技术已经成为信息管理、自动化办公和智能系统的关键组成部分。

二.简介

为了易于集成和使用,我们将文字识别OCR封装为DLL(动态链接库)。这种封装方式不仅保留了算法的性能优势,还提供了跨平台和跨语言的兼容性,目前支持编程语言如下:

  • C++
  • Python
  • 易语言

1.C++头文件

 #ifndef __SN_OCR__H__
#define __SN_OCR__H__#include "windows.h"//返回参数
typedef struct SN_STATU {int code;			//错误码,如果为 0 表示成功,否则表示错误号char message[4096];	//错误信息,如果为 "OK" 表示成功,否则返回错误信息}SN_STATU;/*启动OCR文字识别服务
*
* 参数:
*	[in]  szOnnxFilePath:	设置 onnx 模型文件路径,如果设置为 NULL,默认和 DLL文件同级目录
* 	[out] pResult:			返回错误信息,参数pResult->code(错误码)如果为 0 表示成功,否则表示错误号;
*
* 返回值:成功返回0,失败返回错误号,详细错误信息请参考 pResult
*
*/
int WINAPI apiSNInitOCRServer(char* szOnnxFilePath, SN_STATU* pStatu);/*创建OCR文字识别句柄
*
* 参数:
*	[in]  szKey:		卡密(购买卡密:https://shop.4yuns.com/links/7C9F16B7)
* 	[in]  pOnnxFilePath:设置 onnx 模型文件路径,如果设置为 NULL,默认和 DLL文件同级目录
* 	[out] pResult:		返回错误信息,参数pResult->code(错误码)如果为 0 表示成功,否则表示错误号;
*
* 返回值:成功返回句柄,失败返回NULL
*
*/
HANDLE WINAPI apiSNCreateOCRHandle(char* szKey, char* szOnnxFilePath, SN_STATU* pStatu);/*获取OCR文字识别卡密到期时间
*
* 参数:
*	[in]  handle:		句柄(通过调用apiSNCreateOCRHandle得到)
* 	[out] pResult:		返回错误信息,参数pResult->code(错误码)如果为 0 表示成功,否则表示错误号;
*
* 返回值:返回卡密到期时间,失败返回NULL,错误信息请查看参数 pResult->message
*
*/
char* WINAPI apiSNGetKeyExpiresTime(HANDLE handle, SN_STATU* pResult);/*获取OCR文字识别结果(以json字符串形式返回)
*
* 参数:
*	[in]  handle:			句柄(通过调用apiSNCreateOCRHandle得到)
*  	[in]  szImageFilePath:	图片路径
* 	[out] pResult:			返回错误信息,参数pResult->code(错误码)如果为 0 表示成功,否则表示错误号;
*
* 返回值:返回OCR文字识别结果(以json字符串形式返回),失败返回NULL,错误信息请查看参数 pResult->message
*
*/
char* WINAPI apiSNGetOCRFromImage(HANDLE handle, char* szImageFilePath, SN_STATU* pStatu);/*释放OCR文字识别句柄(释放内存)
*
* 参数:
*	[in] handle:		句柄(通过调用apiSNCreateOCRHandle得到)
*
* 返回值:返回 0 表示成功,其他值表示错误号;
*
*/
int WINAPI apiSNDestroyOCRHandle(HANDLE handle);#endif

2.Python调用dll接口

from ctypes import cdll, c_char_p, Structure, byref
import ctypes# 定义SN_STATU结构体
class SN_STATU(Structure):_fields_ = [("code", ctypes.c_int),("message", c_char_p * 4096)]# 加载DLL
lib = cdll.LoadLibrary('D://SNOCR.dll')# 设置函数参数类型
lib.apiSNInitOCRServer.argtypes = [c_char_p, ctypes.POINTER(SN_STATU)]
lib.apiSNInitOCRServer.restype = ctypes.c_intlib.apiSNCreateOCRHandle.argtypes = [c_char_p, c_char_p, ctypes.POINTER(SN_STATU)]
lib.apiSNCreateOCRHandle.restype = ctypes.c_void_plib.apiSNGetKeyExpiresTime.argtypes = [ctypes.c_void_p, ctypes.POINTER(SN_STATU)]
lib.apiSNGetKeyExpiresTime.restype = c_char_plib.apiSNGetOCRFromImage.argtypes = [ctypes.c_void_p, c_char_p, ctypes.POINTER(SN_STATU)]
lib.apiSNGetOCRFromImage.restype = c_char_plib.apiSNDestroyOCRHandle.argtypes = [ctypes.c_void_p]
lib.apiSNDestroyOCRHandle.restype = ctypes.c_int# 初始化变量
statu = SN_STATU()
key = b"SNKJe9xffLhdFY7r3TcffXq44ThDVcE3BQFQFfVA9VG4"
onnx_path = b"D://SNOCR.onnx"
image_path = b"D://7.jpg"# 1. 启动OCR服务
ret = lib.apiSNInitOCRServer(onnx_path, byref(statu))
if ret < 0:print(f"Error:{statu.message.decode('utf-8')}")exit()# 2. 创建OCR句柄
handle = lib.apiSNCreateOCRHandle(key, onnx_path, byref(statu))
if not handle:print(f"Error:{statu.message.decode('utf-8')}")exit()# 3. 获取卡密到期时间
expires_time = lib.apiSNGetKeyExpiresTime(handle, byref(statu))
if not expires_time:print(f"Error:{statu.message.decode('utf-8')}")exit()
print(f"Expires Time: {expires_time.decode('utf-8')}")# 4. 识别OCR,返回Json字符串
ocr_result = lib.apiSNGetOCRFromImage(handle, image_path, byref(statu))
if not ocr_result:print(f"Error:{statu.message.decode('utf-8')}")exit()
try:print(f"OCR Result: {ocr_result.decode('utf-8')}")
except UnicodeDecodeError:print(f"OCR Result: {ocr_result.decode('GBK')}")# 5. 释放内存
lib.apiSNDestroyOCRHandle(handle)# 等待输入,防止程序直接退出
input("Press Enter to exit...")

三.效果演示

1.图片1

识别效果:

{"type":	0,"task_id":	1,"err_code":	0,"ocr_result":	{"single_result":	[{"single_rate":	0.939104,"left":	102.208336,"top":	41.812500,"right":	329.854156,"bottom":	67.829170,"single_str_utf8":	"中国建设银行"}, {"single_rate":	0.966887,"left":	104.431534,"top":	68.423492,"right":	309.992828,"bottom":	84.602386,"single_str_utf8":	"China Construction Bank"}, {"single_rate":	0.968900,"left":	102.672920,"top":	96.168755,"right":	403.258331,"bottom":	111.964584,"single_str_utf8":	"龙卡通(储蓄卡)LONG CARD(DEBIT CARD)"}, {"single_rate":	0.975151,"left":	41.781921,"top":	137.955643,"right":	410.251556,"bottom":	164.107880,"single_str_utf8":	"6227 0033 2069 0222 205"}, {"single_rate":	0.935433,"left":	20.770407,"top":	210.668716,"right":	77.230583,"bottom":	230.122101,"single_str_utf8":	"ATM"}, {"single_rate":	0.960131,"left":	103.137505,"top":	185.368759,"right":	192.337509,"bottom":	207.204163,"single_str_utf8":	"CCB GZ"}, {"single_rate":	0.929293,"left":	338.376495,"top":	201.118103,"right":	417.111450,"bottom":	224.273529,"single_str_utf8":	"UnionPa"}, {"single_rate":	0.917808,"left":	367.485413,"top":	220.677078,"right":	413.479156,"bottom":	239.260422,"single_str_utf8":	"银联"}],"unknown_1":	446,"unknown_2":	280}
}

2.图片2

识别效果:

{"type":	0,"task_id":	1,"err_code":	0,"ocr_result":	{"single_result":	[{"single_rate":	0.919637,"left":	622.061157,"top":	123.251556,"right":	1046.638920,"bottom":	190.015121,"single_str_utf8":	"马托13610000670"}, {"single_rate":	0.996936,"left":	40.618664,"top":	324.310150,"right":	541.513184,"bottom":	371.843231,"single_str_utf8":	"广州利驰服装有限公司"}, {"single_rate":	0.997014,"left":	624.066650,"top":	218.300000,"right":	1040.933350,"bottom":	242.966675,"single_str_utf8":	"地址:广州市海珠区赤岗路173号"}, {"single_rate":	0.964238,"left":	624.066650,"top":	251.600000,"right":	844.833374,"bottom":	276.266663,"single_str_utf8":	"金丰大厦503室"}, {"single_rate":	0.979285,"left":	625.300000,"top":	286.133331,"right":	1044.633300,"bottom":	309.566681,"single_str_utf8":	"厂址:广州市海珠区赤岗西路232"}, {"single_rate":	0.996300,"left":	624.066650,"top":	318.200000,"right":	759.733337,"bottom":	341.633331,"single_str_utf8":	"号-234号"}, {"single_rate":	0.991057,"left":	624.066650,"top":	351.500000,"right":	925,"bottom":	374.933350,"single_str_utf8":	"热线:400-688-7260"}, {"single_rate":	0.964125,"left":	625.239319,"top":	381.016510,"right":	922.600220,"bottom":	404.499695,"single_str_utf8":	"电话:020-84022958"}, {"single_rate":	0.993601,"left":	624.066650,"top":	408.233337,"right":	923.766663,"bottom":	432.900000,"single_str_utf8":	"传真:020-84022572"}, {"single_rate":	0.950434,"left":	625.203430,"top":	438.767609,"right":	1107.616580,"bottom":	464.666626,"single_str_utf8":	"邮箱:kunhemwl@yahoo.com.cn"}, {"single_rate":	0.962023,"left":	624.066650,"top":	471.133331,"right":	1001.466670,"bottom":	494.566681,"single_str_utf8":	"网址:www.hxkunhe.com"}],"unknown_1":	1184,"unknown_2":	614}
}

四.常见问题

1.是否支持多线程

支持

五.更新日志

  • 2024.02.06 c++ 模拟人工鼠标轨迹demo
  • 2024.06.06 python 模拟人工鼠标轨迹demo
  • 2024.06.25 新增错误日志信息
  • 2024.07.15 优化水平/垂直轨迹
  • 2024.08.20 优化部分轨迹可能出现负数的问题
  • 2024.09.19 优化部分轨迹延迟时间为0的情况(可能会造成鼠标瞬移)
  • 2024.09.21 修复部分水平/垂直轨迹出现负数的情况
  • 2024.09.28 新增易语言demo
  • 2024.11.01 修改接口,兼容易语言代码
  • 2024.11.17 支持移动轨迹为相对坐标(默认是轨迹是绝对坐标)
  • 2024.12.15 新增文字识别OCR,支持编程语言如下:
    • Python
    • 易语言
    • C语言
    • C++
  • 2024.12.22 优化鼠标轨迹
    • 新增滑块轨迹
    • 优化鼠标轨迹 - 支持密度调节
  • 2024.12.29
    • 修复鼠标轨迹可能会崩溃的问题
    • 修复OCR文字识别失败问题(带有中文路径的图片)
  • 2025.06.08 
    • 1.优化鼠标轨迹数据
    • 2.优化卡密验证耗时问题
    • 3.优化OCR文字识别耗时问题

六.云盘源码下载

  • 百度云盘
  • 夸克云盘
  • 123云盘
http://www.xdnf.cn/news/19239.html

相关文章:

  • 蓓韵安禧活性叶酸优生优育守护者
  • CSS基础学习第二天
  • 简说DDPM
  • 【系列07】端侧AI:构建与部署高效的本地化AI模型 第6章:知识蒸馏(Knowledge Distillation
  • 监听nacos配置中心数据的变化
  • vector的学习和模拟
  • 桌面GIS软件添加设置牵引文字标注
  • Fortran二维数组去重(unique)算法实战
  • 电子健康记录风险评分与多基因风险评分的互补性与跨系统推广性研究
  • 福彩双色球第2025100期篮球号码分析
  • GESP5级2024年03月真题解析
  • Coze源码分析-API授权-获取令牌列表-后端源码
  • UNet改进(36):融合FSATFusion的医学图像分割
  • TensorFlow 面试题及详细答案 120道(71-80)-- 性能优化与调试
  • Next.js 快速上手指南
  • 数值分析——算法的稳定性
  • 【ACP】2025-最新-疑难题解析- 练习二汇总
  • 文档转换总出错?PDF工具免费功能实测
  • Docker 部署深度网络模型(Flask框架思路)
  • Intellij IDEA社区版(下载安装)
  • 项目管理方法全流程解析
  • HarmonyOS 持久化存储:PersistentStorage 实战指南
  • 详解推测性采样加速推理的算法逻辑
  • nginx配置websock请求,wss
  • java中的VO、DAO、BO、PO、DO、DTO
  • 【重学 MySQL】九十三、MySQL的字符集的修改与底层原理详解
  • 项目管理和产品管理的区别
  • 【gflags】安装与使用
  • 2025 批量下载雪球和东方财富帖子和文章导出excel和pdf
  • 一体化步进伺服电机在视觉检测设备中的应用案例