Skip to content

Commit

Permalink
Add Paddle.js deploy tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
JingyuanZhang authored May 17, 2021
1 parent 0a450e7 commit 21acc09
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ venv.bak/

# mypy
.mypy_cache/

# js
node_modules/
package-lock.json
74 changes: 74 additions & 0 deletions deploy/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Web 端部署

## 1.介绍
以人像分割在 MacOS Chrome 的部署为例,介绍如何使用前端推理引擎 [Paddle.js](https://github.com/PaddlePaddle/Paddle.js) 对分割模型进行部署。文档第二部分介绍如何使用人像分割模型 js 库 [@paddlejs-models/humanseg](https://github.com/PaddlePaddle/Paddle.js/tree/master/packages/paddlejs-models/humanseg),第三部分介绍重点 API。

## 2. 使用

### 2.1 要求
* 安装 Node (https://nodejs.org/zh-cn/download/)
* 确认是否安装成功,在命令行执行
```sh
# 显示所安 node 版本号,即表示成功安装
node -v
```
### 2.2 步骤
```sh
# clone Paddle.js
git clone https://github.com/PaddlePaddle/PaddleSeg.git

# 进入 deploy web example 目录,安装依赖
cd PaddleSeg/deploy/web/example/ && npm install

# 执行命令
npm run dev

# 访问 http://0.0.0.0:8866/ ,即可体验人像分割处理图片应用
```


### 2.3 效果展示

![image](https://user-images.githubusercontent.com/10822846/118273079-127bf480-b4f6-11eb-84c0-8a0bbc7c7433.png)

## 3. 重点 API 介绍

## 3.1 @paddlejs-models/humanseg 介绍
npm 库 [@paddlejs-models/humanseg](https://github.com/PaddlePaddle/Paddle.js/tree/master/packages/paddlejs-models/humanseg) 封装了前端推理引擎 [Paddle.js](https://github.com/PaddlePaddle/Paddle.js) 和计算方案 [paddlejs-backend-webgl](https://github.com/PaddlePaddle/Paddle.js/tree/master/packages/paddlejs-backend-webgl),该计算方案通过 WebGL 获得 GPU 加速。
所以只需引入库 @paddlejs-models/humanseg 即可,无需再额外引入推理引擎和计算方案。

## 3.1.1 API 介绍
@paddlejs-models/humanseg 暴露了四个 API:
* load
调用 load API 完成推理引擎初始化。下载humanseg_lite_inference web 模型,根据模型结构和参数文件生成神经网络,并完成模型预热。

* getGrayValue
给 getGrayValue API 传入需要处理的图片,执行后获得推理结果。

* drawHumanSeg
绘制人像。给 drawHumanSeg API 传入 canvas 画布元素和推理结果,可以通过该元素传递背景信息,分割后的人像将绘制在此 canvas上。

* drawMask
绘制人像遮罩。传入 canvas 画布元素和推理结果,同时传入参数 dark 配置是否使用暗黑模式。效果将绘制在传入的 canvas 画布元素上。

## 3.2 如何使用 @paddlejs-models/humanseg

```js
// 引入
import * as humanseg from '@paddlejs-models/humanseg';

// load humanseg model
await humanseg.load();

// get the seg value [192 * 192];
const { data } = await humanseg.getGrayValue(img);

// draw human segmentation
const canvas1 = document.getElementById('demo1') as HTMLCanvasElement;
humanseg.drawHumanSeg(canvas1, data);

// draw the background mask
const canvas2 = document.getElementById('demo2') as HTMLCanvasElement;
humanseg.drawMask(canvas2, data, true);

```
Binary file added deploy/web/example/bg/bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions deploy/web/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>paddle web demo - humanseg</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#loading {
position: fixed;
top: 0;
left: 0;
z-index: 10;
width: 100vw;
height: 100vh;
line-height: 100vh;
background-color: rgba(0, 0, 0, .4);
color: #fff;
text-align: center;
font-size: 16px;
}
</style>
</head>
<body>
<img id="image" src="https://m.baidu.com/se/static/img/iphone/logo.png" style="max-width: 100%;" />
<canvas id="demo1"></canvas>
<canvas id="demo2"></canvas>
<input type="file" id="uploadImg">
<div id="loading">loading...</div>
</body>
</html>
49 changes: 49 additions & 0 deletions deploy/web/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as humanseg from '@paddlejs-models/humanseg';



async function load() {
await humanseg.load();
document.getElementById('loading')!.style.display = 'none';
}

load();
const canvas1 = document.getElementById('demo1') as HTMLCanvasElement;
const canvas2 = document.getElementById('demo2') as HTMLCanvasElement;

const ctx = canvas1.getContext('2d');
const img = new Image();
img.src = './bg/bg.jpg';
img.onload = function () {
ctx.drawImage(img, 0, 0, canvas1.width, canvas1.height);
};

async function run(input) {
const {
data
} = await humanseg.getGrayValue(input);

humanseg.drawHumanSeg(canvas1, data);
humanseg.drawMask(canvas2, data, true);
}

function selectImage(file) {
if (!file.files || !file.files[0]) {
return;
}
const reader = new FileReader();
reader.onload = function (evt) {
const img = document.getElementById('image') as HTMLImageElement;
img.src = evt.target.result as any;
img.onload = function () {
run(img);
};
};
reader.readAsDataURL(file.files[0]);
}

// selectImage
document.getElementById('uploadImg').onchange = function () {
selectImage(this);
};

25 changes: 25 additions & 0 deletions deploy/web/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@paddlejs-models/humanseg",
"version": "0.0.8",
"description": "",
"main": "lib/index",
"scripts": {
"dev": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"@paddlejs-mediapipe/opencv": "^1.0.0",
"@paddlejs-models/humanseg": "^0.0.8",
"@paddlejs/paddlejs-backend-webgl": "^1.0.1",
"@paddlejs/paddlejs-core": "^2.0.1"
},
"devDependencies": {
"html-webpack-plugin": "^3.2.0",
"ts-loader": "^8.0.12",
"typescript": "^3.9.5",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}
74 changes: 74 additions & 0 deletions deploy/web/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2015", "dom"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true, /* Do not emit comments to output. */
"types": ["node"],
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": false, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": false, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"resolveJsonModule": true
},
"include": [
"index.ts"
],
"exclude": [
"node_modules/"
],
}
41 changes: 41 additions & 0 deletions deploy/web/example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
mode: 'development',
entry: {
index: './index.ts'
},
devtool: 'inline-source-map',
devServer: {
host: '0.0.0.0',
port: 8866
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['index'],
template: 'index.html'
})
],
resolve: {
// Add ".ts" and ".tsx" as resolvable extensions.
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
exclude: /node_modules/
}
]
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
node: {
fs: 'empty'
}
};

0 comments on commit 21acc09

Please sign in to comment.