AstosM commited on
Commit
ac3dd94
·
verified ·
1 Parent(s): eef4429

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -2,56 +2,56 @@ import cv2
2
  import numpy as np
3
  import gradio as gr
4
 
5
- # ---------------------------
6
- # Invisibility Cloak Function
7
- # ---------------------------
8
  def invisibility(frame):
9
- # Flip horizontally (mirror effect)
 
 
 
 
 
10
  frame = cv2.flip(frame, 1)
 
 
 
 
 
11
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
12
 
13
- # Define red cloak color range
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 for red color
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
  # Noise removal
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 (all except cloak)
29
  inverse_mask = cv2.bitwise_not(cloak_mask)
30
 
31
- # Background (black image for demo)
32
- background = np.zeros_like(frame)
33
-
34
- # Extract cloak and non-cloak regions
35
  cloak_area = cv2.bitwise_and(background, background, mask=cloak_mask)
36
  non_cloak_area = cv2.bitwise_and(frame, frame, mask=inverse_mask)
37
 
38
- # Merge
39
  final_output = cv2.addWeighted(cloak_area, 1, non_cloak_area, 1, 0)
40
 
41
- # Convert BGR → RGB for Gradio
42
  return cv2.cvtColor(final_output, cv2.COLOR_BGR2RGB)
43
 
44
 
45
- # ---------------------------
46
- # Gradio UI
47
- # ---------------------------
48
  demo = gr.Interface(
49
  fn=invisibility,
50
- inputs=gr.Image(source="webcam", streaming=True, label="Webcam"),
51
- outputs=gr.Image(label="Invisibility Cloak Output"),
52
  live=True,
53
  title="🧙 Magic Invisibility Cloak",
54
- description="Wear something red to see the invisibility effect like Harry Potter!"
55
  )
56
 
57
  if __name__ == "__main__":
 
2
  import numpy as np
3
  import gradio as gr
4
 
5
+ background = None
6
+
 
7
  def invisibility(frame):
8
+ global background
9
+
10
+ # Convert input (PIL → numpy BGR)
11
+ frame = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
12
+
13
+ # Flip frame
14
  frame = cv2.flip(frame, 1)
15
+
16
+ # Initialize background
17
+ if background is None:
18
+ background = frame.copy()
19
+
20
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
21
 
22
+ # Red color ranges
23
  lower_red1 = np.array([0, 120, 70])
24
  upper_red1 = np.array([10, 255, 255])
25
  lower_red2 = np.array([170, 120, 70])
26
  upper_red2 = np.array([180, 255, 255])
27
 
 
28
  mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
29
  mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
30
  cloak_mask = mask1 + mask2
31
 
32
  # Noise removal
33
+ cloak_mask = cv2.morphologyEx(cloak_mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=2)
34
+ cloak_mask = cv2.dilate(cloak_mask, np.ones((3, 3), np.uint8), iterations=1)
35
 
36
+ # Invert mask
37
  inverse_mask = cv2.bitwise_not(cloak_mask)
38
 
39
+ # Apply masks
 
 
 
40
  cloak_area = cv2.bitwise_and(background, background, mask=cloak_mask)
41
  non_cloak_area = cv2.bitwise_and(frame, frame, mask=inverse_mask)
42
 
 
43
  final_output = cv2.addWeighted(cloak_area, 1, non_cloak_area, 1, 0)
44
 
 
45
  return cv2.cvtColor(final_output, cv2.COLOR_BGR2RGB)
46
 
47
 
 
 
 
48
  demo = gr.Interface(
49
  fn=invisibility,
50
+ inputs=gr.Image(label="Webcam Frame", type="pil"),
51
+ outputs=gr.Image(label="Cloak Output"),
52
  live=True,
53
  title="🧙 Magic Invisibility Cloak",
54
+ description="Wear a red cloth and vanish like Harry Potter!"
55
  )
56
 
57
  if __name__ == "__main__":