|
| 1 | +import numpy as np |
| 2 | +import cv2 |
| 3 | +import time |
| 4 | + |
| 5 | +print(""" |
| 6 | +BE PREPARE YOU WILL BE INVISIBLE SOON............ |
| 7 | +""") |
| 8 | + |
| 9 | +if __name__ == '__main__': |
| 10 | + cap = cv2.VideoCapture(0) |
| 11 | + |
| 12 | +#For capturing output video |
| 13 | + fourcc = cv2.VideoWriter_fourcc(*'XVID') |
| 14 | + out = cv2.VideoWriter('harry.avi' , fourcc, 20.0, (640,480)) |
| 15 | + time.sleep(2) |
| 16 | + background = 0 |
| 17 | + |
| 18 | +#capturing background |
| 19 | +for i in range(30): |
| 20 | + ret, background = cap.read() |
| 21 | + |
| 22 | + #capturing image |
| 23 | +while(cap.isOpened()): |
| 24 | + ret, img = cap.read() |
| 25 | + |
| 26 | + if not ret: |
| 27 | + break |
| 28 | +#HSV stands for Hue Satrurated Value |
| 29 | + hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
| 30 | + |
| 31 | +#YOU CAN CHANGE THE COLOR VALUE BELOW ACCORDING TO YOUR CLOTH COLOR |
| 32 | + lower_red = np.array([0,120,70]) |
| 33 | + upper_red = np.array([10,255,255]) |
| 34 | + mask1 = cv2.inRange(hsv , lower_red , upper_red) |
| 35 | + |
| 36 | + lower_red = np.array([170,120,70]) |
| 37 | + upper_red = np.array([180,255,255]) |
| 38 | + mask2 = cv2.inRange(hsv , lower_red , upper_red) |
| 39 | + |
| 40 | + mask1 = mask1 + mask2 |
| 41 | + |
| 42 | +#Open and clean the mask image |
| 43 | + mask1=cv2.morphologyEx(mask1, cv2.MORPH_OPEN ,np.ones((3,3) , np.uint8) , iterations=2) |
| 44 | + |
| 45 | + mask2=cv2.morphologyEx(mask1, cv2.MORPH_DILATE ,np.ones((3,3) , np.uint8) , iterations=1) |
| 46 | + |
| 47 | + mask2 = cv2.bitwise_not(mask1) |
| 48 | + |
| 49 | +#Generating the final output |
| 50 | + res1 = cv2.bitwise_and(background, background, mask=mask1) |
| 51 | + res2 = cv2.bitwise_and(img, img, mask=mask2) |
| 52 | + |
| 53 | + final_output = cv2.addWeighted(res1 , 1, res2 , 1, 0) |
| 54 | + |
| 55 | + cv2.imshow('Invisibility Game' , final_output) |
| 56 | + k=cv2.waitKey(10) |
| 57 | + if k==27: |
| 58 | + print("Escape hit, closing...") |
| 59 | + break |
| 60 | + |
| 61 | +cap.release() |
| 62 | +out.release() |
| 63 | +cv2.destroyAllWindows() |
0 commit comments