|
| 1 | +#import libraries |
| 2 | +import numpy as np |
| 3 | +import cv2 |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +cap = cv2.VideoCapture(0) |
| 8 | + |
| 9 | +#time to adjust camera |
| 10 | +time.sleep(5) |
| 11 | + |
| 12 | +#background image display when cloak on myself |
| 13 | +background = 0 |
| 14 | + |
| 15 | +#capturing the background |
| 16 | +for i in range(50): |
| 17 | + |
| 18 | + ret, background = cap.read() |
| 19 | + |
| 20 | +#code is running until webcam is not off |
| 21 | +while(cap.isOpened()): |
| 22 | + |
| 23 | + ret, img = cap.read() |
| 24 | + |
| 25 | + if not ret: |
| 26 | + break |
| 27 | + |
| 28 | + hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
| 29 | + |
| 30 | + #HSV values |
| 31 | + lower_red = np.array([0,50,50]) |
| 32 | + upper_red = np.array([10,255,255]) |
| 33 | + |
| 34 | + #seprating the cloak part |
| 35 | + mask1 = cv2.inRange(hsv, lower_red, upper_red) |
| 36 | + |
| 37 | + lower_red = np.array([170, 70, 50]) |
| 38 | + upper_red = np.array([180, 255, 255]) |
| 39 | + mask2 = cv2.inRange(hsv, lower_red, upper_red) |
| 40 | + |
| 41 | + #OR |
| 42 | + mask1 = mask1 + mask2 |
| 43 | + |
| 44 | + mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, |
| 45 | + np.ones((3,3), np.uint8), iterations=10) #noise removal |
| 46 | + mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, |
| 47 | + np.ones((3,3), np.uint8), iterations=10) |
| 48 | + |
| 49 | + mask2 = cv2.bitwise_not(mask1)#except the cloak |
| 50 | + |
| 51 | + res1 = cv2.bitwise_and(background, background, mask=mask1)#used for segmentation of color |
| 52 | + res2 = cv2.bitwise_and(img, img, mask=mask2)#used to substitute the cloak part |
| 53 | + final_output = cv2.addWeighted(res1, 1, res2, 1, 0) |
| 54 | + |
| 55 | + cv2.imshow('Welcome To Hogwarts!!',final_output) |
| 56 | + k= cv2.waitKey(10) |
| 57 | + if k == 27: |
| 58 | + break |
| 59 | + |
| 60 | +cap.release() |
| 61 | +cv2.destroyAllWindows() |
| 62 | + |
| 63 | + |
0 commit comments