-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_old.js
162 lines (150 loc) · 4.3 KB
/
index_old.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const term = require("terminal-kit").terminal;
const pack = require("./package.json");
const cocoSsd = require("@tensorflow-models/coco-ssd");
const tf = require("@tensorflow/tfjs-node");
const fs = require("fs").promises;
const file = require("fs");
const http = require("https");
let counter = 0;
let limit;
let globalmodel, imgTensor, filee;
async function sleep(t) {
return new Promise((r) => {
setTimeout(() => {
r();
}, t);
});
}
async function getfile(counter) {
return new Promise(async (r) => {
if (
!file.existsSync("./github/" + counter + ".jpg") &&
!file.existsSync("./face/" + counter + ".jpg") &&
!file.existsSync("./notface/" + counter + ".jpg")
) {
// await sleep(1000);
filee = file.createWriteStream("./github/" + counter + ".jpg");
await http
.get(`https://avatars.githubusercontent.com/u/${counter}`, function (
response
) {
response.pipe(filee);
// after download completed close filestream
filee.on("finish", async () => {
filee.close();
await fs.readFile(`./github/${counter}.jpg`).then(async (d) => {
imgTensor = await tf.node.decodeImage(new Uint8Array(d), 3);
await globalmodel.detect(imgTensor).then(async (a) => {
if (a[0]) {
if (a[0].class == "person") {
fs.rename(
`./github/${counter}.jpg`,
`./face/${counter}.jpg`
);
} else {
fs.rename(
`./github/${counter}.jpg`,
`./notface/${counter}.jpg`
);
}
} else {
fs.rename(
`./github/${counter}.jpg`,
`./notface/${counter}.jpg`
);
}
});
});
});
})
.on("error", (e) => {
console.log(e);
});
} else {
r();
counter++;
if (counter >= limit) {
intro(true);
term.yellow("ALREADY DOWNLOADED THESE MUCH FILES\n");
process.exit(0);
}
// getfile(counter);
}
});
}
cocoSsd.load().then(async (v) => {
await term("enter an image processing limit (1000): ");
term.inputField({}, async (e, input) => {
term.green("\nlimit set to %s\n", input);
console.clear();
limit = input || 1000;
globalmodel = v;
intro();
while (counter < limit) {
await sleep(1500);
getfile(counter);
counter++;
progressBar.update(counter / limit);
if (counter / limit >= 1) {
intro(true);
term.green("ALL FILES DOWNLOADED AND PARSED! CHECK FOLDERS!\n");
process.exit(0);
}
if (counter >= limit) {
process.exit(0);
}
}
});
});
async function intro(without) {
if (without) {
console.clear();
}
console.log("╔═╗┬┌┬┐┬ ┬┬ ┬┌┐ ╔═╗┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐");
console.log("║ ╦│ │ ├─┤│ │├┴┐ ╚═╗│ ├┬┘├─┤├─┘├┤ ├┬┘");
console.log("╚═╝┴ ┴ ┴ ┴└─┘└─┘ ╚═╝└─┘┴└─┴ ┴┴ └─┘┴└─");
console.log("v" + pack.version);
console.log("press ctrl+c to exit");
if (!without) {
progressBar = term.progressBar({
width: 80,
title: "progress: ",
eta: false,
percent: true,
});
console.log();
term.spinner({
animation: "asciiSpinner",
});
} else {
await file.readdir("./face", async (e, f) => {
if (e) {
term.red("run setup");
process.exit(0);
}
await file.readdir("./notface", (e, ff) => {
if (e) {
term.red("run setup");
process.exit(0);
}
counter = f.length + ff.length;
console.log("estimate of pre-progress: ", counter);
});
});
}
}
intro(true);
/**
* safety now
*/
const r = require("readline");
r.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
process.stdin.on("keypress", (s, k) => {
if (k.ctrl == true && k.name == "c") {
term.red("\nctrl+c pressed, now exitting\n");
process.exit(0);
}
});