question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to resize the aligned face images to a constant fixed size?

See original GitHub issue

Hi I am trying to display the list of detected and aligned face images in a scroll div in html5. I am able to detect and append into the div dynamically however the size of the resultant face images are all different. At which function do I change the width and height so let’s say they will all be 160x160.

const input = await faceapi.toNetInput(videoEl)
        const locations = await faceapi.locateFaces(input, minConfidence)
        //const resized_locations = locations.map(det => det.forSize(160, 160))
        const faceImages = await faceapi.extractFaces(input.inputs[0], locations)

        // detect landmarks and get the aligned face image bounding boxes
        const alignedFaceBoxes = await Promise.all(faceImages.map(
            async (faceCanvas, i) => {
            const faceLandmarks = await faceapi.detectLandmarks(faceCanvas)
            return faceLandmarks.align(locations[i])
            }
        ))
        const alignedFaceImages = await faceapi.extractFaces(input.inputs[0], alignedFaceBoxes)
        
        // free memory for input tensors
        input.dispose()

        //$('#facesContainer').empty()
        faceImages.forEach(async (faceCanvas, i) => {
            $('#facesContainer').append(alignedFaceImages[i])
            percentage = percentage + 5;
        })

These are the script i used for detect and append. I tried resizing the faceCanvas, or looping through #faceContainer and resize them, or mapping the alignedFaceImages to 160x160 size but all to no avail. Any assistance is much appreciated. Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stephenxp04commented, Jul 12, 2018

I did the html5 canvas method. Since individual alignedFaceImages element is a HTML canvas just create a new canvas children with size I wanted, then draw the alignedFaceImages[i] into the new canvas context inside the forEach loop.

var res = document.createElement("canvas")
            res.width = 160
            res.height = 160
            res_con = res.getContext("2d")
            res_con.drawImage(alignedFaceImages[i], 0, 0, 160, 160)

i know the approach is rather simple not sure about the optimization since this is just POC.

Not familiar with the tensor method though, it is possible to use tf function within js?

0reactions
pravynandascommented, Jun 23, 2020

I did the html5 canvas method. Since individual alignedFaceImages element is a HTML canvas just create a new canvas children with size I wanted, then draw the alignedFaceImages[i] into the new canvas context inside the forEach loop.

var res = document.createElement("canvas")
            res.width = 160
            res.height = 160
            res_con = res.getContext("2d")
            res_con.drawImage(alignedFaceImages[i], 0, 0, 160, 160)

i know the approach is rather simple not sure about the optimization since this is just POC.

Not familiar with the tensor method though, it is possible to use tf function within js?

Just in case if somebody looking for a canvas without “stretching” the image, you can do x,y offsets. My solution is like below

faceImages.forEach((face)=>{ var res = canvas.createCanvas(200, 200) let h = face.height; let w = face.width; let a = 0; let b = 0; if (h <= 200 && w <= 200) { a = (200 - w) / 2; b = (200 - h) / 2; res.getContext(“2d”).drawImage(face, a, b, w, h) } else { res.getContext(“2d”).drawImage(face, 0, 0, 200, 200) } //save or display canvas })

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resize, Fit and Align Images with Filestack and React
resize =align:center, top, bottom, left, right, or faces : using align, you can choose the area of the image to focus on.
Read more >
How can I make all images of different height and width the ...
1. make the divs fixed height/width, then stuff in the images with an overflow:none , and center the image horizontally/vertically within the div....
Read more >
Resizing or Scaling -- IM v6 Examples - ImageMagick
The most obvious and common way to change the size of an image is to resize or scale an image. The content of...
Read more >
Resizing background images with background-size - CSS
The background-size CSS property lets you resize the background ... To do this, we can use a fixed background-size value of 150 pixels....
Read more >
How to Adjust Enlarge and Edit Image Size in ... - YouTube
This Microsoft Word 2016 tutorial shows you how to resize pictures and images in MS Office 365. I show how to increase and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found