Skip to content

OCR相关函数

函数列表

1. 创建一个飞桨OCR实例对象 (PaddleOcr.new)

说明: 创建一个飞桨OCR实例对象

函数: PaddleOcr.new()

参数: 无

返回值:

返回值类型说明
userdata返回一个userdata对象

示例:

lua
local paddle = PaddleOcr.new()
if paddle ~= nil then
	print("创建成功")
else
	print("创建失败")
end

2. 加载懒人自带的飞桨模型 (PaddleOcr.loadModel)

说明: 加载懒人自带的飞桨模型

函数: PaddleOcr.loadModel(isUseOnnx)

参数:

参数名类型说明
isUseOnnx布尔值如果为true表示使用onnx模型去识别,否则使用ncnn模型识别

返回值:

返回值类型说明
boolean返回一个布尔类型

示例:

lua
function loadModel(useOnnx)
	local paddle = PaddleOcr.new()
	if paddle ~= nil then
		local ret = paddle:loadModel(useOnnx)
		if ret then
			return paddle
		end
	else
		print("创建失败")
	end
	return nil
end

local paddleOnnx = loadModel(true)

if paddleOnnx ~= nil then
	print("加载onnx模型成功")
end

local paddleNcnn = loadModel(false)

if paddleNcnn ~= nil then
	print("加载ncnn模型成功")
end

3. 加载用户自己的onnx模型 (PaddleOcr.loadOnnx)

说明: 加载用户自己的onnx模型

函数: PaddleOcr.loadOnnx(modelDetPath, modelClsPath, modelRecPath, keyTxt)

参数:

参数名类型说明
modelDetPath字符串检测模型绝对路径
modelClsPath字符串方向分类器模型路径
modelRecPath字符串文本识别模型路径
keyTxt字符串训练的文本集绝对路径

返回值:

返回值类型说明
boolean布尔类型

示例:

lua
local workpath = getWorkPath()
local modelDetPath = workpath .. "/det.onnx"
local modelClsPath = workpath .. "/cls.onnx"
local modelRecPath = workpath .. "/rec.onnx"
local keyTextPath = workpath .. "/keys.txt"
extractAssets("飞桨例子.rc" , workpath , "*.*")
local paddle = PaddleOcr.new()
if paddle ~= nil then
	local ret = paddle:loadOnnx(modelDetPath , modelClsPath , modelRecPath , keyTextPath)
	if ret then
		print("加载成功")
	else
		print("加载失败")
	end
end

4. 加载用户自己的ncnn模型 (PaddleOcr.loadNcnn)

说明: 加载用户自己的ncnn模型

函数: PaddleOcr.loadNcnn(detParams, recParams, detBin, recBin, keyTxt)

参数:

参数名类型说明
detParams字符串检测模型算子描述文件绝对路径
recParams字符串文本模型算子描述文件绝对路径
detBin字符串检测模型文件绝对路径
recBin字符串文本识别模型绝对路径
keyTxt字符串训练的文本集绝对路径

返回值:

返回值类型说明
boolean布尔类型

示例:

lua
local workpath = getWorkPath()
local detParams = workpath .. "/det.param"
local recParams = workpath .. "/rec.param"
local detBin = workpath .. "/det.bin"
local recBin = workpath .. "/rec.bin"
local keyTextPath = workpath .. "/keys.txt"
extractAssets("飞桨例子.rc" , workpath , "*.*")
local paddle = PaddleOcr.new()
if paddle ~= nil then
	local ret = paddle:loadNcnn(detParams , recParams , detBin ,recBin, keyTextPath)
	if ret then
		print("加载成功")
	else
		print("加载失败")
	end
end

5. 区域检测文本 (PaddleOcr.detect)

说明: 区域检测文本

函数: PaddleOcr.detect(x1, y1, x2, y2)

参数:

参数名类型说明
x1整数区域左上角x坐标
y1整数区域左上角y坐标
x2整数区域右下角x坐标
y2整数区域右下角y坐标

返回值:

返回值类型说明
字符串json字符串

示例:

lua
function loadModel(useOnnx)
	local paddle = PaddleOcr.new()
	if paddle ~= nil then
		local ret = paddle:loadModel(useOnnx)
		if ret then
			return paddle
		end
	else
		print("创建失败")
	end
	return nil
end

local paddleOnnx = loadModel(true)

if paddleOnnx ~= nil then
	print("加载onnx模型成功")
	local ret = paddleOnnx:detect(37 , 219 , 372 , 273)
	print(ret)
end

local paddleNcnn = loadModel(false)

if paddleNcnn ~= nil then
	print("加载ncnn模型成功")
	local ret = paddleNcnn:detect(37 , 219 , 372 , 273)
	print(ret)
