21

开发函数计算的正确姿势——OCR 服务

 4 years ago
source link: http://dockone.io/article/10144
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

7vUneuN.jpg!web

简介

首先介绍下在本文出现的几个比较重要的概念:

OCR(光学字符识别): 光学字符识别(Optical Character Recognition, OCR)是指对文本资料的图像文件进行分析识别处理,获取文字及版面信息的过程。亦即将图像中的文字进行识别,并以文本的形式返回。

Tesserocr: Tesserocr 是 python 的一个 OCR 识别库,但其实是对 tesseract 做的一层 python API 封装,所以它的核心是 tesseract。

函数计算(Function Compute): 函数计算 是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息 参考

Funcrfat: Funcraft 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档 参考

备注: 本文介绍的技巧需要 Funcraft 版本大于等于 3.6.8 。

本文介绍将一个预制的 tesserocr 示例快速部署到函数计算平台。该示例借助于 Funcraft 安装了最新的 4.1.1 版本的 tesseract,相比于包管理器的 3.0.2 版本识别率大幅度提升。

该示例提供了一个 vue.js 实现的交互界面,有三种提供输入图片的方式:使用示例图片、上传图片或者提供图片 URL。然后用户点击识别按钮就能迅速体验识别效果。

将 tesserocr 部署于函数计算,借助于函数计算的自动伸缩和按量计费的特性,提供了免运维和成本优势。

下面是部署以后的使用效果动图:

VJRjey2.gif

初始化

bash

git clone https://github.com/vangie/fc-ocr-example.git

安装依赖

bash

$ fun install

本地测试

```bash

$ fun local start domain_for_ocr

using template: template.yml

CustomDomain domain_for_ocr of tesserocr/tesserocr was registered

url: http://localhost:8000/

methods: [ 'GET', 'POST' ]

authType: ANONYMOUS

function compute app listening on port 8000!

```

使用浏览器打开 http://localhost:8000/

同步文件到 NAS

同步模型目录 tessdata_fast 和 依赖目录 .fun/root .fun/python 到 NAS 盘。

bash

$ fun nas sync

部署

```bash

$ fun deploy

using template: template.yml

using region: cn-shanghai

using accountId: ***********3743

using accessKeyId: ***********Ptgk

using timeout: 60

...

Detect 'DomainName:Auto' of custom domain 'domain_for_ocr'

Fun will reuse the temporary domain 1712300-1986114430573743.test.functioncompute.com, expired at 2020-04-27 19:35:00, limited by 1000 per day.

Waiting for custom domain domain_for_ocr to be deployed...

custom domain domain_for_ocr deploy success

...

```

注意上面返回的临时域名地址:1712300-1986114430573743.test.functioncompute.com

使用浏览器打开 http://1712300-198611443057374 ... e.com

小结

  1. Tesserocr 库提供了开箱即用的图片识别效果,而且提供了预训练好的模型,如果需要也可以直接训练模型
  2. 函数计算为 Tesserocr 服务化提供了便利。借助函数计算可以轻松部署一个高可用、按量访问量计费的 OCR 服务。
  3. Funcraft 工具解决 Tesserocr 移植到函数计算的一些技术难题,比如原生依赖,打包问题。细节可以查看 Funfile 文件。

阿里巴巴云原生 关注微服务、Serverless、容器、Service Mesh 等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,做最懂云原生开发者的技术圈。”


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK