Showing
2 changed files
with
26 additions
and
5 deletions
... | @@ -117,18 +117,28 @@ function main() | ... | @@ -117,18 +117,28 @@ function main() |
117 | let msize = new cv.Size(video.width / 4, video.height / 4); | 117 | let msize = new cv.Size(video.width / 4, video.height / 4); |
118 | classifier.detectMultiScale(dst, faces, 1.1, 3, 0, msize); | 118 | classifier.detectMultiScale(dst, faces, 1.1, 3, 0, msize); |
119 | // draw faces. | 119 | // draw faces. |
120 | - console.log('draw faces'); | ||
121 | for (let i = 0; i < faces.size(); ++i) { | 120 | for (let i = 0; i < faces.size(); ++i) { |
122 | let face = faces.get(i); | 121 | let face = faces.get(i); |
123 | let point1 = new cv.Point(face.x, face.y); | 122 | let point1 = new cv.Point(face.x, face.y); |
124 | let point2 = new cv.Point(face.x + face.width, face.y + face.height); | 123 | let point2 = new cv.Point(face.x + face.width, face.y + face.height); |
125 | cv.rectangle(dst, point1, point2, [255, 0, 0, 255], 8); | 124 | cv.rectangle(dst, point1, point2, [255, 0, 0, 255], 8); |
126 | let cropped = new cv.Mat(); | 125 | let cropped = new cv.Mat(); |
127 | - let rect = new cv.Rect(Math.max(face.x-22, 0), Math.max(face.y-44, 0), Math.min(face.width+44, src.cols), Math.min(face.height+66, src.rows)); | 126 | + let margin_x = 0; |
127 | + let margin_y = 0; | ||
128 | + if (face.width > face.height) | ||
129 | + { | ||
130 | + margin_y = (face.width - face.height) / 2; | ||
131 | + } | ||
132 | + else | ||
133 | + { | ||
134 | + margin_x = (face.height - face.width) / 2; | ||
135 | + } | ||
136 | + Math.max(face.width, face.height) | ||
137 | + Math.min(face.width, face.height) | ||
138 | + let rect = new cv.Rect(Math.max(face.x-margin_x, 0), Math.max(face.y-margin_y, 0), Math.min(face.width+margin_x, src.cols), Math.min(face.height+margin_y, src.rows)); | ||
128 | cropped = src.roi(rect); | 139 | cropped = src.roi(rect); |
129 | let tempCanvas = document.createElement("canvas"); | 140 | let tempCanvas = document.createElement("canvas"); |
130 | cv.imshow(tempCanvas,cropped); | 141 | cv.imshow(tempCanvas,cropped); |
131 | - console.log('b64encode'); | ||
132 | if (tracker.register(face.x, face.y, face.width, face.height)){ | 142 | if (tracker.register(face.x, face.y, face.width, face.height)){ |
133 | let b64encoded = tempCanvas.toDataURL("image/jpeg", 1.0); | 143 | let b64encoded = tempCanvas.toDataURL("image/jpeg", 1.0); |
134 | b64encoded = b64encoded.replace('data:image/jpeg;base64,', ''); | 144 | b64encoded = b64encoded.replace('data:image/jpeg;base64,', ''); | ... | ... |
... | @@ -119,14 +119,25 @@ function main() | ... | @@ -119,14 +119,25 @@ function main() |
119 | let msize = new cv.Size(video.width / 4, video.height / 4); | 119 | let msize = new cv.Size(video.width / 4, video.height / 4); |
120 | classifier.detectMultiScale(dst, faces, 1.1, 3, 0, msize); | 120 | classifier.detectMultiScale(dst, faces, 1.1, 3, 0, msize); |
121 | // draw faces. | 121 | // draw faces. |
122 | - console.log('draw faces'); | ||
123 | for (let i = 0; i < faces.size(); ++i) { | 122 | for (let i = 0; i < faces.size(); ++i) { |
124 | let face = faces.get(i); | 123 | let face = faces.get(i); |
125 | let point1 = new cv.Point(face.x, face.y); | 124 | let point1 = new cv.Point(face.x, face.y); |
126 | let point2 = new cv.Point(face.x + face.width, face.y + face.height); | 125 | let point2 = new cv.Point(face.x + face.width, face.y + face.height); |
127 | cv.rectangle(dst, point1, point2, [255, 0, 0, 255], 8); | 126 | cv.rectangle(dst, point1, point2, [255, 0, 0, 255], 8); |
128 | let cropped = new cv.Mat(); | 127 | let cropped = new cv.Mat(); |
129 | - let rect = new cv.Rect(Math.max(face.x-22, 0), Math.max(face.y-44, 0), Math.min(face.width+44, src.cols), Math.min(face.height+66, src.rows)); | 128 | + let margin_x = 0; |
129 | + let margin_y = 0; | ||
130 | + if (face.width > face.height) | ||
131 | + { | ||
132 | + margin_y = (face.width - face.height) / 2; | ||
133 | + } | ||
134 | + else | ||
135 | + { | ||
136 | + margin_x = (face.height - face.width) / 2; | ||
137 | + } | ||
138 | + Math.max(face.width, face.height) | ||
139 | + Math.min(face.width, face.height) | ||
140 | + let rect = new cv.Rect(Math.max(face.x-margin_x, 0), Math.max(face.y-margin_y, 0), Math.min(face.width+margin_x, src.cols), Math.min(face.height+margin_y, src.rows)); | ||
130 | cropped = src.roi(rect); | 141 | cropped = src.roi(rect); |
131 | let tempCanvas = document.createElement("canvas"); | 142 | let tempCanvas = document.createElement("canvas"); |
132 | cv.imshow(tempCanvas,cropped); | 143 | cv.imshow(tempCanvas,cropped); | ... | ... |
-
Please register or login to post a comment