Advertisement

Active Hackathon

My Fun AI Project – How To Be Invisible?

In this implementation, we will learn how to create our own ‘Invisibility Cloak’ using simple computer vision techniques in OpenCV. Here we are using OpenCV because it provides the best-inbuilt libraries to implement this in a few steps.
invisible using opencv

Most of you must have heard about the Harry Potter series. In that series, Harry potter used an invisibility cloak, that makes him invisible…! Are you wondering how they made invisible cloaks…..? That’s not as tough as rocket science to build this invisibility cloak. By using simple techniques in the OpenCV we can implement this invisible cloak. This concept is similar to the green mat used in movie shootings. After shooting, they add graphics in the place of a green mat to give the output.

In this implementation, we will learn how to create our own ‘Invisibility Cloak’ using simple computer vision techniques in OpenCV. Here we are using OpenCV because it provides the best-inbuilt libraries to implement this in a few steps.

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.

Steps used for this implementation on Being invisible using OpenCV:-

  • Defining the function 
  • Capturing and storing the background
  • Capturing the live video using a webcam
  • Displaying the output

So, now let’s begin with this interesting implementation.

Importing libraries 

import numpy as np

import cv2

Step – 1 – Defining the function 

def detect_func(frame, background):

    hsv_image = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)  

Using the below code snippet, we will assign the values of sensitivity, Hue, and range for blue color.

    sensitivity = 20

    H_Value = 20   

    light_blue = np.array([H_Value - sensitivity, 60, 60])

    dark_blue = np.array([H_Value + sensitivity, 255, 255])

In the above implementation, we have used blue color values you can replace with another color just by changing those values

Using the below code snippet, we will use morphing techniques to create the mask for the blue-colored region.

    create_mask = cv2.inRange(hsv_image, light_blue, dark_blue)  

    kernel_size = 15

    kernel = np.ones((kernel_size, kernel_size), np.uint8)

    closing = cv2.morphologyEx(create_mask, cv2.MORPH_CLOSE, kernel)

    contours, _ = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    sorted = sorted(contours, key=cv2.contourArea, reverse=True)[:5]

    mask = cv2.fillPoly(np.zeros((500, 500, 3), dtype=np.uint8), pts =[cont_sorted[0]], color=(255,255,255))

In the above implementation, the bigger sized kernel gives fewer gaps on the image

In the below code snippet, we will use the cv2.fillPoly function to merge the mask and background frames which displays the final output.

    object_mask = cv2.fillPoly(frame, pts =[cont_sorted[0]], color=(0,0,0))

    background = np.bitwise_and(contour_mask, background)

    final_img = cv2.bitwise_or(object_mask, background

    return final_img

To manipulate the video by using a mask, we have used bitwise_and and bitwise_or operations.

Step – 2 – Capturing and storing the background

cap = cv2.VideoCapture(0)

ret, background = cap.read()

background = cv2.resize(background, (500, 500))

cv2.imshow('back_img', background)

cv2.waitKey(0)

cv2.destroyAllWindows()

invisible using opencv

Step – 3 – Capturing the live video and applying an invisible function to it.

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

output = cv2.VideoWriter('output.mp4', fourcc, 30.0, (500,500))

while(True):

    ret, frame = cap.read()

    frame = cv2.resize(frame, (500, 500))

    image = detect_blue(frame, background)

    output.write(image)

Step – 4 – Displaying the results 

cv2.imshow('Img',image)

    if cv2.waitKey(1) & 0xFF == ord('q'):

cap.release()

output.release()

cv2.destroyAllWindows()

Final Output:-

Video Link 

Conclusion

In the above demonstration, we demonstrated how to create an invisible cloak by using very simple functions in OpenCV. It is a simple and interesting implementation that requires using a few lines of code. 

More Great AIM Stories

Prudhvi varma
AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.

Our Upcoming Events

Conference, Virtual
Genpact Analytics Career Day
3rd Sep

Conference, in-person (Bangalore)
Cypher 2022
21-23rd Sep

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
21st Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM
MOST POPULAR

Data Science Skills Survey 2022 – By AIM and Great Learning

Data science and its applications are becoming more common in a rapidly digitising world. This report presents a comprehensive view to all the stakeholders — students, professionals, recruiters, and others — about the different key data science tools or skillsets required to start or advance a career in the data science industry.

How to Kill Google Play Monopoly

The only way to break Google’s monopoly is to have localised app stores with an interface as robust as Google’s – and this isn’t an easy ask. What are the options?

[class^="wpforms-"]
[class^="wpforms-"]