Skip to content

Commit 052239e

Browse files
committed
feat(paddlejs-examples): add ocrdetection
1 parent 75e430f commit 052239e

File tree

10 files changed

+6602
-1
lines changed

10 files changed

+6602
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[中文版](./README_cn.md)
2+
3+
# ocr_detect
4+
5+
ocr_detect model is used to detect the text area in the image.
6+
7+
# Usage
8+
9+
```js
10+
11+
import * as ocr from '@paddlejs-models/ocrdet';
12+
13+
// Load ocr_detect model
14+
await ocr.load();
15+
16+
// Get text area points
17+
const res = await ocr.detect(img);
18+
19+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[English](./README.md)
2+
3+
# ocr_detect
4+
5+
ocr_detect模型用于检测图像中文字区域。
6+
7+
# 使用
8+
9+
```js
10+
import * as ocr from '@paddlejs-models/ocrdet';
11+
12+
// ocr_detect模型加载
13+
await ocr.load();
14+
15+
// 获取文字区域坐标
16+
const res = await ocr.detect(img);
17+
18+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>paddle ocr detection demo</title>
6+
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
7+
<style>
8+
body {
9+
margin: 0 auto;
10+
width: 100%;
11+
height: 100%;
12+
}
13+
.wrapper {
14+
position: relative;
15+
width: 100%;
16+
height: 100%;
17+
margin: 0 auto;
18+
}
19+
#isLoading {
20+
position: fixed;
21+
top: 0;
22+
left: 0;
23+
right: 0;
24+
bottom: 0;
25+
width: 100vw;
26+
height: 100vh;
27+
background-color: rgba(0, 0, 0, .5);
28+
}
29+
#isLoading .loading-text {
30+
color: #fff;
31+
font-size: 24px;
32+
text-align: center;
33+
line-height: 100vh;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div class="wrapper">
39+
<img id="image1" src="https://m.baidu.com/se/static/img/iphone/logo.png">
40+
<div id="tool">
41+
<input type="file" id="uploadImg">
42+
</div>
43+
<div id="txt">...</div>
44+
<canvas id="canvas"></canvas>
45+
</div>
46+
47+
<div id="isLoading">
48+
<p class="loading-text center">loading中……</p>
49+
</div>
50+
51+
</body>
52+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as ocr from '@paddlejs-models/ocrdet';
2+
3+
const loading = document.getElementById('isLoading');
4+
const inputElement = document.getElementById('uploadImg');
5+
const imgElement = document.getElementById('image1') as HTMLImageElement;
6+
const canvasOutput = document.getElementById('canvas') as HTMLCanvasElement;
7+
8+
load();
9+
10+
async function load() {
11+
await ocr.load();
12+
loading.style.display = 'none';
13+
}
14+
15+
function drawBox(points: number[]) {
16+
canvasOutput.width = imgElement.naturalWidth;
17+
canvasOutput.height = imgElement.naturalHeight;
18+
const ctx = canvasOutput.getContext('2d');
19+
ctx.drawImage(imgElement, 0, 0, canvasOutput.width, canvasOutput.height);
20+
points.forEach(point => {
21+
// 开始一个新的绘制路径
22+
ctx.beginPath();
23+
// 设置线条颜色为蓝色
24+
ctx.strokeStyle = 'blue';
25+
// 设置路径起点坐标
26+
ctx.moveTo(point[0][0], point[0][1]);
27+
ctx.lineTo(point[1][0], point[1][1]);
28+
ctx.lineTo(point[2][0], point[2][1]);
29+
ctx.lineTo(point[3][0], point[3][1]);
30+
ctx.closePath();
31+
ctx.stroke();
32+
});
33+
}
34+
35+
inputElement.addEventListener('change', (e: Event) => {
36+
imgElement.src = URL.createObjectURL((e.target as HTMLInputElement).files[0]);
37+
}, false);
38+
39+
imgElement.onload = async function () {
40+
canvasOutput.width = imgElement.width;
41+
canvasOutput.height = imgElement.height;
42+
// 获取文本检测坐标
43+
const res = await ocr.detect(imgElement);
44+
drawBox(res);
45+
};

0 commit comments

Comments
 (0)