Invisible Cloak of Potter With Python

Introduction: Invisible Cloak of Potter With Python

Who does not want to acquire super power? Super power of being INVISIBLE? YES you can do so with just a few lines of codes .So what are you waiting for ? Prepare yourself for the surprise . Lets get started.......

Its a simple computer vision technique which create a magical effect . The technique is called color detection and segmentation. Here we will be using Red colored cloth to use as a cloak . You can also use green or blue or anycolored clothes with some minute changes in the code and create that magical effect .

Step 1: Install Python

First You have to make sure that you have python installed in your device , might be python 3 or have Jupyter Notebook . I have done my project in Jupyter Notebook. It provides a simple browser based IDE for python to do your coding stuffs.

You can download from this link - https://www.anaconda.com/products/individual

Step 2: Install OpenCV

After that you have to install OpenCV . Open Jupyter Notebook and write the following command to install the required OpenCV modules :-

pip install opencv-python


I have already installed OpenCV , so it is showing :- Requirement already satisfied.

Step 3: Algorithm to Be Used

  • Give some time to Capture the background frame.
  • Now detection of red/green/blue color using color detection and segmentation algo.

  • Segment out the red/green/blue colored cloth .

  • Now show the final magical output.

Step 4: Magical Code

#Invisible Cloak By A.D

import numpy as np
import cv2
import time
capture=cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
final = cv2.VideoWriter('pOtterF.avi' , fourcc, 20.0, (640,480))
time.sleep(2)
background = 0 #capture the background
for i in range(30):
    ret, background = capture.read()#capture the image
    
    
while(capture.isOpened()):
    ret, img = capture.read()
    
    if not ret:
        break
        
    hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    #for red 
    lower_red = np.array([0,120,70])
    upper_red = np.array([20,255,255])
    mask1 = cv2.inRange(hsv , lower_red , upper_red)
    
    lower_red = np.array([170,120,70])
    upper_red = np.array([180,255,255])
    mask2 = cv2.inRange(hsv , lower_red , upper_red)
    
    #for Green comment out the above 6 lines of code for red color and use the following three lines
    # lower_green = np.array([25,52,72])
    # upper_green = np.array([102,255,255])
    # mask1 = cv2.inRange(hsv , lower_green , upper_green)
    
    #for blue comment out the above 6 lines of code for red color and use the following three lines
    #lower_blue = np.array([94, 80, 2])
    #upper_blue = np.array([126, 255, 255])
    #mask1 = cv2.inRange(hsv , lower_blue , upper_blue)
    
    mask1=mask1+mask2  # for green and blue remove this line

    mask1=cv2.morphologyEx(mask1, cv2.MORPH_OPEN ,np.ones((3,3) , np.uint8) , iterations=2)
    mask1=cv2.morphologyEx(mask1, cv2.MORPH_DILATE ,np.ones((3,3) , np.uint8) , iterations=1)
    
        
    mask2 = cv2.bitwise_not(mask1) 
    
    
    
    res1 = cv2.bitwise_and(background, background, mask=mask1)
    res2 = cv2.bitwise_and(img, img, mask=mask2)
    
    final_output = cv2.addWeighted(res1 , 1, res2 , 1 , 0)
    final.write(final_output)
    
    cv2.imshow('pOtterF' , final_output)
    if cv2.waitKey(1) & 0xFF == ord('q'):  #press 'q' to close the window
        break
        
capture.release()
final.release()
cv2.destroyAllWindows()

Step 5: Now Enjoy the Magic

After you run the code wait for some time for the web cam to capture the background. Then you enter the scene with your RED/GREEN/BLUE cloak and enjoy the super power of being invisible .

Step 6: Finished Work

Be the First to Share

    Recommendations

    • Back to School: Student Design Challenge

      Back to School: Student Design Challenge
    • Robots Contest

      Robots Contest
    • Organization Contest

      Organization Contest

    Comments