end

6. 根据模型识别图片增强版 (PaddleOcr.detectWithPadding)

说明: 区域检测文本

函数: PaddleOcr.detectWithPadding(image, padding, r, g, b)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
padding整数期望目标图标周围填充空白大小(单位是像素)
r整数是空白区域的红色通道
g整数区域右下角绿色通道
b整数区域右下角蓝色通道

返回值:

返回值类型说明
字符串json字符串

示例:

lua
function loadModel(useOnnx)
	local paddle = PaddleOcr.new()
	if paddle ~= nil then
		local ret = paddle:loadModel(useOnnx)
		if ret then
			return paddle
		end
	else
		print("创建失败")
	end
	return nil
end

local image = QImage.new()
if image:snapShot(37,219,372,273) == false then
	print("截图失败")
	exitScript()
end
local paddleOnnx = loadModel(true)

if paddleOnnx ~= nil then
	print("加载onnx模型成功")
	local ret = paddleOnnx:detectWithPadding(image,50,255,255,255)
	print(ret)
end

local paddleNcnn = loadModel(false)

if paddleNcnn ~= nil then
	print("加载ncnn模型成功")
	local ret = paddleNcnn:detectWithPadding(image,50,255,255,255)
	print(ret)
end

7. 设置字库文件 (setDict)

说明: 设置字库文件

函数: setDict(index, name)

参数:

参数名类型说明
index整数需要设置的字典的索引目前取值范围0-29
name字符串字典的名称,目前字典只能放入资源文件中

返回值:

返回值类型说明
整数0失败1成功

示例:

lua
setDict(0,"dict.txt")

8. 使用字库 (useDict)

说明: 设置要查找的字典索引,后面所有的查询都是在这个字典中查找

函数: useDict(index)

参数:

参数名类型说明
index整数需要设置的字典的索引目前取值范围0-29

返回值:

返回值类型说明
整数0失败1成功

示例:

lua
useDict(0)

9. 点阵ocr识别 (ocr)

说明: 根据选中的字典查找该区域内所有文字组成的字符串

