Skip to content

Commit c92d87a

Browse files
committed
get data bugs fixed
1 parent 6e3d588 commit c92d87a

File tree

5 files changed

+82
-40
lines changed

5 files changed

+82
-40
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Training deep learning model to play video games, self driving, etc, inspired by
44
Steps to run:
55
1) python3 getdata.py #click on 'r' button and mouse left click top-left and bottom-right
66
corners of window to be saved. Use keyboard and play the game.
7-
7+
2)

getdata.py

+43-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
from getscreen import GetScreen
88
import time
99
import os
10+
import sys
11+
12+
def compare_imgs(im1,im2):
13+
if (im1 is None) or (im2 is None) :
14+
return False
15+
else:
16+
difference = cv2.subtract(im1, im2)
17+
result = not np.any(difference)
18+
return result
1019

1120

1221
if __name__ == '__main__':
@@ -17,29 +26,46 @@
1726

1827
scobj=GetScreen()
1928
kobj = GetKey()
20-
21-
29+
cv2.namedWindow('crop', cv2.WINDOW_NORMAL)
30+
im1=None
2231
while True:
2332
im,t2 = scobj.getimg()
24-
cv2.imshow('screen', im)
25-
2633
keys = kobj.get_pressed_keys()
27-
str1 = ""
28-
29-
# using join function join the list s by
30-
# separating words by str1
31-
str1 = str1.join(keys)
32-
print('Keys pressed: ',str1)
33-
34-
if cv2.waitKey(1) == 27:
35-
cv2.destroyAllWindows()
36-
break
37-
nam = os.path.join(dst, str(int(t2* 10**7)) +'_'+ str1+'.jpg')
38-
#import pdb;pdb.set_trace()
39-
cv2.imwrite(nam,im)
4034

4135

4236

37+
#import pdb;pdb.set_trace()
38+
if (not compare_imgs(im1,im)) or keys :
39+
40+
im1=im
41+
cv2.imshow('crop', im)
42+
43+
str1 = ''
44+
keys2=[]
45+
46+
if keys:
47+
keys2.append('_')
48+
for key in keys:
49+
keys2.append(key)
50+
keys2.append('_')
51+
str1 = str1.join(keys2)
52+
print('Keys pressed: ',str1)
53+
#import pdb;pdb.set_trace()
54+
55+
56+
if cv2.waitKey(50) == 27:
57+
cv2.destroyAllWindows()
58+
break
59+
nam = os.path.join(dst, str(int(t2* 10**7)) +str1+'.jpg')
60+
cv2.imwrite(nam,im)
61+
if keys:
62+
if keys[-1]=='esc':
63+
break
64+
65+
66+
sys.stdout.flush()
67+
print('getdata end')
68+
4369

4470

4571

