-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetect.js
144 lines (117 loc) · 4.45 KB
/
detect.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
const tf = require('@tensorflow/tfjs-node');
const sharp = require('sharp');
const fs = require('fs');
var mtcnn_js = require('./mtcnn');
const canvas = require('canvas');
// const cv = require('opencv4nodejs');
const Jimp = require('jimp');
// const pnet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/pnet/model.json'
// const rnet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/rnet/model.json'
// const onet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/onet/model.json'
class MTCNN {
constructor(pnet, rnet, onet) {
this.mtcnn = this.load_model(pnet, rnet, onet)
}
load_model(pnet, rnet, onet)
{
var model = new mtcnn_js(pnet, rnet, onet);
return model
}
async load_img(img_url){
try {
var img = await sharp(img_url).toFormat('png').removeAlpha().rotate().toBuffer()
} catch (error) {
console.log(error);
return error;
}
try {
var tensor = tf.node.decodeImage(img)
} catch (error) {
console.log(error);
return error;
}
return {img, tensor}
}
async draw_img(img_url, output_url=null) {
const data = await this.detect(img_url)
var {img} = await this.load_img(img_url)
const boxes = data.boxes
// """Draw bounding boxes on image"""
canvas.loadImage(img).then(image => {
const ct = canvas.createCanvas(image.width, image.height)
const ctx = ct.getContext('2d');
ctx.drawImage(image, 0, 0);
ctx.beginPath()
ctx.rect(boxes[0], boxes[1], boxes[2]-boxes[0], boxes[3]-boxes[1])
ctx.strokeStyle = 'red'
ctx.lineWidth = 4
ctx.stroke()
ctx.closePath()
if (output_url == null){
var img_out = ct.toBuffer('image/jpeg')
fs.writeFileSync('draw.jpg', img_out)
return;
}
var fileExt = output_url.split('.').pop();
var img_out = null
if (fileExt == 'jpg' || fileExt == 'jpeg') {
img_out = ct.toBuffer('image/jpeg')
}
else if (fileExt == 'png'){
img_out = ct.toBuffer('image/png')
}
else {
console.log('Output type is not supported')
return;
}
fs.writeFileSync(output_url, img_out)
})
}
async crop_face(img_url, output_url = null, return_img = false) {
const data = await this.detect(img_url)
var {tensor} = await this.load_img(img_url)
const boxes = data.boxes
// crop face
var cropped = tf.slice(tensor, [boxes[1], boxes[0]], [boxes[3]-boxes[1], boxes[2]-boxes[0]])
if (return_img){
if (output_url != null){
tf.node.encodeJpeg(cropped).then((f) => {
fs.writeFileSync(output_url, f)});
}
else{
tf.node.encodeJpeg(cropped).then((f) => {
fs.writeFileSync('cropped.jpg', f)});
}
}
return cropped
}
async detect(img_url) {
// """mtcnn image demo"""
var {tensor} = await this.load_img(img_url)
var data = await this.mtcnn.detect(tensor);
const {boxes, landmarks, scores} = data
if(boxes == null){
console.error('Image does not contain a face or image quality is too bad.\nPlease try another image.')
return false
}
boxes[0] = Math.max(0, boxes[0])
boxes[1] = Math.max(0, boxes[1])
const dict = {}
dict['boxes'] = boxes
dict['landmarks'] = landmarks
dict['scores'] = scores
return dict
}
}
exports.MTCNN = MTCNN;
// async function test(){
// const pnet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/pnet/model.json'
// const rnet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/rnet/model.json'
// const onet_url = 'https://storage.googleapis.com/my-mtcnn-models/final_model/onet/model.json'
// mtn = new MTCNN(pnet_url, rnet_url, onet_url)
// mtn.mtcnn.pnet = await mtn.mtcnn.pnet
// mtn.mtcnn.rnet = await mtn.mtcnn.rnet
// mtn.mtcnn.onet = await mtn.mtcnn.onet
// mtn.crop_face('/home/whoisltd/Downloads/peww.jpg', null, true)
// }
// test()