pixels in the mustache image and create a mask and an inverted
mask.
After capturing the image, you must write it to a file and then
reopen it for editing. The raw image fresh from the capture is
non-editable, even by sudo.
This finds the mouth(s) in the grayscale image, using the Haar
cascade.
This determines a
region of interest
(ROI) above the mouth
where we are going to place the mustache.
This masks the face with the mustache image.
This masks the non-mustache portion of the mustache image
with the portion of the face image that lies within the ROI.
This line literally adds the two images together.
Finally, replace the ROI of the face image with the mustache +
face image we just created.
Now you’re ready to give it a try. Make sure your camera is connected.
Next, go to the terminal, change to the
photobooth
directory, and
then run the script:
$ python3 photobooth.py
Point the camera toward your face and press the button. If all of
the parameters are correct and you’re pointing the camera in the
right direction, you’ll see an image on the screen similar to the one
above.
TROUBLESHOOTING:
This is where some serious experimentation on your part maybe
necessary. The mouth classifier is not nearly as well-trained as the
face classifier we used earlier, so you’re bound to get either some
false positives or the program won’t recognize any mouths in the
image at all, even if you’re grinning from ear to ear at the camera.
If either of these things happen, play around with the values in
above. Also, make sure your face is well-lit, the camera is pointed
directly at you, and your face is centered in the frame.
Next, there’s the placement of the mustache. I’ve sized the one
we’re using here to fit pretty well on a well-centered face inside
164 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 164GSW_RASPI_4ED_FIN.indd 164 10/28/21 10:54 AM10/28/21 10:54 AM