-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.py
108 lines (76 loc) · 2.81 KB
/
collection.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
from PIL import Image
from mss import mss
import keyboard
import numpy as np
import os.path
import time
import pandas as pd
import sys
import keyboard
from ahk import AHK
import os
ah=AHK(executable_path = "C://Program Files//AutoHotkey//UX//AutoHotkeyUX.exe")
n_window= ah.find_window(title='NSUNS4')
global images, events, save ,fnum
N_FRAMES = 3
RES_=(64, 64)
images = np.zeros( (1 , N_FRAMES , RES_[0] , RES_[1]) )
events = np.array(keyboard.KeyboardEvent( "down" , 4)).reshape(1, -1)
if os.path.exists("data"):
if os.path.exists("data/episode_count.txt"):
f=open('data/episode_count.txt','r')
fnum=f.readline()
f.close()
else:
fnum="1"
with open("data/episode_count.txt" ,"w") as f:
f.write("1")
else:
os.makedirs("data")
fnum="1"
with open("data/episode_count.txt" ,"w") as f:
f.write("1")
def capture_screenshot(n_frames =3 , res = (768, 768) ):
images_stack = np.zeros((1 , res[0] , res[1]))
with mss() as sct:
event=keyboard.read_event()
monitor = sct.monitors[1]
for i in range(n_frames):
sct_img = sct.grab(monitor)
images_stack = np.concatenate( [ images_stack , np.array( Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX').resize(res).convert("L") ).reshape(1 , res[0] , res[1]) ] )
if i == 0 :
images_stack = images_stack[1: , :]
images_stack = images_stack / 255
images_stack = images_stack.reshape(1 , n_frames ,res[0] , res[1])
if event.name=='esc':
return None,None
else :
return images_stack , event
if __name__=='__main__':
print("Press ` to start recoarding")
keyboard.wait('`')
while True:
#try:
img_stack , event =capture_screenshot(N_FRAMES , RES_)
if event ==None:
events = events[1: ]
images = images[1: , : , : , :]
np.save("data/"+"episode"+fnum , images)
np.save("data/"+"episode_events"+fnum , events)
fnum = str( int(fnum)+1 )
with open("data/episode_count.txt" ,"w") as f:
f.write(fnum)
del images
del events
images = np.zeros( (1 , N_FRAMES , RES_[0] , RES_[1]) )
events = np.array(keyboard.KeyboardEvent( "down" , 4)).reshape(1, -1)
print('Capturing paused , press (`) to continue ... ')
keyboard.wait('`')
else:
images = np.concatenate( [images , img_stack] , axis=0 )
events = np.append(events , np.array(event).reshape(1, -1) )
if n_window !=None:
n_window.restore()
#except Exception :
#print ('invalid keys entered ... cancelling iteration...')
#continue