Sunglass Attach using CV2

Sunglass Attach using CV2

Introduction

  • In this LinkedIn article, we'll explore how to use Python's OpenCV library to automatically add stylish sunglasses to a portrait photo. Let's dive into the code and uncover the magic!
  • Profile pictures are often our first impression in the digital world. Adding a twist of creativity can make your profile stand out.
  • We're going to showcase how to use OpenCV, a powerful computer vision library, to programmatically place sunglasses on a person's face in an image.




Step by Step Guide

Loading Images

  • The script loads a sunglass image and a destination image (the photo to which we want to add sunglasses).


sunglass_image = cv2.imread("b.png", cv2.IMREAD_UNCHANGED
    destination_image = cv2.imread("a.jpg"))        


Image

No alt text provided for this image


No alt text provided for this image



Face Detection

  • The code uses a pre-trained Cascade classifier to detect faces in the destination image. If no faces are detected, the script gracefully exits.


face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    gray_image = cv2.cvtColor(destination_image, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)


    if len(faces) == 0:
        print("No face detected in the destination image.")
        return        



Positioning the Sunglasses

  • Assuming a single face is detected, the script calculates the dimensions and position to place the sunglasses. The sunglasses are resized to match the face's width and positioned at a specific height above the eyes.


 sunglass_resized = cv2.resize(sunglass_image, (w, int(0.4 * h))
    x_offset = x
    y_offset = y + int(0.3 * h)        



Alpha Channel Handling

  • The code checks if the sunglass image has an alpha channel (transparency). If it does, the sunglasses are blended into the destination image using the alpha channel to ensure a smooth integration. If not, the sunglasses are placed without transparency.


 if sunglass_resized.shape[2] == 4:  # Check if sunglass image has an alpha channe 1
        alpha_s = sunglass_resized[:, :, 3] / 255.0
        alpha_d = 1.0 - alpha_s
        for c in range(0, 3):
            destination_image[y_offset:y_offset + sunglass_resized.shape[0], x_offset:x_offset + sunglass_resized.shape[1], c] = (
                alpha_s * sunglass_resized[:, :, c] + alpha_d * destination_image[y_offset:y_offset + sunglass_resized.shape[0], x_offset:x_offset + sunglass_resized.shape[1], c]
            )
    else:
        for c in range(0, 3):
            destination_image[y_offset:y_offset + sunglass_resized.shape[0], x_offset:x_offset + sunglass_resized.shape[1], c] = (
                sunglass_resized[:, :, c]
            )        



Saving the Output

  • Finally, the modified image with the sunglasses is saved to a file called "output_image.jpg."


cv2.imwrite('output_image.jpg', destination_image)        



Output

No alt text provided for this image




Feedback/Queries

I hope this article will be useful for you and that you learned something new Please, feel free to drop any questions in the comments below. I would be happy to answer them.

Thanks for reading 💚



To view or add a comment, sign in

More articles by Chandan Kumar Singh

  • NumPy Image Creation

    Introduction Are you fascinated by the endless possibilities of Python programming? Ready to delve into the realm of…

  • Multiple Video Streaming Concurrently

    Introduction In the realm of computer vision and image processing, OpenCV (Open Source Computer Vision Library) stands…

  • Magic of Speech-to-Text Transcription

    Introduction Are you ready to dive into the incredible world of Speech-to-Text Transcription? 🚀 This technology is…

  • Running Docker Inside Docker

    Introduction Docker has revolutionized how we develop and deploy applications, but what if you could take it a step…

  • CURD Operation on Amazon S3 Bucket

    Introduction Amazon Simple Storage Service (S3) is one such prominent cloud storage service offered by Amazon Web…

    1 Comment
  • Launching an Amazon EC2 Instance Using Python and Boto3

    Introduction Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the…

    1 Comment
  • Inside docker container install Firefox and start Firefox

    Introduction Docker has revolutionized the way we deploy and manage applications, and now, with just a few simple…

  • Install Docker on Local System

    Introduction Docker has revolutionized the way we build, ship, and run applications. Its lightweight and…

  • Power of Concurrent Python Programming

    Introduction In this article, we'll embark on an exciting journey to explore the world of concurrent Python programming…

    1 Comment
  • Menu-Driven Python Application using tkinter

    Introduction Introduce the concept of a menu-driven Python application. Mention the purpose of the script and its…

Others also viewed

Explore content categories