-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
146 lines (102 loc) · 3.57 KB
/
extract.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import cv2
import time
import win32gui
import win32ui
import win32con
import numpy as np
import math
import pandas as pd
import keyboard
window_title = "Trackmania"
fps = 30
class WindowCapture:
w = 640
h = 480
hwnd = None
crop_x = 0
crop_y = 0
def __init__(self, window_title):
self.hwnd = win32gui.FindWindow(None, window_title)
# window_rect = win32gui.GetWindowRect(self.hwnd)
border = 8
titlebar = 30
self.w = self.w - border*2
self.h = self.h - (titlebar + border)
self.crop_x = border
self.crop_y = titlebar
def capture(self):
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(self.w, self.h) , dcObj, (self.crop_x, self.crop_y), win32con.SRCCOPY)
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.frombuffer(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# Free Resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
return img
recorder = WindowCapture(window_title)
curr_time = time.time()
i = 0
j = 0
prev_input = ''
angles = [-30, -40, -55, -65, -75, -90, -105, -115, -125, -140, -150]
export = pd.DataFrame({k:[] for k in [f'd{i}' for i in range(len(angles))] + ['input', 'prev_input']})
while True:
start_time = time.time()
frame = recorder.capture()[150:, ]
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2HLS)
cv2.imshow('hls', frame)
lower = np.uint8([100, 9, 20])
upper = np.uint8([200, 100, 100])
bw = cv2.inRange(frame, lower, upper)
cv2.imshow('selcted', bw)
bw = cv2.GaussianBlur(bw, (3,3), cv2.BORDER_DEFAULT)
bw = cv2.resize(bw, (int(recorder.w/4), int(recorder.h/4)))
bw = cv2.Canny(bw, 130, 140)
bw[int(recorder.h/8) - 15: int(recorder.h/4), int(recorder.w/8) - 15: int(recorder.w/8) + 15] = 0
bw[int(recorder.h/8) + 10: int(recorder.h/4), int(recorder.w/8) - 25: int(recorder.w/8) + 25] = 0
h, w = bw.shape
center_h = int(recorder.h/4 - 20)
center_w = int(recorder.w/8)
distances = []
for angle in angles:
y = center_h
x = center_w
while (x >= 0 and x < w) and (y >= 0 and y < h):
image_x = int(x)
image_y = int(y)
if bw[image_y, image_x] == 255:
bw[image_y - 2: image_y + 2, image_x - 2: image_x + 2] = 255
break
x += np.cos(math.radians(angle))
y += np.sin(math.radians(angle))
cv2.line(bw, (center_w, center_h), (image_x, image_y), 50)
distances.append(math.dist([center_h, center_w], [image_y, image_x]))
cv2.imshow('frame', bw)
pressed = ''
try:
if keyboard.is_pressed('w'):
pressed ='w'
if keyboard.is_pressed('a'):
pressed ='a'
if keyboard.is_pressed('d'):
pressed ='d'
# if keyboard.is_pressed('s'):
# pressed ='s'
except:
pass
if cv2.waitKey(1) & 0xFF == ord('q'):
break
export.loc[len(export)] = distances + [pressed, prev_input]
prev_input = pressed
if((time.time() - start_time) < 1/fps):
time.sleep(1/fps - (time.time() - start_time))
export.to_csv('data.csv', index=False)
cv2.destroyAllWindows()