函数: ocr([image], x1, y1, x2, y2, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串返回查找的字符串

示例:

lua
setDict(0 , "dict.txt")
useDict(0)

function testOcr(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return nil
			end
		else
			return nil
		end
	end
	local str = nil
	if img ~= nil then
		str = ocr(img , 37 , 164 , 273 , 213 , "222222-101010" , 0.9 , 10 , 10)
	else
		str = ocr(37 , 164 , 273 , 213 , "222222-101010" , 0.9 , 10 , 10)
	end
	print(str)
	return str
end

testOcr(false)
testOcr(true)

10. 点阵ocr识别 (ocrNew)

说明: 根据选中的字典查找该区域内所有文字组成的字符串

函数: ocrNew([image], index, x1, y1, x2, y2, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
index整数字库的索引
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串返回查找的字符串

示例:

lua
setDict(0 , "dict.txt")

function testOcrNew(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return nil
			end
		else
			return nil
		end
	end
	local str = nil
	if img ~= nil then
		str = ocrNew(img,0, 37 , 164 , 273 , 213 , "222222-101010" , 0.9 , 10 , 10)
	else
		str = ocrNew(0,37 , 164 , 273 , 213 , "222222-101010" , 0.9 , 10 , 10)
	end
	print(str)
	return str
end

testOcrNew(false)
testOcrNew(true)

11. 返回区域范围内所有识别文字的坐标 (ocrj)

说明: 根据选中的字典查找该区域内所有文字和坐标

函数: ocrj([image], x1, y1, x2, y2, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串json格式的字符串

示例:

lua
setDict(0 , "dict.txt")
useDict(0)

function testOcrJ(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return nil
			end
		else
			return nil
		end
	end
	local str = nil
	if img ~= nil then
		str = ocrj(img,37,164,273,213,"222222-101010",0.9,10,10)
	else
		str = ocrj(37,164,273,213,"222222-101010",0.9,10,10)
	end
	print(str)
	return str
end

testOcrJ(false)
testOcrJ(true)

12. 返回区域范围内所有识别文字的坐标 (ocrjNew)

说明: 根据选中的字典查找该区域内所有文字和坐标

函数: ocrjNew([image], index, x1, y1, x2, y2, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
index整数字库的索引
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串json格式的字符串

示例:

lua
setDict(0 , "dict.txt")

function testOcrJNew(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return nil
			end
		else
			return nil
		end
	end
	local str = nil
	if img ~= nil then
		str = ocrjNew(img,0,37,164,273,213,"222222-101010",0.9,10,10)
	else
		str = ocrj(0,37,164,273,213,"222222-101010",0.9,10,10)
	end
	print(str)
	return str
end

testOcrJNew(false)
testOcrJNew(true)

13. 区域文字查找 (findStr)

说明: 区域文字查找

函数: findStr([image], x1, y1, x2, y2, text, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
text字符串要找的文字,多个图片用"
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数如果有多个颜色用"
整数对应找到的x坐标
整数对应找到的y坐标

示例:

lua
setDict(0 , "dict.txt")
useDict(0)

function testFindStr(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return
			end
		else
			return
		end
	end
	if img ~= nil then
		local ret , x , y = findStr(img,37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		print(ret , x , y)
	else
		local ret , x , y = findStr(37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		print(ret , x , y)
	end
end

testFindStr(false)
testFindStr(true)

14. 区域文字查找 (findStrNew)

说明: 区域文字查找

函数: findStrNew([image], index, x1, y1, x2, y2, text, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
index整数字库的索引
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
text字符串要找的文字,多个图片用"
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数如果有多个颜色用"
整数对应找到的x坐标
整数对应找到的y坐标

示例:

lua
setDict(0 , "dict.txt")

function testFindStrNew(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return
			end
		else
			return
		end
	end
	if img ~= nil then
		local ret , x , y = findStrNew(img,0,37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		print(ret , x , y)
	else
		local ret , x , y = findStrNew(0,37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		print(ret , x , y)
	end
end

testFindStrNew(false)
testFindStrNew(true)

15. 区域文字查找高级版 (findStrEx)

说明: 区域文字查找

函数: findStrEx(x1, y1, x2, y2, text, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
text字符串要找的文字,多个图片用"
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串返回一个json数组字符串

示例:

lua
setDict(0 , "dict.txt")
useDict(0)

function testFindStrEx(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return
			end
		else
			return
		end
	end
	if img ~= nil then
		local str = findStrEx(img , 37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		if str~= nil then
			print(str)
		end
	else
		local str = findStrEx(37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		if str~= nil then
			print(str)
		end
	end
end

testFindStrEx(false)
testFindStrEx(true)

16. 区域文字查找高级版 (findStrExNew)

说明: 区域文字查找

函数: findStrExNew([image], index, x1, y1, x2, y2, text, colorfmt, sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
index整数字库的索引
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
text字符串要找的文字,多个图片用"
colorfmt字符串文字的颜色格式
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
字符串返回一个json数组字符串

示例:

lua
setDict(0 , "dict.txt")

function testFindStrExNew(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if img:snapShot() == false then
				return
			end
		else
			return
		end
	end
	if img ~= nil then
		local str = findStrExNew(img ,0, 37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		if str~= nil then
			print(str)
		end
	else
		local str = findStrExNew(0,37 , 164 , 273 , 213 , "打|开" , "222222-101010" , 0.9 , 10 , 10)
		if str~= nil then
			print(str)
		end
	end
end

testFindStrExNew(false)
testFindStrExNew(true)

17. 创建本地光学ocr对象 (createOcr)

说明: 创建一个tresseract-ocr(4.1.0)本地光学ocr对象

函数: createOcr(lang, [mode], [engine])

参数:

参数名类型说明
lang字符串字库语言文件名称,不填则默认eng(只支持一般的中英+数字+标点符号),注意lang也可以是字库文件的绝对路径
mode整数0仅检测方向和文本,1自动检测方向和文本(OSD),2自动检测,但不进行OSD或OCR处理,3自动PSM模式(但不含OSD),4所识别图片的字体大小不一,5所识别图片当作整块竖向文字区域,6所识别图片当作整块横向文字区域(默认值),7所识别图片当作一行文字,8所识别图片当作单个词语,9所识别图片当作单个圆型围绕的词语,10所识别图片当作单个英文/数字字符,11尽可能识别更多的字符(无顺序要求),12分散稀疏的OSD检测,可选参数
engine整数0 OEM_TESSERACT_ONLY老版本训练引擎,1 OEM_LSTM_ONLY新版本基于神经网络的训练引擎,3 OEM_DEFAULT默认引擎,可选参数

返回值:

返回值类型说明
句柄一个tresseract-ocr对象句柄

示例:

lua
--lang不填写表示使用自带的英文字库
local handle = createOcr("",3,3)
if handle then
	print("用自带的英文字库创建光学ocr成功")
	releaseOcr(handle)
else
	print("用自带的英文字库创建光学ocr失败")
end

--先把chi_sim.traineddata 添加到资源文件中
local handleChi = createOcr("chi_sim",3,3)
if handleChi then
	print("加载资源文件中的中文字库创建ocr成功")
	releaseOcr(handleChi)
else
	print("加载资源文件中的中文字库创建ocr失败")
end

--先把资源里面的chi_sim.traineddata 释放到工作目录
local workpath = getWorkPath()
extractAssets("llop.rc" , workpath , "*.*")
local chipath = workpath .. "/chi_sim.traineddata"
local handleChi1 = createOcr(chipath,3,3)
if handleChi1 then
	print("加载本地文件中的中文字库创建ocr成功")
else
	print("加载本地文件中的中文字库创建ocr失败")
end

18. 释放本地光学ocr对象 (releaseOcr)

说明: 释放指定句柄的tresseract-ocr对象

函数: releaseOcr(handle)

参数:

参数名类型说明
handle句柄通过createOcr创建的句柄

返回值: 无

示例:

lua
local handle = createOcr("chi-sim")
local text = ocrText(handle,0,0,0,0,"ffffff-0f0f0f")
if text ~= nil then
	print("ocr text:"..text)
end
releaseOcr(handle)

19. 设置本地光学ocr白名单 (setWhiteList)

说明: 设置本地光学ocr白名单

函数: setWhiteList(handle, whitelist)

参数:

参数名类型说明
handle句柄通过createOcr创建的句柄
whitelist字符串不在这个字符集里面的字符全部被过滤掉,说明:如果你选择的光学ocr识别引擎使用的是lstm神经网络模式那么白名单将失效

返回值:

返回值类型说明
boolean布尔类型

示例:

lua
local handle = createOcr("eng",3,3)
if handle ~= nil then
	setWhiteList(handle,"123456789.")
	local ret = ocrText(handle,355,940,557,993,"222222-202020|535353-202020")
	if ret ~= nil then
		print(ret)
	end
	releaseOcr(handle)
end

20. 本地光学ocr识别 (ocrText)

说明: 获取指定范围内的文本字符串

函数: ocrText(handle, [image], x1, y1, x2, y2, colorfmt)

参数:

参数名类型说明
handle整数tresseract-ocr对象句柄
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式

返回值:

返回值类型说明
字符串返回查找的字符串

示例:

lua
function testOcrText(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if not img:snapShot() then
				print("截图失败")
				return
			end
		end
	end
	local handle = createOcr("eng" , 3 , 3)
	if handle ~= nil then
		setWhiteList(handle , "123456789.")
		local ret = nil
		if img ~= nil then
			ret = ocrText(handle , img , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		else
			ret = ocrText(handle , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		end
		if ret ~= nil then
			print(ret)
		end
		releaseOcr(handle)
	end
end

21. 本地光学ocr识别返回坐标 (ocrTextEx)

说明: 获取指定范围内的文本字符串和坐标

函数: ocrTextEx(handle, [image], x1, y1, x2, y2, colorfmt)

参数:

参数名类型说明
handle整数tresseract-ocr对象句柄
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式

返回值:

返回值类型说明
字符串返回一个json数组字符串

示例:

lua
function testOcrTextEx(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if not img:snapShot() then
				print("截图失败")
				return
			end
		end
	end
	local handle = createOcr("eng" , 3 , 3)
	if handle ~= nil then
		setWhiteList(handle , "123456789.")
		local ret = nil
		if img ~= nil then
			ret = ocrTextEx(handle , img , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		else
			ret = ocrTextEx(handle , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		end
		if ret ~= nil then
			print(ret)
		end
		releaseOcr(handle)
	end
end

22. 本地光学ocr识别返回坐标和置信度 (ocrTextEx2)

说明: 获取指定范围内的文本字符串、坐标和置信度

函数: ocrTextEx2(handle, [image], x1, y1, x2, y2, colorfmt)

参数:

参数名类型说明
handle整数tresseract-ocr对象句柄
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图,可选参数
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
colorfmt字符串文字的颜色格式

返回值:

返回值类型说明
字符串返回一个json数组字符串

示例:

lua
function testOcrTextEx2(useImage)
	local img = nil
	if useImage then
		img = QImage.new()
		if img ~= nil then
			if not img:snapShot() then
				print("截图失败")
				return
			end
		end
	end
	local handle = createOcr("eng" , 3 , 3)
	if handle ~= nil then
		setWhiteList(handle , "123456789.")
		local ret = nil
		if img ~= nil then
			ret = ocrTextEx2(handle , img , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		else
			ret = ocrTextEx2(handle , 355 , 940 , 557 , 993 , "222222-202020|535353-202020")
		end
		if ret ~= nil then
			print(ret)
		end
		releaseOcr(handle)
	end
end