Commit fe036c90 by vincent

make drawLandmarks accept an array of landmarks

parent 24ec35d4
...@@ -21,6 +21,6 @@ export declare function drawText(ctx: CanvasRenderingContext2D, x: number, y: nu ...@@ -21,6 +21,6 @@ export declare function drawText(ctx: CanvasRenderingContext2D, x: number, y: nu
export declare function drawDetection(canvasArg: string | HTMLCanvasElement, detection: FaceDetection | FaceDetection[], options?: DrawBoxOptions & DrawTextOptions & { export declare function drawDetection(canvasArg: string | HTMLCanvasElement, detection: FaceDetection | FaceDetection[], options?: DrawBoxOptions & DrawTextOptions & {
withScore: boolean; withScore: boolean;
}): void; }): void;
export declare function drawLandmarks(canvasArg: string | HTMLCanvasElement, faceLandmarks: FaceLandmarks, options?: DrawLandmarksOptions & { export declare function drawLandmarks(canvasArg: string | HTMLCanvasElement, faceLandmarks: FaceLandmarks | FaceLandmarks[], options?: DrawLandmarksOptions & {
drawLines: boolean; drawLines: boolean;
}): void; }): void;
...@@ -161,22 +161,25 @@ function drawLandmarks(canvasArg, faceLandmarks, options) { ...@@ -161,22 +161,25 @@ function drawLandmarks(canvasArg, faceLandmarks, options) {
var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines; var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines;
var ctx = getContext2dOrThrow(canvas); var ctx = getContext2dOrThrow(canvas);
var lineWidth = drawOptions.lineWidth, color = drawOptions.color; var lineWidth = drawOptions.lineWidth, color = drawOptions.color;
if (drawLines) { var faceLandmarksArray = Array.isArray(faceLandmarks) ? faceLandmarks : [faceLandmarks];
ctx.strokeStyle = color; faceLandmarksArray.forEach(function (landmarks) {
ctx.lineWidth = lineWidth; if (drawLines) {
drawContour(ctx, faceLandmarks.getJawOutline()); ctx.strokeStyle = color;
drawContour(ctx, faceLandmarks.getLeftEyeBrow()); ctx.lineWidth = lineWidth;
drawContour(ctx, faceLandmarks.getRightEyeBrow()); drawContour(ctx, landmarks.getJawOutline());
drawContour(ctx, faceLandmarks.getNose()); drawContour(ctx, landmarks.getLeftEyeBrow());
drawContour(ctx, faceLandmarks.getLeftEye(), true); drawContour(ctx, landmarks.getRightEyeBrow());
drawContour(ctx, faceLandmarks.getRightEye(), true); drawContour(ctx, landmarks.getNose());
drawContour(ctx, faceLandmarks.getMouth(), true); drawContour(ctx, landmarks.getLeftEye(), true);
return; drawContour(ctx, landmarks.getRightEye(), true);
} drawContour(ctx, landmarks.getMouth(), true);
// else draw points return;
var ptOffset = lineWidth / 2; }
ctx.fillStyle = color; // else draw points
faceLandmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); }); var ptOffset = lineWidth / 2;
ctx.fillStyle = color;
landmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); });
});
} }
exports.drawLandmarks = drawLandmarks; exports.drawLandmarks = drawLandmarks;
//# sourceMappingURL=utils.js.map //# sourceMappingURL=utils.js.map
\ No newline at end of file
...@@ -15098,22 +15098,25 @@ ...@@ -15098,22 +15098,25 @@
var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines; var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines;
var ctx = getContext2dOrThrow(canvas); var ctx = getContext2dOrThrow(canvas);
var lineWidth = drawOptions.lineWidth, color = drawOptions.color; var lineWidth = drawOptions.lineWidth, color = drawOptions.color;
if (drawLines) { var faceLandmarksArray = Array.isArray(faceLandmarks) ? faceLandmarks : [faceLandmarks];
ctx.strokeStyle = color; faceLandmarksArray.forEach(function (landmarks) {
ctx.lineWidth = lineWidth; if (drawLines) {
drawContour(ctx, faceLandmarks.getJawOutline()); ctx.strokeStyle = color;
drawContour(ctx, faceLandmarks.getLeftEyeBrow()); ctx.lineWidth = lineWidth;
drawContour(ctx, faceLandmarks.getRightEyeBrow()); drawContour(ctx, landmarks.getJawOutline());
drawContour(ctx, faceLandmarks.getNose()); drawContour(ctx, landmarks.getLeftEyeBrow());
drawContour(ctx, faceLandmarks.getLeftEye(), true); drawContour(ctx, landmarks.getRightEyeBrow());
drawContour(ctx, faceLandmarks.getRightEye(), true); drawContour(ctx, landmarks.getNose());
drawContour(ctx, faceLandmarks.getMouth(), true); drawContour(ctx, landmarks.getLeftEye(), true);
return; drawContour(ctx, landmarks.getRightEye(), true);
} drawContour(ctx, landmarks.getMouth(), true);
// else draw points return;
var ptOffset = lineWidth / 2; }
ctx.fillStyle = color; // else draw points
faceLandmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); }); var ptOffset = lineWidth / 2;
ctx.fillStyle = color;
landmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); });
});
} }
var NetInput = /** @class */ (function () { var NetInput = /** @class */ (function () {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -90,17 +90,18 @@ ...@@ -90,17 +90,18 @@
const locations = await faceapi.locateFaces(input, minConfidence) const locations = await faceapi.locateFaces(input, minConfidence)
const faceTensors = (await faceapi.extractFaceTensors(input, locations)) const faceTensors = (await faceapi.extractFaceTensors(input, locations))
landmarksByFace = await Promise.all(faceTensors.map(t => faceapi.detectLandmarks(t))) let landmarksByFace = await Promise.all(faceTensors.map(t => faceapi.detectLandmarks(t)))
// free memory for face image tensors after we computed their descriptors // free memory for face image tensors after we computed their descriptors
faceTensors.forEach(t => t.dispose()) faceTensors.forEach(t => t.dispose())
landmarksByFace.forEach((landmarks, i) => { // shift and scale the face landmarks to the face image position in the canvas
// shift and scale the face landmarks to the face image position in the canvas landmarksByFace = landmarksByFace.map((landmarks, i) => {
const box = locations[i].forSize(width, height).getBox() const box = locations[i].forSize(width, height).getBox()
faceapi.drawLandmarks(canvas, landmarks.forSize(box.width, box.height).shift(box.x, box.y), { lineWidth: drawLines ? 2 : 4, drawLines, color: 'red' }) return landmarks.forSize(box.width, box.height).shift(box.x, box.y)
}) })
faceapi.drawLandmarks(canvas, landmarksByFace, { lineWidth: drawLines ? 2 : 4, drawLines, color: 'red' })
faceapi.drawDetection('overlay', locations.map(det => det.forSize(width, height))) faceapi.drawDetection('overlay', locations.map(det => det.forSize(width, height)))
} }
......
...@@ -211,7 +211,7 @@ function drawContour( ...@@ -211,7 +211,7 @@ function drawContour(
export function drawLandmarks( export function drawLandmarks(
canvasArg: string | HTMLCanvasElement, canvasArg: string | HTMLCanvasElement,
faceLandmarks: FaceLandmarks, faceLandmarks: FaceLandmarks | FaceLandmarks[],
options?: DrawLandmarksOptions & { drawLines: boolean } options?: DrawLandmarksOptions & { drawLines: boolean }
) { ) {
const canvas = getElement(canvasArg) const canvas = getElement(canvasArg)
...@@ -229,21 +229,25 @@ export function drawLandmarks( ...@@ -229,21 +229,25 @@ export function drawLandmarks(
const ctx = getContext2dOrThrow(canvas) const ctx = getContext2dOrThrow(canvas)
const { lineWidth, color } = drawOptions const { lineWidth, color } = drawOptions
if (drawLines) { const faceLandmarksArray = Array.isArray(faceLandmarks) ? faceLandmarks : [faceLandmarks]
ctx.strokeStyle = color
ctx.lineWidth = lineWidth faceLandmarksArray.forEach(landmarks => {
drawContour(ctx, faceLandmarks.getJawOutline()) if (drawLines) {
drawContour(ctx, faceLandmarks.getLeftEyeBrow()) ctx.strokeStyle = color
drawContour(ctx, faceLandmarks.getRightEyeBrow()) ctx.lineWidth = lineWidth
drawContour(ctx, faceLandmarks.getNose()) drawContour(ctx, landmarks.getJawOutline())
drawContour(ctx, faceLandmarks.getLeftEye(), true) drawContour(ctx, landmarks.getLeftEyeBrow())
drawContour(ctx, faceLandmarks.getRightEye(), true) drawContour(ctx, landmarks.getRightEyeBrow())
drawContour(ctx, faceLandmarks.getMouth(), true) drawContour(ctx, landmarks.getNose())
return drawContour(ctx, landmarks.getLeftEye(), true)
} drawContour(ctx, landmarks.getRightEye(), true)
drawContour(ctx, landmarks.getMouth(), true)
return
}
// else draw points // else draw points
const ptOffset = lineWidth / 2 const ptOffset = lineWidth / 2
ctx.fillStyle = color ctx.fillStyle = color
faceLandmarks.getPositions().forEach(pt => ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth)) landmarks.getPositions().forEach(pt => ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth))
})
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment