Building Cloak Of Invisibility Using Python

Let's get into Hogwartz 🌠

·

3 min read

Building Cloak Of Invisibility Using  Python

Hello, buddies! It is a dream of many of us to go to Hogwarts and learn magical spells just like Harry Potter and his friends. And you may remember Harry had a cloak that could turn him invisible. That cloak was known as Cloak of Invisibility.

Well, we are not creating Cloak of Invisibility; it will be a graphical trick that would resemble Invisibility Cloak, using Python!

Prerequisites

  • Python IDE
  • Numpy
  • OpenCV
pip install OpenCV

Coding Time!

Now, let the magic begin.

First of all, we should import the modules and Initialize the camera

To access the camera, we use the method cv2.VideoCapture(0) and set the capture object as a cap.

After that, we recognize the colors using cap.read().

import numpy as np
import cv2
import time

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('harry.avi' , fourcc, 20.0, (640,480))
time.sleep(2)

background = 0
for i in range(30):
    ret, background = cap.read()
while(cap.isOpened()):
    ret, img = cap.read()

    if not ret:
        break

All set! Now what we have to do is, detecting the cloth with the help of HSV.

What is HSV? HSV stands for HUE, SATURATION, and VALUE (or brightness). It is a cylindrical color space.

  • HUE: The hues are modeled as an angular dimension that encodes color information.
  • SATURATION: Saturation encodes the intensity of color.
  • VALUE: Value represents the brightness of the color.
hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    lower_val = np.array([0,120,70])
    upper_val = np.array([10,255,255])
    mask1 = cv2.inRange(hsv , lower_val , upper_val)

    lower_val = np.array([171,120,70])
    upper_val = np.array([180,255,255])
    mask2 = cv2.inRange(hsv , lower_val , upper_val)

    mask1 = mask1 + mask2
    mask1=cv2.morphologyEx(mask1, cv2.MORPH_OPEN ,np.ones((3,3) , np.uint8) , iterations=2)

    mask2=cv2.morphologyEx(mask1, cv2.MORPH_DILATE ,np.ones((3,3) , np.uint8) , iterations=1)

    mask2 = cv2.bitwise_not(mask1)
  • cv2.cvtColor() function converts colorspace.
  • Lower bound and Upper bound are the boundaries of red color.
  • cv2.inRange() function returns a segmented binary mask of the frame where the red color is present.

We have successfully detected the cloak. We want to show our previously-stored background in the mainframe where the cloak is present, First, we need to take the only white region from the background.

 res1 = cv2.bitwise_and(background, background, mask=mask1)
 res2 = cv2.bitwise_and(img, img, mask=mask2)

cv2.bitwise_and() applies mask on frame in the region where the mask is true (means white).

Finally, we have a cloak background and current frame background. Now it’s time to combine those to get a whole frame.

final_output = cv2.addWeighted(res1 , 1, res2 , 1, 0)
    final_output=cv2.flip(final_output,2)
    cv2.imshow('Buddy Potter' , final_output)
    k=cv2.waitKey(10)
    if k==27:
      break

cap.release()
cv2.destroyAllWindows()

Woah, that's it! Now just run it and see. Wait, there are more few things to note,

  • Make sure there is no other red-colored object in the frame.
  • Wear something other than red.
  • The red color cloth should be of proper shade.
  • Make sure you are out of frame when you run the code.
  • Come inside the frame when the webcam has captured the background image.

Get Full Code

Â