PaddleOCR之高性能Go语言实现OCR识别
最近为了让python语言能够直接调用PaddleOCR的C++的动态链接库,针对本人已经开源的PaddleOCR项目https://gitee.com/raoyutian/paddle-ocrsharp使用的PaddleOCR的C++动态库,进行了大量代码修改,修改后PaddleOCR,导出标准C函数接口,极大地方便了其他语言直接调用并进行OCR文字识别。
__declspec(dllexport) void Initializejson(char* modelPath_det_infer, char* modelPath_cls_infer, char* modelPath_rec_infer, char* keys, char* parameterjson);
__declspec(dllexport) char* Detect(char* imagefile);
__declspec(dllexport) char* DetectByte(char* imagebytedata, size_t* size);
__declspec(dllexport) char* DetectBase64(char* imagebase64);
__declspec(dllexport) char* DetectByteData(const char* img, int nWidth, int nHeight, int nChannel);
__declspec(dllexport) void FreeEngine();
本文将介绍python ,go ,c#几种开发语言的识别结果。
一 、pyhon:
import os
import ctypes
import Parameter
from ctypes import *
import json
from datetime import datetime
import numpy as np
paddleOCR=cdll.LoadLibrary(".\PaddleOCR.dll")#加载C++动态库
encode="gbk"
#传入OCR模型参数
root="./"
cls_infer =root+"/inference/ch_ppocr_mobile_v2.0_cls_infer"
rec_infer = root+"/inference/ch_PP-OCRv3_rec_infer"
det_infer = root+"/inference/ch_PP-OCRv3_det_infer"
ocrkeys = root+"/inference/ppocr_keys.txt"
#OCR识别参数对象,后面序列化为json字符串
parameter=Parameter.Parameter()
p_cls_infer=cls_infer.encode(encode)
p_rec_infer=rec_infer.encode(encode)
p_det_infer=det_infer.encode(encode)
p_ocrkeys=ocrkeys.encode(encode)
def main():
#序列化参数为json字符串
parameterjson= json.dumps(parameter,default=Parameter.Parameter2dict)
#初始化OCR引擎,一次即可
paddleOCR.Initializejson( p_det_infer, p_cls_infer, p_rec_infer, p_ocrkeys, parameterjson.encode(encode))
result=""
paddleOCR.Detect.restype = ctypes.c_wchar_p #识别结果是宽字节编码,
imagepath=os.path.abspath('.')+"\\image\\"
imagefiles=os.listdir(imagepath)
total=[]
for image in imagefiles:
imagefile=imagepath+image
t1= datetime.utcnow()
#调用OCR识别接口,调用的是文件路径接口
result= paddleOCR.Detect(imagefile.encode(encode))
t2=datetime.utcnow()
c=t2-t1
total.append(c)
print("time:",c)
print(result)
print("平均时间:", np.mean(total))
if __name__=="__main__":
main()
input()
Python直接调用C++的动态库进行OCR识别,相比python调用python的预测库进行OCR,性能提升了不少。

