-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloak.py
More file actions
77 lines (58 loc) · 2.15 KB
/
cloak.py
File metadata and controls
77 lines (58 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Invisibility Cloak using Python + OpenCV
Author: Munazza
Description:
This project creates the Harry Potter invisibility cloak effect.
It detects a blue cloak and replaces that area with the background.
"""
import cv2
import numpy as np
import time
def capture_background(cap, frames=30):
"""Capture background image without the subject."""
background = 0
for i in range(frames):
ret, background = cap.read()
if not ret:
continue
return np.flip(background, axis=1)
def invisibility_cloak():
# Start webcam
cap = cv2.VideoCapture(0)
time.sleep(2) # Allow camera to warm up
# Capture background
print("[INFO] Capturing background... Please step out of frame.")
background = capture_background(cap)
print("[INFO] Background captured successfully!")
print("[INFO] Starting invisibility effect. Press 'q' to quit.")
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Flip frame horizontally (mirror effect)
frame = np.flip(frame, axis=1)
# Convert frame to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define HSV range for BLUE cloak
lower_blue = np.array([94, 80, 2])
upper_blue = np.array([126, 255, 255])
# Create mask for blue
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Refine mask
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8))
# Inverse mask for non-cloak area
mask_inv = cv2.bitwise_not(mask)
# Apply masks
cloak_area = cv2.bitwise_and(background, background, mask=mask)
non_cloak_area = cv2.bitwise_and(frame, frame, mask=mask_inv)
final_output = cv2.addWeighted(cloak_area, 1, non_cloak_area, 1, 0)
# Show result
cv2.imshow("Invisibility Cloak (Blue)", final_output)
# Exit on 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
invisibility_cloak()