So guys, one of the most awaited blogs is here. Today we are going to code Harry’s Invisibility Cloak in less than 50 lines of code and you will be totally amazed after watching the results. Yeah Yeah, I know you can’t control your emotions 🙂
Sneak Peek at our Result…
This is going to be a very interesting blog, so without any further due, Let’s do it…
Code for Harry’s Invisibility Cloak…
import cv2 import numpy as np import time cap = cv2.VideoCapture(0) for i in range(30): ret,background = cap.read() background = np.flip(background,axis=1) while(1): ret, frame = cap.read() img = np.flip(frame,axis=1) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) blurred = cv2.GaussianBlur(hsv, (35, 35),0) lower = np.array([2,170,0]) upper = np.array([28,255,255]) mask = cv2.inRange(hsv,lower,upper) mask = cv2.erode(mask,np.ones((7,7),np.uint8)) mask = cv2.dilate(mask,np.ones((19,19),np.uint8)) img[np.where(mask==255)] = background[np.where(mask==255)] cv2.imshow('MAGIC',img) if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cap.release() # This was the code for Harry's Invisibility Cloak
- Line 1-3 – Importing required libraries.
- Line 5 – Instantiating the VideoCapture() object to acces webcam later.
- Line 7- 8 – Initially record the background. We are running the loop till the 30th frame and the 30th frame is kept as the background image. Remember you need not be in the frame when you are recording the background and also don’t change the position of your laptop/camera after recording the background.
- Line 10 – Simply flipping the background image horizontally.
- Let’s run the loop…
- Line 13 – Read the current frame using the VideoCapture() object we made above.
- Line 15 – Flip the image.
- Line 16 – Convert the image to HSV color space because we will take out the mask from it in further steps and for masking purposes, HSV works best.
- Line 17 – Gaussian Blur the image to reduce Gaussian Noise.
- Line 19-20 – Specifying the range of the color in which you will be using a cloak. I used an orange color cloak. These are orange color’s upper and lower HSV ranges.
- Line 21 – Creating the mask out of the image. This means when we will show the cloak in the frame it will mark only that region as white and all others as black.
- Line 23-24 – Erode and Dilate the mask for its enhancement.
- Line 26 – [MOST IMPORTANT LINE] In the mask where we have white regions (where cloak is there) replace those pixels with the pixels of the background image we recorded above. And in this way, it creates an invisibility effect. Where all the cloak is present it shows background at those places and it looks like you are invisible.
- Line 28 – Simply showing our image.
- Line 30-31 – If someone hits the ESC key, break the code.
- Line 33-34 – Release the webcam and destroy all open windows.
Download the Source Code for Harry’s Invisibility Cloak…
Do let me know if there’s any query regarding Harry’s Invisibility Cloak by contacting me by email or LinkedIn.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…
Read my previous post: HOW TO DETECT SHAPES IN OPENCV
Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net.
2 thoughts on “Harry’s Invisibility Cloak – less than 50 lines of code – with source code – easiest explanation – 2023”
What did the guy in the video use to cover his body?? Can anyone please tell??
It’s an orange color cloth.