Virtual Makeup App using Streamlit and OpenCV

Pavan Kunchala
3 min readJun 10, 2022

--

This is how the UI will look like
Application Demo

Today I am gonna show you how I created this Virtual makeup Application using Streamlit and OpenCV

You can check out the code for this application from here, you will also need a pre-trained face parsing model which you can download from here

If you want to learn more about the original model you can check out this repo

I have also built many other cool demos using ComputerVision and VR you can check them out from here(don’t forget to start the repo for getting the updates)

That's enough of the intro, let's get right into the code

Importing Libraries

We are using Streamlit for creating the UI for this application, and OpenCV for image processing, you can install them using pip with the following code

pip install streamlit
pip install opencv-python

We are also gonna use PIL for its filters and more.

Creating functions

We are gonna create some functions to sharpen the image as well as parse the hair

Hair parsing results

Title and File Uploader

Let's get into the application part then. We are using streamlit’s file uploader for dynamically uploading different images to test out

In the above snippet first I created a variable for Demo-Image which the applications uses as default. I used st.title() and st.sidebar.title() for adding the title to the application.

I also created a dictionary called table which matches the name to the number in the face parser. You can also add to it and also parse the face if you want check out the main repo for a better understanding.

Show and Resize

We have used st.image() function to show the original image and are resizing it to 1024*1024 to make it compatible with the model. We have created a variable with the path of the model as well.

Evaluate and Show

We have created a function called evaluate() which you can find in the test.py file from here, make sure you download this repo for everything to run smoothly.

I created a color-picker using streamlit’s color_picker() function and used PIL library to make it compatible to apply to the image.

Finally, I resize the output image and voila an image-based Virtual Makeup App is ready.

To run the application type the following in your terminal

streamlit run app.py

Just replace the app.py with your code’s file name.

I have also deployed this application on the web you can check it out here: https://share.streamlit.io/pavankunchala/virtual_makeup_streamlit/main/app.py

Here are some of the results I got.

Keep in mind that this blog is about how to use the face-parser model and create an application with streamlit, so I haven’t really gone into the code of how the face parser works. You can check out the main repo for a better understanding.

P.S: I know there is a lot more to improve with my explanation and its mostly code, I will try to update them as quickly as possible and give your suggestions in my email(possible improvements or topics I should write an article about) and If you want to talk about this or any ML or computer vision topics do message me on my Linkedin here

You can also check out my GitHub from here(If you like the content don’t forget to follow)

I also showcase some of my work you can check it out here

I am also looking for Freelancing or Full-time opportunities in the field of Deep Learning and Computer vision if you are willing to collaborate, mail me here( pavankunchalapk@gmail.com)

Have a wonderful day!

--

--

Pavan Kunchala
Pavan Kunchala

Written by Pavan Kunchala

Machine learning & Computer Vision Engineer |Deep learning and Reinforcement learning enthusiast

Responses (2)