Add Virtual Glasses

Let’s learn how to digitally position frames over a facial image.

Introduction

It’s often overwhelming to pick out new glasses after an annual vision test with the eye doctor or when looking for a new pair of sunglasses. Sometimes, you need to try many styles and options before finding the perfect pair. Generally, a virtual glasses try-on utility involves computer vision, augmented reality, and image processing technology. Using the virtual try-on utility discussed in this lesson, we can start a virtual test of preselected eyeglasses or sunglasses in order to see how they fit by uploading a digital face photo.

Objective

Explore augmented reality with this lesson, where we look at the steps required for developing a lightweight, virtual glasses try-on utility in the Python programing language.

Dependencies

We’ll be using the following external Python libraries:

Library

Version

mediapipe

0.8.9

opencv-python

4.4.0.46

scipy

1.5.4

numpy

1.19.4

filetype

1.0.7

Let’s code the utility!

Let’s look at the principal functions of this utility.

The resize_img function

This function reshapes a selected image (the glasses frame) to a specific width (the face width) while maintaining its aspect ratio. Let’s define this function. Line 5 gets the dimensions of the image.

  • Line 8 calculates the resizing ratio.

  • Line 10 defines the new dimensions. The new height will be computed based on the resizing ratio.

  • Line 12 ...

Access this course and 1400+ top-rated courses and projects.