Skip to content

Commit

Permalink
Added files and README
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhadityaMukherjee committed Apr 1, 2019
0 parents commit 260383e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# README

This project aims to emulate an invisibilty cloak.
Run the thresholding code once to choose the color you want to.

### python range_det.py -f HSV --webcam

Check the video for an example ;)

Binary file added inv.mp4
Binary file not shown.
43 changes: 43 additions & 0 deletions invisible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import cv2
import time
import numpy as np
import pickle

with open('range.pickle','rb') as f:
t = pickle.load(f)

lower_red = np.array([t[0],t[1],t[2]])
upper_red = np.array([t[3],t[4],t[5]])


cap = cv2.VideoCapture(0)
time.sleep(3)
background = 0



#initial capture

for i in range(50):
ret,background = cap.read()

background = np.flip(background,axis = 1)


while True:
ret,img = cap.read()
img = np.flip(img,axis=1)
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask1 = cv2.inRange(hsv,lower_red,upper_red)

mask1 = cv2.morphologyEx(mask1,cv2.MORPH_OPEN,np.ones((3,3),np.uint8))
mask1 = cv2.morphologyEx(mask1,cv2.MORPH_DILATE,np.ones((3,3),np.uint8))

mask2 = cv2.bitwise_not(mask1)
res1 = cv2.bitwise_and(img,img,mask=mask2)

res2 = cv2.bitwise_and(background,background,mask=mask1)

final = cv2.addWeighted(res1,1,res2,1,0)
cv2.imshow("Evanesco",final)
cv2.waitKey(1)
Binary file added range.pickle
Binary file not shown.
113 changes: 113 additions & 0 deletions range_det.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# USAGE: You need to specify a filter and "only one" image source
#
# (python) range-detector --filter RGB --image /path/to/image.png
# or
# (python) range-detector --filter HSV --webcam

import cv2
import argparse
from operator import xor
import pickle


def callback(value):
pass


def setup_trackbars(range_filter):
cv2.namedWindow("Trackbars", 0)

for i in ["MIN", "MAX"]:
v = 0 if i == "MIN" else 255

for j in range_filter:
cv2.createTrackbar("%s_%s" % (j, i), "Trackbars", v, 255, callback)


def get_arguments():
ap = argparse.ArgumentParser()
ap.add_argument('-f', '--filter', required=True,
help='Range filter. RGB or HSV')
ap.add_argument('-i', '--image', required=False,
help='Path to the image')
ap.add_argument('-w', '--webcam', required=False,
help='Use webcam', action='store_true')
ap.add_argument('-p', '--preview', required=False,
help='Show a preview of the image after applying the mask',
action='store_true')
args = vars(ap.parse_args())

if not xor(bool(args['image']), bool(args['webcam'])):
ap.error("Please specify only one image source")

if not args['filter'].upper() in ['RGB', 'HSV']:
ap.error("Please speciy a correct filter.")

return args


def get_trackbar_values(range_filter):
values = []

for i in ["MIN", "MAX"]:
for j in range_filter:
v = cv2.getTrackbarPos("%s_%s" % (j, i), "Trackbars")
values.append(v)

return values


def main():
args = get_arguments()

range_filter = args['filter'].upper()

if args['image']:
image = cv2.imread(args['image'])

if range_filter == 'RGB':
frame_to_thresh = image.copy()
else:
frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
else:
camera = cv2.VideoCapture(1)
if camera.read()[0] == False:
camera = cv2.VideoCapture(0)

setup_trackbars(range_filter)

while True:
if args['webcam']:
ret, image = camera.read()

if not ret:
break

if range_filter == 'RGB':
frame_to_thresh = image.copy()
else:
frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

v1_min, v2_min, v3_min, v1_max, v2_max, v3_max = get_trackbar_values(range_filter)

thresh = cv2.inRange(frame_to_thresh, (v1_min, v2_min, v3_min), (v1_max, v2_max, v3_max))

if args['preview']:
preview = cv2.bitwise_and(image, image, mask=thresh)
cv2.imshow("Preview", preview)
else:
cv2.imshow("Original", image)
cv2.imshow("Thresh", thresh)

if cv2.waitKey(1) & 0xFF is ord('q'):
t = (v1_min, v2_min, v3_min, v1_max, v2_max, v3_max)
with open("range.pickle", "wb") as f:
pickle.dump(t,f)
break


if __name__ == '__main__':
main()

0 comments on commit 260383e

Please sign in to comment.