二、Go:
package main
import (
"fmt"
"syscall"
"unsafe"
"os"
"bufio"
"C"
)
// 获取字符串的长度指针
func lenPtr(s string) uintptr {
return uintptr(len(s))
}
// 获取数字的指针
func intPtr(n int) uintptr {
return uintptr(n)
}
// 获取字符串的指针
func strPtr(s string) uintptr {
return uintptr(unsafe.Pointer(syscall.StringBytePtr(s)))
}
func main() {
dll,err:= syscall.LoadDLL("PaddleOCR.dll")
if err!=nil {
fmt.Println(err)
return
}
Initjson,err:=dll.FindProc("Initializejson")
if err!=nil {
fmt.Println(err)
return
}
detect,err:=dll.FindProc("Detect")
if err!=nil {
fmt.Println(err)
return
}
Initjson.Call(strPtr("D:\\PaddleOCR\\deploy\\Go\\inference\\ch_PP-OCRv3_det_infer"),
strPtr("D:\\PaddleOCR\\deploy\\Go\\inference\\ch_ppocr_mobile_v2.0_cls_infer"),
strPtr("D:\\PaddleOCR\\deploy\\Go\\inference\\ch_PP-OCRv3_rec_infer"),
strPtr("D:\\PaddleOCR\\deploy\\Go\\inference\\ppocr_keys.txt"),strPtr("{}")) res, _, _:=detect.Call(strPtr("D:\\PaddleOCR\\deploy\\Go\\image\\test.jpg"))
p_result := (*C.char)(unsafe.Pointer(res))
ocrresult:= C.GoString(p_result)
fmt.Println(ocrresult) input := bufio.NewScanner(os.Stdin)
input.Scan()
}
go语言现学现卖。Go实现,主要采用CGo,syscall.LoadDLL("PaddleOCR.dll")
三、C#
c#语言调用C++动态库,直接采用DllImport方法。
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern void Initialize(string det_infer, string cls_infer, string rec_infer, string keys, OCRParameter parameter);
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern void Initializejson(string det_infer, string cls_infer, string rec_infer, string keys, string parameterjson);
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr Detect(string imagefile);
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr DetectByte(byte[] imagebytedata, long size);
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr DetectBase64(string imagebase64);
[DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern int FreeEngine();

开源项目地址: https://gitee.com/raoyutian/paddle-ocrsharp
更多内容,欢迎关注公众号,加入QQ群,了解更多内容。




PaddleOCR之高性能Go语言实现OCR识别的更多相关文章
- 【Win10 应用开发】OCR识别
OCR,即Optical Character Recognition,光学字符识别.以下介绍来自搜索: OCR(Optical Character Recognition,光学字符识别)是指电子设备( ...
- Atitit ocr识别原理 与概论 attilax总结
Atitit ocr识别原理 与概论 attilax总结 1.1. Ocr的过程与流程1 1.2. OCR不同技术细分略有不同,但大概原理是一样的. 即主要技术过程是:二值化(又叫归一化)----- ...
- 基于Python实现对PDF文件的OCR识别
http://www.jb51.net/article/89955.htm https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-pytho ...
- OCR识别-python版(一)
需求:识别图片中的文字信息环境:windows系统 开发语言:python 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.pyocr 网络通直接使用命令:pip ...
- 以API方式调用C# dll,使用OneNote2013 sp1实现OCR识别本地图片
http://www.cnblogs.com/Charltsing/p/OneNoteOCRAPI.html OneNote2013 OCR API调用使用说明2019.4.17 使用说明:1.安装干 ...
- AI时代的OCR识别技术浅析
人工智能这个词可谓是耳熟能详,近几年人工智能热潮再次席卷而来,引起轰动的要数google的AlphaGo,相继打败了围棋界的韩国选手李世石以及世界冠军柯洁,见证了人工智能发展的里程碑式的变革,人工智能 ...
- OCR识别-python3.5版
刚接触,啥子都不会,按着教程走 需求:识别图片中的文字信息环境:windows系统 开发语言:python3.5 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.p ...
- 基于百度云的OCR识别(Python)
2019年7月3日早上,在百度AI开发者大会上,一个来自山西的青年,将一瓶矿泉水浇在了同样来自山西的李彦宏身上. 可以回顾一下 https://b23.tv/av57665929/p1 ,着实让人一惊 ...
- 百度DMLC分布式深度机器学习开源项目(简称“深盟”)上线了如xgboost(速度快效果好的Boosting模型)、CXXNET(极致的C++深度学习库)、Minerva(高效灵活的并行深度学习引擎)以及Parameter Server(一小时训练600T数据)等产品,在语音识别、OCR识别、人脸识别以及计算效率提升上发布了多个成熟产品。
百度为何开源深度机器学习平台? 有一系列领先优势的百度却选择开源其深度机器学习平台,为何交底自己的核心技术?深思之下,却是在面对业界无奈时的远见之举. 5月20日,百度在github上开源了其 ...
- 基于Tesseract的OCR识别小程序
一.背景 先说下开发背景,今年有次搬家找房子(2020了应该叫去年了),发现每天都要对着各种租房广告打很多电话.(当然网上也找了实地也找),每次基本都是对着墙面看电话号码然后拨打,次数一多就感觉非常麻 ...
随机推荐
- jenkins 持续集成和交付——安装与账户安全还有凭证(二)
前言 jenkins 整理完毕,共二十四章,逐步放出,互相交流学习.学会jenkins 只是第一步,真正的还是多写脚本,然后遇到构建过程的坑,然后解决. 正文 安装jenkins 首先是如何安装jen ...
- 通过一个非常简单的SSM项目来将SpringMVC配置整理清晰。
所有的文件在Git上面都能找到,由于把代码搞上来看的很不舒服,结构不清晰. 第一步:确定环境 IDEA MySQL 5.7.19 Tomcat 9 Maven 3.6 第二步:创建数据库 参考GIT上 ...
- FasterViT:英伟达提出分层注意力,构造高吞吐CNN-ViT混合网络 | ICLR 2024
论文设计了新的CNN-ViT混合神经网络FasterViT,重点关注计算机视觉应用的图像吞吐能力.FasterViT结合CNN的局部特征学习的特性和ViT的全局建模特性,引入分层注意力(HAT)方法在 ...
- CF1832B Maximum Sum 题解
[题目描述] 给定一个长度为 \(n\) 的数列,其中每个元素互不相同,进行 \(k\) 次操作,每次可以选择删除序列中最小的两个数或最大的一个数.求操作后剩余数的和的最大值. [思路] 我们构造一组 ...
- 爱奇艺在 Dubbo 生态下的微服务架构实践
简介: 本文整理自作者于 2020 年云原生微服务大会上的分享<爱奇艺在 Dubbo 生态下的微服务架构实践>,重点介绍了爱奇艺在 Dubbo.Sentinel 等开发框架方面的使用经验以 ...
- 重回铁王座!时隔5年!Quill 2.0 终于发布啦🎉
你好,我是 Kagol. 2024年4月17日,Quill 2.0 正式发布 最后一个 1.0 版本 1.3.7 发布于 2019年9月9日,时隔4年零7个月. 富文本编辑器拥有非常丰富的使用场景,我 ...
- dotnet 读 WPF 源代码笔记 聊聊 HwndWrapper.GetGCMemMessage 调试消息
我在阅读 WPF 源代码,在 HwndWrapper 的静态构造函数看到了申请了 HwndWrapper.GetGCMemMessage 这个 Windows 消息,好奇这个消息是什么功能的.通过阅读 ...
- Azure 无服务器 Function 函数计算服务 dotnet core 3.1 创建和部署入门
本文用的是 世纪互联 的 Azure.cn 版本,这个版本因为是在国内,所以网速会快超级超级多.使用 世纪互联 的版本需要一块钱哦,用一块钱就能进入一个月的免费试用.本文主要告诉小伙伴如何使用 Azu ...
- 2018-4-15-WPF-在-Alt+Tab-隐藏窗口
title author date CreateTime categories WPF 在 Alt+Tab 隐藏窗口 lindexi 2018-04-15 10:13:40 +0800 2018-3- ...
- 实验8 #第8章 Verilog有限状态机设计-2 #Verilog #Quartus #modelsim
2. 汽车尾灯控制器 2.1 实验要求:设计一个汽车尾灯控制电路. (1)功能:汽车左右两侧各有3个尾灯,要求控制尾灯按如下规则亮灭. 汽车沿直线行驶时,两侧指示灯全灭. 右转弯时,左侧的指示灯全灭, ...