Show virtual objects (hats and glasses) on a face using OpenCV.
First, we detect face and scale width and height of hat source accordingly.
Then, we resize hat source and hat mask (mask is for transparent background):
cv.resize(hatSrc, hatDst, size);
cv.resize(hatMask, hatMaskDst, size);
After that, we calculate absolute coordinates for hat.
Using these coordinates we copy result image to the main video source src
and apply transparent mask:
hatFrames[i].src.copyTo(src.rowRange(hatFrames[i].y1, hatFrames[i].y2)
.colRange(hatFrames[i].x1, hatFrames[i].x2),
hatFrames[i].mask);
Similar algorithm is used to resize and draw glasses.
Additionaly, we rotate glasses using warpAffine(...)
function and rotation matrix:
cv.warpAffine(glassesDst, glassesDst, glasses.rotMat, rSize);
cv.warpAffine(glassesMaskDst, glassesMaskDst, glasses.rotMat, rSize);