From f5ea56dd2957dde48206ff55f445934b021d68fe Mon Sep 17 00:00:00 2001 From: Dhruv Bansal Date: Wed, 6 Oct 2021 11:29:56 +0530 Subject: [PATCH] add Harry Potter Cloak script using Open-CV (#228) * add Harry Potter Cloak script using Open-CV * add Indian Flag using Turtle * Delete README.md * Delete main.py * Update README.md --- Harry-Potter-Cloak/HarryPotterCloak.py | 73 ++++++++++++++++++++++++++ Harry-Potter-Cloak/README.md | 6 +++ Harry-Potter-Cloak/requirements.txt | 2 + README.md | 1 + 4 files changed, 82 insertions(+) create mode 100644 Harry-Potter-Cloak/HarryPotterCloak.py create mode 100644 Harry-Potter-Cloak/README.md create mode 100644 Harry-Potter-Cloak/requirements.txt diff --git a/Harry-Potter-Cloak/HarryPotterCloak.py b/Harry-Potter-Cloak/HarryPotterCloak.py new file mode 100644 index 0000000..a8437dd --- /dev/null +++ b/Harry-Potter-Cloak/HarryPotterCloak.py @@ -0,0 +1,73 @@ +import cv2 +import numpy + +def hello(x): + print("") + +if __name__=='__main__': + cap = cv2.VideoCapture(0) + bars = cv2.namedWindow("bars") + + cv2.createTrackbar("upper_hue","bars",110,180,hello) + cv2.createTrackbar("upper_saturation","bars",255, 255, hello) + cv2.createTrackbar("upper_value","bars",255, 255, hello) + cv2.createTrackbar("lower_hue","bars",68,180, hello) + cv2.createTrackbar("lower_saturation","bars",55, 255, hello) + cv2.createTrackbar("lower_value","bars",54, 255, hello) + + #Capturing the initial frame for creation of background + while(True): + cv2.waitKey(1000) + ret,init_frame = cap.read() + if(ret): + break + + while(True): + ret,frame = cap.read() + inspect = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + + #getting the HSV values for masking the cloak + upper_hue = cv2.getTrackbarPos("upper_hue", "bars") + upper_saturation = cv2.getTrackbarPos("upper_saturation", "bars") + upper_value = cv2.getTrackbarPos("upper_value", "bars") + lower_value = cv2.getTrackbarPos("lower_value","bars") + lower_hue = cv2.getTrackbarPos("lower_hue","bars") + lower_saturation = cv2.getTrackbarPos("lower_saturation","bars") + + #Kernel to be used for dilation + kernel = numpy.ones((3,3),numpy.uint8) + + upper_hsv = numpy.array([upper_hue,upper_saturation,upper_value]) + lower_hsv = numpy.array([lower_hue,lower_saturation,lower_value]) + + mask = cv2.inRange(inspect, lower_hsv, upper_hsv) + mask = cv2.medianBlur(mask,3) + mask_inv = 255-mask + mask = cv2.dilate(mask,kernel,5) + + #The mixing of frames in a combination to achieve the required frame + b = frame[:,:,0] + g = frame[:,:,1] + r = frame[:,:,2] + b = cv2.bitwise_and(mask_inv, b) + g = cv2.bitwise_and(mask_inv, g) + r = cv2.bitwise_and(mask_inv, r) + frame_inv = cv2.merge((b,g,r)) + + b = init_frame[:,:,0] + g = init_frame[:,:,1] + r = init_frame[:,:,2] + b = cv2.bitwise_and(b,mask) + g = cv2.bitwise_and(g,mask) + r = cv2.bitwise_and(r,mask) + blanket_area = cv2.merge((b,g,r)) + + final = cv2.bitwise_or(frame_inv, blanket_area) + + cv2.imshow("Harry's Cloak",final) + + if(cv2.waitKey(3) == ord('q')): + break; + + cv2.destroyAllWindows() + cap.release() \ No newline at end of file diff --git a/Harry-Potter-Cloak/README.md b/Harry-Potter-Cloak/README.md new file mode 100644 index 0000000..d7ce33e --- /dev/null +++ b/Harry-Potter-Cloak/README.md @@ -0,0 +1,6 @@ +## Harry Potter Cloak using OpenCV + +This is a fun python script to simulate a harry potter cloak using image masking with the help of opencv library. +It also uses the numpy library for some mathematical calculations. + +Requirements: You will need Python installed on your system along with numpy and opencv library for this script to work properly. \ No newline at end of file diff --git a/Harry-Potter-Cloak/requirements.txt b/Harry-Potter-Cloak/requirements.txt new file mode 100644 index 0000000..508cdea --- /dev/null +++ b/Harry-Potter-Cloak/requirements.txt @@ -0,0 +1,2 @@ +numpy +opencv \ No newline at end of file diff --git a/README.md b/README.md index b407a5b..88bbb99 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ So far, the following projects have been integrated to this repo: |[Minecraft Server in background](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Minecraft_server_in_background)|[Max von Forell](https://github.com/mvforell)| |[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()| |[Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner)|[Plutoberth](https://github.com/Plutoberth)| +|[Harry Potter Cloak](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Harry-Potter-Cloak) | [thesmartdeveloperr](https://github.com/thesmartdeveloperr)| |[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)| |[Random name generator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Random_Names_Generator)| [Ayush Bhardwaj](https://github.com/hastagAB)| |[Random Password Generators](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Random_Password_Generator)| [Hafpaf](https://github.com/hafpaf) and [Renderer-RCT2](https://github.com/Renderer-RCT2)|