Remove image background in node

4.3k views Asked by At

I am working on the project where I am capturing user's face from and putting it on the existing model. To combine face and model image I am using gm library from node; and for face detection I am using opencv. I am able to do both detect face and put it on the model. But the issue is my model is having transparent background but user's face not (because face is captured from camera).

I want to remove the background of face and make it transparent. Is there any way to make this possible?

Here is my code for integration:

gm()
            .in('-page', '+0+0')
            .in("public/" + modelImagePath) 
            .in('-page', '+130+45')  
            .in(newDir + '/crp.png')
            .mosaic()
            .write(resultantImage, function (err) {
               if (err) console.log(err);

            });

Here crp.png is face image and modelimagepath is model image Complete opencv + gm code:

cv.readImage(newDir + "/zxcv.png", function(err, im){
      im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
        console.log("into detec:" + faces.length + "," + faces[0]);

        if ( faces.length > 0)
        {
            var x = faces[0];
            gm(newDir + "/zxcv.png")
            .crop(x.width,x.height,x.x,x.y)
            .write(newDir + '/crp.png', function (err) {
                if (err) console.log(err);
             });

            var modelImagePath = getModelBasedOnSize(hips, skin, waist, weight, 1);
            var modelSize = modelImagePath.split("-")[1].split(".")[0];
            resultantImage = newDir + "/result-" + modelSize + ".png";
            gm()
            .in('-page', '+0+0')
            .in("public/" + modelImagePath) 
            .in('-page', '+130+45') // frok -> 95 87 //+105+60 // 
            .in(newDir + '/crp.png')
            .mosaic()
            .write(resultantImage, function (err) {
               if (err) console.log(err);
               res.end(resultantImage.replace('public/', ''));
               //res.end(resultImage.replace('public/', ''));
            });
        }
        else{
            console.log("into else - pic not present");
            var modelImagePath = getModelBasedOnSize(hips, skin, waist, weight, 0);
            res.end(modelImagePath.replace('public/', ''));
        }
      }); //opencv detect face
    })
0

There are 0 answers