Skip to content

Commit 0583e62

Browse files
Merge pull request #401 from PaddlePaddle/master
chore(paddlejs-examples): add ocr detection wechat xcx demo
2 parents 485792a + 4aa5230 commit 0583e62

File tree

13 files changed

+666
-0
lines changed

13 files changed

+666
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# ocr detection 微信小程序代码示例
2+
3+
## 1.介绍
4+
本目录为文本检测小程序代码,通过使用 [Paddle.js](https://github.com/PaddlePaddle/Paddle.js) 以及 [Paddle.js微信小程序插件](https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wx7138a7bb793608c3&token=956931339&lang=zh_CN) 完成在小程序上利用用户终端算力实现文本检测框选效果。
5+
6+
文本检测详见 [@paddlejs-models/ocrdet](https://github.com/PaddlePaddle/Paddle.js/tree/release/v2.2.3/packages/paddlejs-models/ocrdetection)
7+
8+
开发者可在demo基础上增加文本识别能力,可参考 [@paddlejs-models/ocr](https://github.com/PaddlePaddle/Paddle.js/blob/release/v2.2.3/packages/paddlejs-models/ocr/src/index.ts) 实现。
9+
10+
## 2. 项目启动
11+
12+
### 2.1 准备工作
13+
* [申请微信小程序账号](https://mp.weixin.qq.com/)
14+
* [微信小程序开发者工具](https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html)
15+
* 前端开发环境准备:node、npm
16+
* 小程序管理后台配置服务器域名,或打开开发者工具【不校验合法域名】
17+
详情参考:https://mp.weixin.qq.com/wxamp/devprofile/get_profile?token=1132303404&lang=zh_CN)
18+
19+
### 2.2 启动步骤
20+
#### **1. clone Paddle.js**
21+
```sh
22+
git clone https://github.com/PaddlePaddle/Paddle.js.git
23+
```
24+
25+
#### **2. 进入 xxx 目录,安装依赖**
26+
```sh
27+
cd Paddle.js/packages/paddlejs-examples/ocrdetectXcx && npm install
28+
```
29+
30+
#### **3. 微信小程序导入代码**
31+
打开微信开发者工具 --> 导入 --> 选定目录,输入相关信息
32+
33+
#### **4. 添加 Paddle.js微信小程序插件**
34+
小程序管理界面 --> 设置 --> 第三方设置 --> 插件管理 --> 添加插件 --> 搜索 `wx7138a7bb793608c3` 并添加
35+
[参考文档](https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/using.html)
36+
37+
#### **5. 构建依赖**
38+
点击开发者工具中的菜单栏:工具 --> 构建 npm
39+
***原因**:node_modules 目录不会参与编译、上传和打包中,小程序想要使用 npm 包必须走一遍“构建 npm”的过程,构建完成会生成一个 miniprogram_npm 目录,里面会存放构建打包后的 npm 包,也就是小程序真正使用的 npm 包。*
40+
[参考文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)
41+
42+
### 2.3 效果展示
43+
![image](https://user-images.githubusercontent.com/43414102/157648579-cdbbee61-9866-4364-9edd-a97ac0eda0c1.png)
44+
45+
## 3. Paddle.js 框架推理
46+
```typescript
47+
// 引入 paddlejs 和 paddlejs-plugin,注册小程序环境变量和合适的 backend
48+
import * as paddlejs from '@paddlejs/paddlejs-core';
49+
import '@paddlejs/paddlejs-backend-webgl';
50+
const plugin = requirePlugin('paddlejs-plugin');
51+
plugin.register(paddlejs, wx);
52+
53+
// 初始化推理引擎
54+
const runner = new paddlejs.Runner({modelPath, feedShape, mean, std});
55+
await runner.init();
56+
57+
// 获取图像信息
58+
wx.canvasGetImageData({
59+
canvasId: canvasId,
60+
x: 0,
61+
y: 0,
62+
width: canvas.width,
63+
height: canvas.height,
64+
success(res) {
65+
// 推理预测
66+
runner.predict({
67+
data: res.data,
68+
width: canvas.width,
69+
height: canvas.height,
70+
}, function (data) {
71+
// 获取推理结果
72+
console.log(data)
73+
});
74+
}
75+
});
76+
```
77+
78+
## 4. 更多
79+
* [详细文档](https://mp.weixin.qq.com/s/GP1lc3FZ6lQyD7FJfU67xw)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* global wx, App */
2+
import * as paddlejs from '@paddlejs/paddlejs-core';
3+
import '@paddlejs/paddlejs-backend-webgl';
4+
// eslint-disable-next-line no-undef
5+
const plugin = requirePlugin('paddlejs-plugin');
6+
plugin.register(paddlejs, wx);
7+
8+
App({
9+
globalData: {
10+
Paddlejs: paddlejs.Runner
11+
}
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"pages": [
3+
"pages/index/index"
4+
],
5+
"plugins": {
6+
"paddlejs-plugin": {
7+
"version": "2.0.1",
8+
"provider": "wx7138a7bb793608c3"
9+
}
10+
},
11+
"sitemapLocation": "sitemap.json"
12+
}

packages/paddlejs-examples/ocrdetectXcx/package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "paddlejs-demo",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "app.js",
6+
"dependencies": {
7+
"@paddlejs/paddlejs-backend-webgl": "^1.1.19",
8+
"@paddlejs/paddlejs-core": "^2.1.17",
9+
"d3-polygon": "2.0.0",
10+
"js-clipper": "1.0.1",
11+
"number-precision": "1.5.2"
12+
},
13+
"scripts": {
14+
"test": "echo \"Error: no test specified\" && exit 1"
15+
},
16+
"keywords": [],
17+
"author": "",
18+
"license": "ISC"
19+
}
222 KB
Loading

0 commit comments

Comments
 (0)