Mr.India/Harry Potter Invisible Cloak Using Computer Vision in Python
Hello Friends,
Today we are going to do magic not by using magic trick but by using Python.Isn’t it sound intresting?Offcourse yes!!!!!!!!!
So lets start building our own invisible cloak using computer vision in python.
For working on this project we need to know 3 things :
1) Python — 3.x (we used Python 3.9.6 in this project)
2) Numpy — 1.19.2
3) OpenCV — 4.5
We are using VSCode editor for this project.
Steps:
- Create new Project in VSCode .
- Create new file “magic.py”
- Open Terminal & Type:
pip install opencv-python
pip install numpy
4. And now start writing code for project for code go with following flow.
Project Flow
Code Flow
- Importing needed libraries
import numpy as np
import cv2
import time
2. Recording and caching the background for each frame.
cap=cv2.VideoCapture(0)
#for the system to sleep for 3 second before the webcam starts
time.sleep(3)
3. Capturing background & Video.
for i in range(30):
retval,back=cap.read()
back=np.flip(back,axis=1)
cap=cv2.VideoCapture(0)
4. Read every frame & convert BGR->HSV
#Read every Frame from the webcam, until the camera is open
while (cap.isOpened()):
ret,img=cap.read()
if ret:
img=np.flip(img,axis=1)
#convert the color space from BGR to HSV
hsv=cv2.cvtColor(img,cv2.COLOR\_BGR2HSV)
5. Setting values for cloak & mask.
Here I have selected color as Red, you can choose of your own and set lower and upper values accordingly.
#Generate masks to detect red color
lower\_red = np.array(\[0,120,70\])
upper\_red = np.array(\[10,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)
mask1+=mask2
6. Using Morphological Transformation to remove noise from cloth and un-necessary things.
mask = cv2.morphologyEx(mask1, cv2.MORPH\_OPEN, np.ones((5,5),np.uint8))
img\[np.where(mask==255)\]=back\[np.where(mask==255)\]
7. Combining mask and showing in one frame.
cv2.imshow("Invisible Cloak",img)
key = cv2.waitKey(1)
if key==ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Please find Github link below for code:
GitHub - suryawanshiyashaswini/Magic_Project
Hello Friends, Today we are going to do magic not by using magic trick but by using Python.Isn't it sound…github.com
Demo: