-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
example.ts
65 lines (57 loc) · 2.06 KB
/
example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as fs from "fs";
import * as nodeFetch from "node-fetch";
import {
EfficientNetCheckPointFactory,
EfficientNetCheckPoint,
EfficientNetModel,
EfficientNetResult,
EfficientNetLableLanguage,
EfficientNetLanguageProvider
} from "./index";
const images = ["car.jpg", "panda.jpg", "fish.jpg"];
const imageDir = "./samples";
const imageDirRemoteUri =
"https://raw.githubusercontent.com/ntedgi/node-EfficientNet/main/samples";
if (!fs.existsSync(imageDir)) fs.mkdirSync(imageDir);
async function download(image: string, cb: fs.NoParamCallback) {
const response = await nodeFetch.default(`${imageDirRemoteUri}/${image}`);
const buffer = await response.buffer();
fs.writeFile(`${imageDir}/${image}`, buffer, cb);
}
//Default language results (English)
EfficientNetCheckPointFactory.create(EfficientNetCheckPoint.B0)
.then((model: EfficientNetModel) => {
images.forEach(async (image) => {
await download(image, () => {
model
.inference(`${imageDir}/${image}`, { topK: 3 })
.then((result: EfficientNetResult) => {
console.log("Result in English : \n -------------------");
console.log(result.result);
});
});
});
})
.catch((e: Error) => {
console.error(e);
});
//Not default language results (Spanish)
EfficientNetCheckPointFactory.create(EfficientNetCheckPoint.B0)
.then(async (model: EfficientNetModel) => {
const labelLanguage = EfficientNetLableLanguage.SPANISH;
const languageProvider = new EfficientNetLanguageProvider(labelLanguage);
await languageProvider.load();
images.forEach(async (image) => {
await download(image, () => {
model
.inference(`${imageDir}/${image}`, { topK: 3 }, languageProvider)
.then((result: EfficientNetResult) => {
console.log("Result in Spanish : \n -------------------");
console.log(result.result);
});
});
});
})
.catch((e: Error) => {
console.error(e);
});