-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (79 loc) · 2.47 KB
/
script.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { readFileSync, writeFileSync, promises } from 'fs'
import weaviate from 'weaviate-ts-client';
// connect to db client
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
// create 'MEME' schema - run once after db init
const createDBSchema = async () => {
const schemaConfig = {
'class': 'Meme',
'vectorizer': 'img2vec-neural',
'vectorIndexType': 'hnsw',
'moduleConfig': {
'img2vec-neural': {
'imageFields': [
'image'
]
}
},
'properties': [
{
'name': 'image',
'dataType': ['blob']
},
{
'name': 'text',
'dataType': ['string']
}
]
}
client.schema
.classCreator()
.withClass(schemaConfig)
.do()
.then(schemaRes => console.log('schemaRes', schemaRes))
.catch(err => console.error('err', err));
const schemaRes = await client.schema.getter().do();
console.log('Schema created!', schemaRes);
};
// store img to db - no loop, invoke for every meme in img folder
const storeImgInDB = async () => {
const dir = await promises.opendir('./img/');
for await (const dirent of dir) {
const file = readFileSync(`./img/${dirent.name}`),
image = Buffer.from(file).toString('base64'),
text = `${dirent.name.split('.').slice(0, -1).join('.')} meme`;
await client.data.creator()
.withClassName('Meme')
.withProperties({
image,
text
})
.do();
console.log(`${dirent.name} img saved!`);
}
};
const findSimilarMeme = async () => {
const inputImgame = 'aMEPeB1_460s.jpg',
inputImgBase = Buffer.from(readFileSync(`./input/${inputImgame}`)).toString('base64'),
resImage = await client.graphql.get()
.withClassName('Meme')
.withFields(['image'])
.withNearImage({ image: inputImgBase })
.withLimit(1)
.do();
const result = resImage.data.Get.Meme[0].image;
writeFileSync('./result.jpg', result, 'base64');
console.log('result img created!');
}
// check if db is online
const liveChecker = await client.misc.liveChecker().do();
if (liveChecker) {
createDBSchema();
// storeImgInDB();
// findSimilarMeme();
} else {
console.error('db is not running');
}