getkey.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class to manage keypress, returns keys that wr pressed on being continued to be
66
'''
77
class GetKey():
88
def __init__(self):
9+
print('GetKey constructor')
910
self.keyp=[]#
1011
self.keyr=[]
1112

@@ -14,29 +15,38 @@ def __init__(self):
1415
on_press=self.on_press,
1516
on_release=self.on_release)
1617
self.listener.start()
18+
def __del__(self):
19+
self.listener.stop()
20+
print('GetKey destroctor')
21+
1722
def stop(self):
1823
print('stopping listener')
1924
self.listener.stop()
2025
def on_press(self,key):
26+
#import pdb;pdb.set_trace()
2127
try:
2228
if key.char not in self.keyp:
2329
self.keyp.append(key.char)
2430
#print(' {0} pressed'.format(self.keyp))
2531

2632
except AttributeError:
2733
#self.keyp.append(key.char)
28-
print('special key {0} pressed'.format(key))
34+
#print('special key {0} pressed'.format(key.name))
35+
self.keyp.append(key.name)
36+
'''
2937
if key == keyboard.Key.esc:
3038
# Stop listener
3139
print('esc pressed')
40+
'''
3241

3342
def on_release(self,key):
3443
try:
3544
if key.char not in self.keyr:
3645
self.keyr.append(key.char)
3746
#print('{0} released'.format(self.keyr))
3847
except AttributeError:
39-
print('special key {0} released'.format(key))
48+
self.keyr.append(key.name)
49+
#print('special key {0} released'.format(key))
4050

4151
def get_pressed_keys(self):
4252
#returns keys pressed
@@ -45,6 +55,7 @@ def get_pressed_keys(self):
4555
#print('{0} released2'.format(self.keyr))
4656
self.keyp=list(set(self.keyp)-set(self.keyr))
4757
self.keyr=[]
58+
sys.stdout.flush()
4859
return k
4960

5061
def checkforkey(self,key):
@@ -59,16 +70,22 @@ def checkforkey(self,key):
5970

6071
if __name__ == '__main__':
6172
kobj = GetKey()
73+
keys=[]
6274
while 1:
6375
time.sleep(.5)
6476

6577
keys = kobj.get_pressed_keys()
66-
print('Keys pressed: ',keys)
78+
if keys:
79+
print('Keys pressed: ',keys)
6780

6881
'''
6982
kflag = kobj.checkforkey('r')
7083
print('kflag: ',kflag)
7184
'''
85+
86+
if keys:
87+
if keys[-1]=='esc':
88+
break
7289

7390

7491

getmouse.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def stop(self):
1818
self.listener.stop()
1919

2020
def on_move(self,x, y):
21-
print('Pointer moved to {0}'.format(
22-
(x, y)))
21+
#print('Pointer moved to {0}'.format((x, y)))
2322
self.pos=[x,y]
2423

2524
def on_click(self,x, y, button, pressed):
@@ -33,9 +32,8 @@ def on_click(self,x, y, button, pressed):
3332
#return False
3433

3534
def on_scroll(self,x, y, dx, dy):
36-
print('Scrolled {0} at {1}'.format(
37-
'down' if dy < 0 else 'up',
38-
(x, y)))
35+
pass
36+
#print('Scrolled {0} at {1}'.format('down' if dy < 0 else 'up',(x, y)))
3937
def getpts(self):
4038
#get clicked points
4139
return self.pts

getscreen.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from getmouse import GetMouse
77
import time
88
import os
9+
import sys
910

1011
class GetScreen():
1112
def __init__(self):
1213
self.sct = mss()
14+
self.im1=None
1315

14-
15-
1616
kobj = GetKey()
1717
print('Press r and select roi using mouse leftbutton diagonal clicks ')
1818
while True:
@@ -22,6 +22,7 @@ def __init__(self):
2222
if kflag:
2323
print('\rkey pressed: ',kflag)
2424
kobj.stop()
25+
sys.stdout.flush()
2526
break
2627

2728
mobj = GetMouse()
@@ -30,15 +31,14 @@ def __init__(self):
3031
while True:
3132
time.sleep(.5)
3233
pts = mobj.getpts()
33-
pos = mobj.pos
34+
3435
if len(pts)==1:
3536
if not pt1:
3637
pt1 = pts[0]
37-
print('pt1: ',pt1)
38+
pt2 = mobj.pos
3839

39-
elif not pt2:
40-
pt2 = pos
41-
elif len(pts)==2:
40+
elif len(pts)>=2:
41+
mobj.stop()
4242
break
4343

4444
print('box pts: ',pt1,pt2)
@@ -47,12 +47,13 @@ def __init__(self):
4747

4848
def getimg(self):
4949
t2 = time.time()
50-
sct_img = np.array(self.sct.grab(self.bounding_box))
51-
im = cv2.cvtColor(sct_img, cv2.COLOR_BGR2RGB)
5250

53-
print('FPS: {} ',format(1/(t2-self.t1)))
51+
im = np.array(self.sct.grab(self.bounding_box))
52+
53+
54+
#print('FPS: {} ',format(1/(t2-self.t1)))
5455
self.t1=t2
55-
return im,t2
56+
return im,self.t1
5657

5758
if __name__ == '__main__':
5859

@@ -61,18 +62,18 @@ def getimg(self):
6162
os.makedirs(dst)
6263

6364
scobj=GetScreen()
64-
65+
cv2.namedWindow('crop', cv2.WINDOW_NORMAL)
6566

6667
while True:
6768
im,t2 = scobj.getimg()
68-
cv2.imshow('screen', im)
69+
cv2.imshow('crop', im)
6970

7071
if cv2.waitKey(1) == 27:
7172
cv2.destroyAllWindows()
7273
break
7374
nam = os.path.join(dst, str(int(t2* 10**7))+'.jpg')
7475
#import pdb;pdb.set_trace()
75-
cv2.imwrite(nam,im)
76+
#cv2.imwrite(nam,im)
7677

7778

7879

0 commit comments

Comments
 (0)