Hey Peeps !!!!
In this new blog, something interesting awaits for you all !!!!!
What’s it all about ????
Let’s write code for becoming a magician using OpenCV !!!!
Pardon for incomplete invisible effect ! Due to bad lightning its like this !! 🙂
First, we need to be clear about the libraries that we are about to use for becoming a magician, we gonna use “OpenCV” library.
We start by importing the necessary packages:
import cv2 import numpy as np import time
“cv2” is the opencv library we are going to use and numpy is another python library used mainly for numerical calculations and time library is used to defining the time.
After importing, it’s now time to read the webcam and store the background as images in range of 60 for making the invisible effect.
#reading the webcam cap = cv2.VideoCapture(0) #allowing the system to sleep for 3 seconds before the webcam fires up ! time.sleep(3) count = 0 background = 0 #Capturing the background in range of 60 for i in range(60): ret,background = cap.read() background = np.flip(background,axis=1)
When the webcam is fired, we read the images from the webcam and convert the RGB images to Hue-Saturation-Value (HSV) .
- Hue – Colours in their purest form.
- Saturation – intensity or chroma component of the colour.
- Value – relative lightness or darkness of a colour.
Then masking range for the colour cloak we are about to use is defined.
while(cap.isOpened()): ret, img = cap.read() if not ret: break count+=1 img = np.flip(img,axis=1) # Converting the color space from BGR to HSV hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Generating mask to detect red color lower_red = np.array([110,50,50]) upper_red = np.array([120,255,255]) mask1 = cv2.inRange(hsv,lower_red,upper_red) lower_red = np.array([120,50,50]) upper_red = np.array([130,255,255]) mask2 = cv2.inRange(hsv,lower_red,upper_red) mask1 = mask1+mask2
After this, we use this mask to detect the colour of the cloak we are using and mask the colour of the cloak by making the background visible, so that the invisible effect is created.
Voilllaaaaaa !!!!! We became a magician !!!
Full code for this project – https://github.com/SaravananJaichandar/Harry-Potter-s-Invisible-cloak-using-Opencv