1.注册一个百度智能云账号 https://cloud.baidu.com/
2.登录进入控制台 选择人工智能下面的文字识别
3.创建一个应用,名字 类型 应用描述随便写.接口选择不用管默认就可以.
4.返回应用列表后记住 API Key 和 Secret Key 这两个的内容.对应下面脚本内的AK 和 SK
5.点击概览可以看到各种识别可以免费用几次,普通的通用文字识别一天是5万次的免费,并发数是10,正常使用应该会够,不够怎么办?多注册几个账号切换用呗
6.文字识别文档:https://cloud.baidu.com/doc/OCR/s/zjwvxzr65
7.注意!!!!使用此函数必须使用373以上版本!!!!!
BaiduAi = {
Init = (function()
local AK = '这里写应用的API Key';
local SK = '这里写应用的Secret Key';
local AccessToken = httpPost('https://aip.baidubce.com/oauth/2.0/token',{["grant_type"]="client_credentials",["client_id"]=AK,["client_secret"]=SK})
AccessToken = jsonDecode(AccessToken);
return AccessToken.access_token;
end),
Ocr = (function(ImagePath,AT,Api,Ext)
local file = io.open(ImagePath, 'rb');
local data = file:read("*all");
file:close();
local image = urlEncode(Base64.Encode(data));
Ext = Ext or {};
Ext["image"]=image;
local OcrInfo = httpPost(string.format('https://aip.baidubce.com/rest/2.0/ocr/v1/%s?access_token=%s',Api,AT),Ext);
return OcrInfo;
end)
}
Base64 = {
Init = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
Encode = (function(data)
local b = Base64.Init;
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end),
Decode = (function(data)
local b = Base64.Init;
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end)
}
function urlEncode(s)
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
function httpPost(path,data)
--local https = require("ssl.https") --5.0以下版本加载ssl才能访问https链接
local https = require("socket.http") --5.0版本以上触摸加载http即可
local ltn12 = require("ltn12")
local request_body = '';
if type(data)=='table' then
for k,v in pairs(data) do
request_body = request_body..k..'='..v..'&'
end
else
request_body = data
end
local response_body = {}
local res, code, response_headers = https.request{
url = path,
method = "POST",
headers =
{
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = string.len(request_body);
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
if type(response_body) == "table" then
if table.concat(response_body) ~= "error" then
return table.concat(response_body);
else
return -1;
end
else
logDebug("Not a table:", type(response_body))
return -1;
end
end
function main()
mSleep(2000)
--获取AccessToken,这个AccessToken有效期为30天,没必要获取很多次,可以获取一次保存下来
AccessToken = BaiduAi.Init()
logDebug(AccessToken)
ImagePath = '/var/touchelf/res/1.jpg'
--截图,也可以使用区域截图,注意图片的要求
--图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px
snapshotScreen(ImagePath);
--通用文字识别
Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'general_basic')
--返回的是个json数据
logDebug(Ocr)
--通用文字识别(高精度含位置版)
Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'accurate')
--返回的是个json数据
logDebug(Ocr)
--通用文字识别自定义参数写法
Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'general_basic',{["vertexes_location"]='true',["probability"]='true'})
--返回的是个json数据
logDebug(Ocr)
--其他类型看文档修改BaiduAi.Ocr第三个参数即可.
end
通用文字识别返回示例
[2019-06-20 21:07:46] {"log_id": 745660733214676500, "words_result_num": 16, "words_result": [{"words": "无SM卡令"}, {"words": "下午9:07"}, {"words": "触摸精灵"}, {"words": "信息"}, {"words": "关于"}, {"words": "帮助"}, {"words": "系统信息"}, {"words": "查询已安装应用包名"}, {"words": "查看日志"}, {"words": "清空日志"}, {"words": "清空GPS伪装信息"}, {"words": ">>>"}, {"words": "重启设备"}, {"words": "三"}, {"words": "列表"}, {"words": "设置"}]}
通用文字识别(高精度含位置版)返回示例
[2019-06-20 21:10:17] {"log_id": 7008446590512061492, "words_result_num": 20, "words_result": [{"location": {"width": 142, "top": 6, "left": 11, "height": 27}, "words": "无SM卡令"}, {"location": {"width": 97, "top": 7, "left": 274, "height": 26}, "words": "下午9:10"}, {"location": {"width": 136, "top": 65, "left": 251, "height": 37}, "words": "触摸精灵"}, {"location": {"width": 34, "top": 264, "left": 30, "height": 123}, "words": "关帮"}, {"location": {"width": 19, "top": 268, "left": 590, "height": 28}, "words": ">"}, {"location": {"width": 31, "top": 352, "left": 65, "height": 36}, "words": "助"}, {"location": {"width": 18, "top": 357, "left": 591, "height": 27}, "words": ">"}, {"location": {"width": 105, "top": 482, "left": 29, "height": 29}, "words": "系统信息"}, {"location": {"width": 308, "top": 551, "left": 30, "height": 37}, "words": "查询已安装应用包名"}, {"location": {"width": 19, "top": 555, "left": 591, "height": 28}, "words": ">"}, {"location": {"width": 136, "top": 638, "left": 30, "height": 37}, "words": "查看日志"}, {"location": {"width": 18, "top": 643, "left": 591, "height": 28}, "words": ">"}, {"location": {"width": 136, "top": 727, "left": 30, "height": 36}, "words": "清空日志"}, {"location": {"width": 18, "top": 731, "left": 591, "height": 28}, "words": ">"}, {"location": {"width": 271, "top": 814, "left": 30, "height": 38}, "words": "清空GPS伪装信息"}, {"location": {"width": 18, "top": 819, "left": 591, "height": 29}, "words": ">"}, {"location": {"width": 136, "top": 903, "left": 30, "height": 36}, "words": "重启设备"}, {"location": {"width": 18, "top": 907, "left": 591, "height": 28}, "words": ">"}, {"location": {"width": 41, "top": 1110, "left": 137, "height": 24}, "words": "列表"}, {"location": {"width": 39, "top": 1111, "left": 458, "height": 22}, "words": "设置"}]}
使用触摸自带httpPost
BaiduAi = {
Init = (function()
local AK = '这里写应用的API Key';
local SK = '这里写应用的Secret Key';
local AccessToken = httpPost('https://aip.baidubce.com/oauth/2.0/token',{},{["grant_type"]="client_credentials",["client_id"]=AK,["client_secret"]=SK},10)
AccessToken = jsonDecode(AccessToken);
return AccessToken.access_token;
end),
Ocr = (function(ImagePath,AT,Api,Ext)
local file = io.open(ImagePath, 'rb');
local data = file:read("*all");
file:close();
local image = urlEncode(Base64.Encode(data));
Ext = Ext or {};
Ext["image"]=image;
local OcrInfo = httpPost(string.format('https://aip.baidubce.com/rest/2.0/ocr/v1/%s?access_token=%s',Api,AT),{},Ext,10);
return OcrInfo;
end)
}
Base64 = {
Init = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
Encode = (function(data)
local b = Base64.Init;
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end),
Decode = (function(data)
local b = Base64.Init;
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end)
}
function urlEncode(s)
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
function main()
mSleep(2000)
--获取AccessToken,这个AccessToken有效期为30天,没必要获取很多次,可以获取一次保存下来
local AccessToken = BaiduAi.Init()
logDebug(AccessToken)
ImagePath = '/var/touchelf/1.jpg'
--截图,也可以使用区域截图,注意图片的要求
--图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px
snapshotScreen(ImagePath);
--通用文字识别
Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'general_basic')
--返回的是个json数据
logDebug(Ocr)
end
大神 我想用数字识别的话应该怎么改?
@fstone 不用改.直接识别就是了.注意图片大小的要求
不行啊 识别不到数字啊 !他有个专门的数字识别,能教我一下改改吗?
@evan 不行啊 识别不到数字啊 !他有个专门的数字识别,能教我一下改改吗?
@fstone Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'numbers')
local https = require("ssl.https")在这行提示出错,貌似找不到插件包
@evan local https = require("ssl.https")在这行提示出错,貌似找不到插件包
@cxb746 要求触摸最低版本373以上
@evan
Ocr = BaiduAi.Ocr(ImagePath,AccessToken,'数字')
{"error_code":3,"error_msg":"Unsupported openapi method"}
这段代码运行是错误的,
请问调用对比数字或者字母的简单函数是什么,本人小白请见谅!
我的理解是
AccessToken = BaiduAi.Init()
--获取AccessToken,这个AccessToken有效期为30天,没必要获取很多次,可以获取一次保存下来
这个保存要怎么保存,完全不懂