-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
129 lines (97 loc) · 3.18 KB
/
Game.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# calcOpticalFlowPyrLK 추적 (track_opticalLK.py)
import collections
import cv2
import numpy as np
import time
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
buffer = collections.deque(maxlen=140)
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture('jump.mp4')
def press_space():
keyboard.press(Key.space)
keyboard.release(Key.space)
fps = cap.get(cv2.CAP_PROP_FPS)
delay = int(1000 / fps)
print(fps)
print(delay)
color = np.random.randint(0, 255, (200, 3))
lines = None
prevImg = None
termcriteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)
jumpcnt = 1
cnt = 0
prevPt = None
for i in range (200):
ret, frame = cap.read()
scale_percent = 50 # percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('', gray)
print(i)
key = cv2.waitKey(delay)
cv2.destroyAllWindows()
while True:
ret, frame = cap.read()
scale_percent = 10 # percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
if not ret:
break
img_draw = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if prevImg is None:
prevImg = gray
lines = np.zeros_like(frame)
prevPt = cv2.goodFeaturesToTrack(prevImg, 200, 0.01, 10)
else:
nextImg = gray
prevPt = cv2.goodFeaturesToTrack(prevImg, 200, 0.01, 10)
nextPt, status, err = cv2.calcOpticalFlowPyrLK(prevImg, nextImg, \
prevPt, None, criteria=termcriteria)
try:
prevMv = prevPt[status == 1]
nextMv = nextPt[status == 1]
vec = prevMv - nextMv
buffer.append(vec[1])
Threshold = 0
if len(buffer) > 40:
Threshold = np.mean(np.array(buffer))
vec = np.mean(vec, axis=0)
if vec[1] > (Threshold+0.3 ) * (1/jumpcnt):
#print(vec[1])
jumpcnt += 1
else:
jumpcnt = np.clip(jumpcnt - 1, 1, 5)
if jumpcnt > 3:
print('jump')
jumpcnt = 1
press_space()
'''
for i, (p, n) in enumerate(zip(prevMv, nextMv)):
px, py = p.ravel()
nx, ny = n.ravel()
cv2.line(lines, (px, py), (nx, ny), color[i].tolist(), 2)
cv2.circle(img_draw, (nx, ny), 2, color[i].tolist(), -1)
img_draw = cv2.add(img_draw, lines)
'''
prevImg = nextImg
prevPt = nextMv.reshape(-1, 1, 2)
except:
pass
key = cv2.waitKey(delay)
if key == 27: # Esc:
break
elif key == 8: # Backspace:
prevImg = None
cnt += 1
cv2.destroyAllWindows()
cap.release()