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
an 800×600 image, but you will most likely need to play with its
positioning. To do that, experiment with the constants in
and
above. When you do, make sure that any changes you make to the
first line are mirrored in the second line, or your script will probably
fail with an error. (If you want to move the mustache to the left or
right, change the y associated constant; up or down, change the x
constant. It’s a bit counter-intuitive.)
You can also play with different size images and see if they give you
any better results.
Going Further
PyImage Search
Any work with OpenCV and the Raspberry Pi has to take into ac-
count Dr. Adrian Rosebrock’s excellent PyImage Search site. This
site is exceptionally well done and has a wealth of information
about almost anything you care to learn about using OpenCV,
whether on Linux or the Pi or even other operating systems. In
addition, he’s been branching out into machine learning, neural
networks, and AI. Definitely worth your time to peruse and book-
mark.
OpenCV Documentation
As always, checking out the online documentation for OpenCV
is never a bad idea. In my opinion, the docs for OpenCV are not
very well indexed or user-friendly, but there is a
lot
of information
there and it may be worthwhile to browse if you have questions
or even if you’re just curious as to everything OpenCV can do.
Working with Cameras 165 165
GSW_RASPI_4ED_FIN.indd 165GSW_RASPI_4ED_FIN.indd 165 10/28/21 10:54 AM10/28/21 10:54 AM
GSW_RASPI_4ED_FIN.indd 166GSW_RASPI_4ED_FIN.indd 166 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset