forked from PaddlePaddle/Paddle.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
46 lines (40 loc) · 1.38 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as mobilenet from '@paddlejs-models/mobilenet';
import map from './data/map.json';
const img = document.getElementById('image') as HTMLImageElement;
const txt = document.getElementById('txt') as HTMLElement;
const time = document.getElementById('all-performance-time') as HTMLElement;
const uploadImg = document.getElementById('uploadImg') as HTMLInputElement;
const loading = document.getElementById('loading');
let startTime = 0;
let endTime = 0;
load();
async function load() {
const path = 'https://paddlejs.cdn.bcebos.com/models/mobilenetV2_nchw';
await mobilenet.load({
path,
mean: [0.485, 0.456, 0.406],
std: [0.229, 0.224, 0.225]
}, map);
loading.style.display = 'none';
}
async function run(input: HTMLElement) {
startTime = +new Date();
const res = await mobilenet.classify(input);
endTime = +new Date() - startTime;
txt.innerText = res;
time.innerHTML = '计算时间是' + endTime;
}
// selectImage
uploadImg.onchange = (e: Event) => {
const reader = new FileReader();
const initInnerText = '...';
txt.innerText = initInnerText;
time.innerText = initInnerText;
reader.onload = () => {
img.src = URL.createObjectURL((e.target as HTMLInputElement).files[0]);
img.onload = () => {
run(img);
};
};
reader.readAsDataURL((e.target as HTMLInputElement).files[0]);
};