-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (89 loc) · 2.84 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const cognitiveServices = require('cognitive-services');
require('dotenv').config();
const fs = require('fs');
const client = new cognitiveServices.computerVision({
endpoint: process.env.ENDPOINT,
apiKey: process.env.API_KEY
});
const parameters = {
"visualFeatures": 'Categories,Color,Description'
}
const headers = {
'Content-type': 'application/octet-stream'
};
// const TEST_IMAGE_URL = fs.readFileSync('bilder/0000001.jpg')
// const body = {"url":FRIENDS_IMAGE_URL}
/*for (let num = 1; num <= 1; num++) {
let imageNumber = ("0000000" + num).slice(-7);
let imagePath = `bilder/${imageNumber}.jpg`
console.log(`Reading: ${imagePath}`)
let image = fs.readFileSync(imagePath)
sendImage(image, imageNumber)
}*/
let fixedNumberLenght = 7
let startNumber = 3000
let endNumber = 4000
if (process.argv.slice(2)){
startNumber = process.argv.slice(2, 3) - 1
endNumber = process.argv.slice(3, 4)
consoleDivider()
console.log('start from: ' + startNumber + ' to: ' + endNumber)
}
startNewUpload(startNumber)
function startNewUpload(currentCounter) {
consoleDivider();
let nextCounter = currentCounter + 1
let imageNumber = fillNumberTo(nextCounter, fixedNumberLenght)
if (nextCounter <= endNumber) {
let filePath = createFilePath(imageNumber)
console.log(`New Image Path: ${filePath}`)
let imageData = fs.readFileSync(filePath)
sendImage(imageData, imageNumber, nextCounter)
console.log(imageData)
} else {
console.log('End of Counter: ' + nextCounter)
consoleDivider();
}
}
function consoleDivider() {
console.log('')
console.log('------------')
}
function fillNumberTo(counter, lenght) {
return ("0000000" + counter).slice(-lenght);
}
function createFilePath(fileNumber) {
return filePath = `bilder/${fileNumber}.jpg`
}
function sendImage(body, imageNumber, currentCounter) {
client.analyzeImage({
parameters,
headers,
body
}).then(response => {
// console.log(response);
console.log(`response Caption from ${imageNumber}:`);
if (response.description.captions) {
console.log(response.description.captions)
}
writeData(response, imageNumber)
startNewUpload(currentCounter)
}).catch((err) => {
console.log(err);
})
}
function writeData(responseData, imageNumber){
// fs.appendFileSync('data.json', JSON.stringify(responseData))
fs.readFile('data/data.json',function(err,content){
if(err) throw err;
let parseJson = JSON.parse(content)
// console.log(parseJson)
parseJson.images.push({
imageNumber: imageNumber,
data: responseData
})
fs.writeFile('data/data.json',JSON.stringify(parseJson),function(err){
if(err) throw err;
})
})
}