AstosM commited on
Commit
b90dc03
·
verified ·
1 Parent(s): c66dedd

Create invisible_cloak.py

Browse files
Files changed (1) hide show
  1. invisible_cloak.py +65 -0
invisible_cloak.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import time
4
+ import gradio as gr
5
+
6
+ # -----------------------------
7
+ # Function for Invisibility Cloak
8
+ # -----------------------------
9
+ def invisibility(frame, mode="i", custom_img_path=""):
10
+ frame = cv2.flip(frame, 1) # Flip horizontally
11
+ hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
12
+
13
+ # Define red cloak range in HSV
14
+ lower_red1 = np.array([0, 120, 70])
15
+ upper_red1 = np.array([10, 255, 255])
16
+ lower_red2 = np.array([170, 120, 70])
17
+ upper_red2 = np.array([180, 255, 255])
18
+
19
+ # Create mask
20
+ mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
21
+ mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
22
+ cloak_mask = mask1 + mask2
23
+
24
+ # Remove noise
25
+ cloak_mask = cv2.morphologyEx(cloak_mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)
26
+ cloak_mask = cv2.dilate(cloak_mask, np.ones((3, 3), np.uint8), iterations=1)
27
+
28
+ # Invert mask
29
+ inverse_mask = cv2.bitwise_not(cloak_mask)
30
+
31
+ # Background selection
32
+ if mode == "i": # Invisible mode (use black background)
33
+ background = np.zeros_like(frame)
34
+ elif mode == "c" and custom_img_path.strip() != "":
35
+ background = cv2.imread(custom_img_path)
36
+ background = cv2.resize(background, (frame.shape[1], frame.shape[0]))
37
+ else:
38
+ background = np.zeros_like(frame)
39
+
40
+ # Extract cloak area from background & non-cloak area from frame
41
+ cloak_area = cv2.bitwise_and(background, background, mask=cloak_mask)
42
+ non_cloak_area = cv2.bitwise_and(frame, frame, mask=inverse_mask)
43
+
44
+ # Combine both
45
+ final_output = cv2.addWeighted(cloak_area, 1, non_cloak_area, 1, 0)
46
+ return final_output
47
+
48
+ # -----------------------------
49
+ # Gradio Interface
50
+ # -----------------------------
51
+ demo = gr.Interface(
52
+ fn=invisibility,
53
+ inputs=[
54
+ gr.Image(source="webcam", streaming=True, label="Webcam"), # Webcam input
55
+ gr.Radio(["i", "c"], label="Mode (i = invisible, c = custom image)", value="i"),
56
+ gr.Textbox(label="Path to custom image (optional)")
57
+ ],
58
+ outputs=gr.Image(),
59
+ live=True,
60
+ title="🧙‍♂ Magic Invisibility Cloak",
61
+ description="Harry Potter–style invisibility cloak using Python + OpenCV. Wear a red cloth to vanish!"
62
+ )
63
+
64
+ if __name__ == "__main__":
65
+ demo.launch()