-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
66 lines (58 loc) · 2.05 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
global.XMLHttpRequest = require('xhr2');
const {Image, createCanvas} = require('canvas');
const fetch = require('node-fetch');
const fs = require('fs');
const posenet = require('@tensorflow-models/posenet')
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const utils = require('./utils.js')
const videoProcessor = require('./video_processor')
const drawKeypoints = (name, index, canvas, keypoints) => {
ctx = canvas.getContext('2d');
keypoints.forEach((key, index) => {
if (index > 4 && index < 11) {
ctx.beginPath();
ctx.arc(key.position.x, key.position.y, 5, 0, 2 * Math.PI, false);
ctx.lineWidth = 3;
ctx.strokeStyle = '#00ff00';
ctx.stroke();
}
});
const buf = canvas.toBuffer();
fs.writeFileSync(`./images/${name}/r${name}_${utils.formatIndex(index)}.jpg`, buf);
fs.writeFileSync(`./images/${name}/d${name}_${utils.formatIndex(index)}.json`, JSON.stringify(keypoints) , 'utf-8');
console.log(`./images/${name}/d${name}_${utils.formatIndex(index)}.json`);
}
const run = async (net, name, index) => {
console.log(index);
let img_path = `./images/${name}/${name}_${utils.formatIndex(index)}.png`;
let { Response } = fetch;
let stream = fs.createReadStream(img_path);
let buffer = await new Response(stream).buffer()
let img = new Image();
img.src = buffer;
const canvas = createCanvas(img.width,img.height);
canvas.getContext('2d').drawImage(img, 0, 0);
const imageScaleFactor = 1;
const flipHorizontal = false;
const outputStride = 8;
const pose = await net.estimateSinglePose(canvas, imageScaleFactor, flipHorizontal, outputStride);
drawKeypoints(name, index, canvas, pose.keypoints);
}
const main = async () => {
try {
const length = 20;
const multiplier = 1.01;
const filename = '5_dollars';
await videoProcessor.generateImages(filename, length);
const net = await posenet.load(multiplier);
let index = 0
while (index < length) {
await run(net, filename, index + 1);
index++;
}
} catch (err) {
console.log(err);
}
}
main();