-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh3_note_worker.py
158 lines (136 loc) · 4.79 KB
/
gh3_note_worker.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
147
148
149
150
151
152
153
154
155
156
157
158
r"""Example using TF Lite to classify a given single_note using an Edge TPU."""
import argparse
from PIL import Image
import numpy as np
import cv2
import classify
import tflite_runtime.interpreter as tflite
import platform, mss, threading, queue, os, time
import direct_keyboard_inputs as k
# the region of screen with all 5 notes assuming 800x600 screen resolution
roi = {
"top": 480,
"left": 215,
"width": 370,
"height": 80
}
note_width = 74
NOTES = [k.GREEN, k.RED, k.YELLOW, k.BLUE, k.ORANGE]
note_q = queue.Queue()
EDGETPU_SHARED_LIB = {
'Linux': 'libedgetpu.so.1',
'Darwin': 'libedgetpu.1.dylib',
'Windows': 'edgetpu.dll'
}[platform.system()]
def notes_worker():
prev_notes = []
while True:
notes = note_q.get()
for key in prev_notes:
k.ReleaseKey(key)
for key in notes:
k.PressKey(key)
k.PressKey(k.STRUM)
time.sleep(0.017)
k.ReleaseKey(k.STRUM)
prev_notes = notes
def make_interpreter(model_file):
model_file, *device = model_file.split('@')
try:
_interpreter = tflite.Interpreter(
model_path=model_file,
experimental_delegates=[
tflite.load_delegate(EDGETPU_SHARED_LIB,
{'device': device[0]} if device else {})
])
except ValueError:
print("must be linux...")
_interpreter = tflite.Interpreter(
model_path=model_file)
return _interpreter
def release_keys():
k.ReleaseKey(k.STRUM)
k.ReleaseKey(k.STAR)
for n in NOTES:
k.ReleaseKey(n)
time.sleep(0.1)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-m', '--model', required=True, help='File path of .tflite file.')
parser.add_argument(
'-d', '--duration', required=False, type=int, default=15.0, help='How long the agent plays')
parser.add_argument(
'-t', '--threshold', type=float, default=0.0,
help='Classification score threshold')
args = parser.parse_args()
interpreter = make_interpreter(args.model)
interpreter.allocate_tensors()
def eval_folder(dir, class_toggle=True):
assert os.path.exists(dir)
wrong_path = "./wrong"
if not os.path.exists(wrong_path):
os.makedirs(wrong_path)
below_thresh_path = "./below_thresh"
if not os.path.exists(below_thresh_path):
os.makedirs(below_thresh_path)
gfr_files = os.listdir(dir)
gh3_pics = []
# keep only the jpg images
for file in gfr_files:
if file.endswith(".jpg") or file.endswith(".png"):
gh3_pics.append(file)
for path in gh3_pics:
pic_path = os.path.join(dir, path)
pic = cv2.imread(pic_path, cv2.COLOR_BGR2RGB)
pic = np.array(pic, dtype=np.uint8)
classify.set_input(interpreter, pic)
interpreter.invoke()
classes = classify.get_output(interpreter, 1, 0)
if class_toggle:
i = 0 # click
else:
i = 1 # no click
if classes[0][0] == i:
if classes[0][1] < args.threshold:
print("below threshold of " + str(args.threshold) + ": " + pic_path)
filename = os.path.join(below_thresh_path, path)
cv2.imwrite(filename, pic)
else:
print("wrong classification: " + pic_path)
filename = os.path.join(wrong_path, path)
cv2.imwrite(filename, pic)
def live_play():
count = 0
single_note = np.zeros((80, 80, 3), dtype=np.float32)
sct = mss.mss() # init screen grab object
threading.Thread(target=notes_worker, daemon=True).start()
start_time = last_strum = time.time()
print("SCRIPT STARTED")
while time.time() - start_time < args.duration:
all_notes = np.asarray(sct.grab(roi))[:,:,:-1] # RGBA, so omit alpha
# cv2.imwrite("test.jpg", all_notes); break
current_notes = []
start_i = -1 * note_width
stop_i = 0
count += 1
for i in range(5):
start_i += note_width
stop_i += note_width
single_note[0:80, 0:note_width, :] = all_notes[0:80, start_i:stop_i, :]
classify.set_input(interpreter, single_note)
interpreter.invoke()
classes = classify.get_output(interpreter, 1, args.threshold)
# if the highest probable class is "click" and over a threshold confidence:
if len(classes) > 0 and classes[0][0] == 0:
current_notes.append(NOTES[i])
can_strum_again = time.time() - last_strum > 0.1
if len(current_notes) and can_strum_again:
note_q.put(current_notes)
print("SCRIPT END! FPS: " + str(count/args.duration))
release_keys()
#eval_folder("/home/rbain/links/fast_storage/python/gh3/data/click_and_no_click/noclick/", False)
live_play()
if __name__ == '__main__':
main()