-
Notifications
You must be signed in to change notification settings - Fork 4
/
harry potter magic wand.py
67 lines (49 loc) · 1.82 KB
/
harry potter magic wand.py
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
import cv2
import numpy as np
x, y, k = 200, 200, -1
cap = cv2.VideoCapture(0)
def get_wand(event, x1, y1, flag, param):
global x, y, k
if event == cv2.EVENT_LBUTTONDOWN:
x = x1
y = y1
k = 1
cv2.namedWindow("get_wand")
cv2.setMouseCallback("get_wand", get_wand)
while True:
check, frame = cap.read()
frame = cv2.flip(frame, 1)
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("get_wand", frame)
if k == 1 or cv2.waitKey(30) == 27:
cv2.destroyAllWindows()
break
stp = 0
old_pts = np.array([[x, y]], dtype=np.float32).reshape(-1, 1, 2)
mask = np.zeros_like(frame)
while True:
check, new_frame = cap.read()
new_frame = cv2.flip(new_frame, 1)
new_gray = cv2.cvtColor(new_frame, cv2.COLOR_BGR2GRAY)
new_pts, status, err = cv2.calcOpticalFlowPyrLK(gray_frame,
new_gray,
old_pts,
None, maxLevel=1,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
15, 0.08))
for i, j in zip(old_pts, new_pts):
x, y = j.ravel()
a, b = i.ravel()
if stp == 0:
mask = cv2.line(mask, (a, b), (x, y), (0, 0, 255), 6)
cv2.circle(new_frame, (x, y), 6, (0, 255, 0), -1)
new_frame = cv2.addWeighted(mask, 0.3, new_frame, 0.7, 0)
cv2.imshow("OutPut Window", new_frame)
cv2.imshow("Result Window", mask)
gray_frame = new_gray.copy()
old_pts = new_pts.reshape(-1, 1, 2)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()
cap.release()