├── requirements.txt ├── README.md └── invisibility_cloak.py /requirements.txt: -------------------------------------------------------------------------------- 1 | opencv-python 2 | numpy 3 | pyttsx3 4 | SpeechRecognition 5 | pycaw 6 | screen-brightness-control 7 | tk -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Magical Invisibility Cloak and Beyond 🧙✨** 2 | 3 | Have you ever dreamed of wearing Harry Potter's Invisibility Cloak? Well, now you can experience a slice of that magic! Inspired by the wizarding world, I created this Python application to bring the charm of the Invisibility Cloak to life, along with a few extra spellbound features for all the modern-day wizards and witches! 4 | 5 | ## **Features** 6 | - 🧥 **Invisibility Cloak**: Hide yourself in plain sight, just like in the movies! 7 | - 💡 **Lumos & Nox**: Control screen brightness with voice commands. 8 | - ⚡ **Expelliarmus & Accio**: Toggle the cloak's functionality using magic words. 9 | - 🔥 **Sectumsempra**: Add a dramatic red filter to your screen (a touch of dark magic). 10 | - 🔊 **Amplificus & Minimus**: Adjust system volume with the flick of a wand. 11 | - ⚖️ **Obliviate**: Reset all magic effects to their default state. 12 | - 📜 **Guide**: Display a detailed spell guide on-screen for your reference. 13 | - ☠️ **Avada Kedavra**: Exit the application with one deadly spell. (Use responsibly!) 14 | 15 | ## **Getting Started** 16 | 17 | ### *1. Prerequisites* 18 | - Python 3.6 or higher 19 | - Webcam and microphone 20 | - Supported OS (Windows, Linux, macOS) 21 | 22 | ### *2. Installation* 23 | Clone the Repository 24 | Run the following commands to clone the repository and move into the project directory: 25 | ```bash 26 | git clone https://github.com/Parul-Srivastava/Invisibility-Cloak-and-Beyond.git 27 | cd invisibility-cloak 28 | ``` 29 | 30 | ### *3. Install Dependencies* 31 | Use the following command to install all the required libraries: 32 | ```bash 33 | pip install -r requirements.txt 34 | ``` 35 | 36 | ### *4. Test Your Setup* 37 | Before running the application, make sure your webcam, microphone, and screen brightness control are working as they should. This is your first step to mastering your magic! 38 | 39 | ## **Usage** 40 | 41 | ### *1. Run the Python Script* 42 | After setting up the project, run the script by typing the following command in your terminal: 43 | ```bash 44 | python invisibility_cloak.py 45 | ``` 46 | This will launch the application and open your webcam. 47 | 48 | ### *2. Select the Color of the Cloak* 49 | Once the webcam is open, a window will appear displaying the live feed from your camera. 50 | Click on the color of your cloak in the video feed. 51 | The application will capture the color you select and use it to hide you in the invisibility effect. 52 | 53 | ### *3. Capture the Background for the Invisibility Effect* 54 | To capture the background (so the cloak blends seamlessly with your surroundings), press the 'b' key on your keyboard. 55 | The captured background will be used to replace the cloak area, making you “invisible” in that part of the screen. 56 | 57 | ### *4. Use Voice Commands to Control the Magic* 58 | You can now use voice commands to interact with the application and cast spells. Speak clearly to activate different magical effects. 59 | The recognized commands will control various aspects of the system: 60 | - **Brightness**: Use "Lumos" to brighten the screen and "Nox" to dim it. 61 | - **Cloak Functionality**: Say "Accio" to enable the cloak and "Expelliarmus" to disable it. 62 | - **Volume Control**: Use "Amplificus" to increase volume and "Minimus" to decrease it. 63 | - **Other Spells**: Say "Obliviate" to reset all spells to default, or "Guide" to view the spell guide. 64 | 65 | ### *5. Exit the Application* 66 | To close the application at any time, use the spell "Avada Kedavra" or simply press the ESC key on your keyboard. 67 | 68 | ## **Spell Guide** 69 | 70 | | **Spell** | **Functionality** | 71 | |-----------------------|--------------------------------------------------| 72 | | **Lumos** | Set brightness to maximum. | 73 | | **Nox** | Set brightness to minimum. | 74 | | **Expelliarmus** | Disable the cloak effect. | 75 | | **Accio** | Enable the cloak effect. | 76 | | **Sectumsempra** | Add a red filter to the screen. | 77 | | **Amplificus** | Increase system volume. | 78 | | **Minimus** | Decrease system volume. | 79 | | **Obliviate** | Reset all spells to default. | 80 | | **Guide** | Display the spell guide. | 81 | | **Avada Kedavra** | Exit the application. | 82 | 83 | ## **Demo** 84 | **Watch the magic unfold!** Check out the demonstration video present in the repository to see the project in action. 85 | 86 | ## **Contributing** 87 | Feel free to fork this repository and contribute your own enhancements, bug fixes, or spells. Submit a pull request and help expand the magic! 88 | 89 | ## **Acknowledgements** 90 | - The Harry Potter series for endless inspiration 🪄 91 | - Python and OpenCV for making magic real in the muggle world 92 | - OpenAI for the AI wizardry behind some of the magic in my tools 93 | 94 | **Enjoy your magical journey, and may your spells always be successful!** 🎩✨ 95 | -------------------------------------------------------------------------------- /invisibility_cloak.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import pyttsx3 4 | import speech_recognition as sr 5 | import threading 6 | import os 7 | import platform 8 | from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume 9 | import screen_brightness_control as sbc 10 | import tkinter as tk 11 | from tkinter import messagebox 12 | 13 | # Initialize variables 14 | selected_color = None 15 | lower_bound = None 16 | upper_bound = None 17 | background = None 18 | brightness = 1.0 19 | cloak_enabled = True 20 | reddish_filter = False 21 | guide_displayed = False 22 | volume_level = 1.0 # Current volume level (range 0 to 1) 23 | running = True 24 | 25 | # Text-to-speech engine 26 | engine = pyttsx3.init() 27 | 28 | def select_color(event, x, y, flags, param): 29 | global lower_bound, upper_bound, selected_color, frame 30 | if event == cv2.EVENT_LBUTTONDOWN: 31 | # Get the selected color 32 | selected_color = frame[y, x] 33 | b, g, r = selected_color 34 | # Define bounds for the selected color 35 | lower_bound = np.array([max(0, b - 40), max(0, g - 40), max(0, r - 40)]) 36 | upper_bound = np.array([min(255, b + 40), min(255, g + 40), min(255, r + 40)]) 37 | print(f"Selected Color: {selected_color}") 38 | print(f"Lower Bound: {lower_bound}") 39 | print(f"Upper Bound: {upper_bound}") 40 | 41 | def adjust_brightness(frame, alpha): 42 | """Adjust brightness.""" 43 | return cv2.convertScaleAbs(frame, alpha=alpha, beta=0) 44 | 45 | def reset_spells(): 46 | """Reset all spells to default.""" 47 | global brightness, cloak_enabled, reddish_filter, guide_displayed, volume_level 48 | brightness = 1.0 49 | cloak_enabled = True 50 | reddish_filter = False 51 | guide_displayed = False 52 | volume_level = 1.0 53 | print("Obliviate! All spells reset.") 54 | 55 | def adjust_system_brightness(level): 56 | """Adjust system brightness.""" 57 | if platform.system() == "Windows": 58 | sbc.set_brightness(level * 100) # scale level to 0-100 59 | elif platform.system() == "Linux": 60 | os.system(f"xrandr --output eDP-1 --brightness {level}") 61 | elif platform.system() == "Darwin": 62 | os.system(f"brightness {level}") 63 | 64 | def adjust_volume(level): 65 | """Adjust system volume.""" 66 | try: 67 | devices = AudioUtilities.GetSpeakers() # Get the speakers device 68 | interface = devices.Activate( 69 | IAudioEndpointVolume._iid_, 1, None) # Activate the volume interface 70 | volume = interface.QueryInterface(IAudioEndpointVolume) 71 | volume.SetMasterVolumeLevelScalar(level, None) # Set the volume to the desired level 72 | except Exception as e: 73 | print(f"Error adjusting volume: {e}") 74 | 75 | def listen_for_spell(): 76 | """Listen for a spell and execute its corresponding effect.""" 77 | global running, brightness, cloak_enabled, reddish_filter, guide_displayed, volume_level 78 | recognizer = sr.Recognizer() 79 | with sr.Microphone() as source: 80 | print("Listening for spells... (Say a spell)") 81 | while running: 82 | try: 83 | audio = recognizer.listen(source, timeout=10) # Increased timeout 84 | spell = recognizer.recognize_google(audio).lower() 85 | print(f"You said: {spell}") 86 | # Handle each spell 87 | if spell == "lumos": 88 | brightness = 2.0 89 | adjust_system_brightness(1.0) # Max brightness 90 | print("Lumos! Brightness set to maximum.") 91 | elif spell == "nox" or spell == "knox": 92 | brightness = 0.1 93 | adjust_system_brightness(0.1) # Min brightness 94 | print("Nox! Brightness set to minimum.") 95 | elif spell == "expelliarmus": 96 | cloak_enabled = False 97 | print("Expelliarmus! Cloak disabled.") 98 | elif spell == "accio": 99 | cloak_enabled = True 100 | print("Accio! Cloak enabled.") 101 | elif spell == "sectumsempra": 102 | reddish_filter = True 103 | print("Sectumsempra! Adding red filter.") 104 | elif spell == "obliviate": 105 | reset_spells() 106 | elif (spell == "amplification" or spell == "amplificus"): 107 | # Increase volume by 5 points until maximum 108 | if volume_level < 1.0: 109 | volume_level = min(1.0, volume_level + 0.05) 110 | adjust_volume(volume_level) 111 | print(f"Amplificus! Volume increased to {volume_level * 100}%.") 112 | elif (spell == "minimise" or spell == "minimus"): 113 | # Decrease volume by 5 points until minimum 114 | if volume_level > 0.0: 115 | volume_level = max(0.0, volume_level - 0.05) 116 | adjust_volume(volume_level) 117 | print(f"Minimus! Volume decreased to {volume_level * 100}%.") 118 | elif spell == "guide": 119 | guide_displayed = True 120 | show_guide_popup() # Show pop-up guide 121 | elif spell == "close guide": 122 | guide_displayed = False 123 | close_guide_popup() # Close pop-up guide 124 | elif spell == "avada kedavra" or spell == "avada keda" : 125 | print("Avada Kedavra! Exiting the script.") 126 | running = False 127 | except sr.WaitTimeoutError: 128 | pass # Ignore timeout errors 129 | except sr.UnknownValueError: 130 | print("Could not understand the spell, try again.") 131 | 132 | def show_guide_popup(): 133 | """Show the spell guide in a pop-up window.""" 134 | guide_text = """ 135 | Spell Guide: 136 | - Lumos: Set brightness to maximum 137 | - Nox: Set brightness to minimum 138 | - Expelliarmus: Disable functionality 139 | - Accio: Enable functionality 140 | - Amplificus: Increase system volume 141 | - Minimus: Decrease system volume 142 | - Obliviate: Reset all spells 143 | - Guide: Show spell guide 144 | - Avada Kedavra: Exit the script 145 | """ 146 | 147 | # Create pop-up window 148 | window = tk.Tk() 149 | window.title("Spell Guide") 150 | window.geometry("400x300") 151 | 152 | label = tk.Label(window, text=guide_text, justify="left", padx=10, pady=10) 153 | label.pack(padx=10, pady=10) 154 | 155 | # Add a button to close the pop-up 156 | close_button = tk.Button(window, text="Close", command=window.destroy) 157 | close_button.pack(pady=10) 158 | 159 | # Start the window's main loop 160 | window.mainloop() 161 | 162 | def close_guide_popup(): 163 | """Close the spell guide pop-up window.""" 164 | for widget in tk._default_root.winfo_children(): 165 | widget.destroy() 166 | 167 | # Initialize the webcam 168 | cap = cv2.VideoCapture(0) 169 | 170 | cv2.namedWindow("Select Color") 171 | cv2.setMouseCallback("Select Color", select_color) 172 | 173 | selected_color = None 174 | lower_bound = None 175 | upper_bound = None 176 | 177 | print("Click on the color of the cloak in the video feed to select it.") 178 | 179 | # Capture the static background frame (press 'b' to capture) 180 | background = None 181 | while True: 182 | ret, frame = cap.read() 183 | if not ret: 184 | break 185 | 186 | frame = cv2.flip(frame, 1) # Flip the frame horizontally 187 | cv2.imshow("Select Color", frame) 188 | 189 | key = cv2.waitKey(1) 190 | if key == ord('b'): # Capture background 191 | background = frame 192 | print("Background captured!") 193 | if key == 27: # ESC to exit 194 | break 195 | 196 | cv2.destroyWindow("Select Color") 197 | 198 | # Start the spell listening thread 199 | threading.Thread(target=listen_for_spell, daemon=True).start() 200 | 201 | if background is not None and lower_bound is not None: 202 | print("Starting real-time invisibility cloak...") 203 | while running: 204 | ret, frame = cap.read() 205 | if not ret: 206 | break 207 | 208 | frame = cv2.flip(frame, 1) 209 | 210 | # Adjust brightness 211 | frame = adjust_brightness(frame, brightness) 212 | 213 | # Apply color filtering if cloak is enabled 214 | if cloak_enabled and lower_bound is not None and upper_bound is not None: 215 | mask = cv2.inRange(frame, lower_bound, upper_bound) 216 | # Refine the mask (remove noise) 217 | mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8)) 218 | mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8)) 219 | 220 | # Invert the mask 221 | mask_inv = cv2.bitwise_not(mask) 222 | 223 | # Segment the cloak area 224 | cloak_area = cv2.bitwise_and(background, background, mask=mask) 225 | 226 | # Segment the non-cloak area 227 | non_cloak_area = cv2.bitwise_and(frame, frame, mask=mask_inv) 228 | 229 | # Combine the two areas 230 | final_output = cv2.addWeighted(cloak_area, 1, non_cloak_area, 1, 0) 231 | 232 | # Apply the red filter if Sectumsempra spell is cast 233 | if reddish_filter: 234 | final_output = cv2.addWeighted(final_output, 1, np.zeros_like(final_output), 0, 50) 235 | 236 | cv2.imshow("Invisibility Cloak", final_output) 237 | 238 | key = cv2.waitKey(1) 239 | if key == 27: # ESC to exit 240 | break 241 | 242 | cap.release() 243 | cv2.destroyAllWindows() 244 | --------------------------------------------------------------------------------