Skip to content

Instantly share code, notes, and snippets.

@Rousan99
Created July 24, 2020 14:15
Show Gist options
  • Save Rousan99/2282cb28ff43fdc2fb7678a7712d1371 to your computer and use it in GitHub Desktop.
Save Rousan99/2282cb28ff43fdc2fb7678a7712d1371 to your computer and use it in GitHub Desktop.
import cv2
import time
import numpy as np
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output = cv2.VideoWriter('Invisible_man.avi', fourcc, 30, (640, 480))
time.sleep(3);still_frame = 0
for i in range(60):
ret, still_frame = cap.read()
still_frame = np.flip(still_frame , axis=1)
while True:
ret, img = cap.read()
if ret == True:
img = np.flip(img, axis=1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([90, 50, 50])
upper_blue = np.array([130, 255,255])
mask1 = cv2.inRange(hsv, lower_blue, upper_blue)
# lower_red = np.array([0, 120, 70])#If you are using red just use these lines see i have used
# upper_red = np.array([10, 255,255])#2 lower and upper limit as red is on both sides of zero
# 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 = mask1 + mask2
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8),iterations=2)
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8),iterations=2)
mask2 = cv2.bitwise_not(mask1)
result1 = cv2.bitwise_and(img, img, mask=mask2)
result2 = cv2.bitwise_and(still_frame , still_frame , mask=mask1)
finalOutput = cv2.addWeighted(result1, 1, result2, 1, 0)
output.write(finalOutput)
cv2.imshow("Invisible Man", finalOutput)
if cv2.waitKey(1) & 0xFF == ord('a'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
print("The video is saved")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment