forked from KuskiGitHub/piano_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
96 lines (68 loc) · 2.09 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
import os
import numpy
import cv2
import time
import random
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
import threading
import numpy as np
class Note():
def __init__(self) -> None:
self.answer = None
self.image_path = None
self.mat = None
def load_data():
ipatsh = [os.path.join(os.getcwd(), "notes", i) for i in os.listdir(os.path.join(os.getcwd(), "notes"))]
Notes = []
for i in ipatsh:
a = Note()
a.image_path = i
a.mat = cv2.imread(i, cv2.IMREAD_UNCHANGED)
a.answer = i.split("\\")[-1].split("_")[1]
Notes.append(a)
return Notes
global imagelist
class Gui():
def __init__(self) -> None:
self.root = Tk()
self.root.geometry("550x300+300+150")
self.root.attributes("-topmost",True)
self.panel = Label(self.root)
self.panel.pack()
self.update()
self.root.mainloop()
def update(self):
global imagelist
self.image = imagelist[0]
self.image = ImageTk.PhotoImage(Image.fromarray(self.image, mode="L"))
self.panel.configure(image=self.image)
self.panel.update()
self.panel.after(100, self.update)
def create_gui():
aa = Gui()
def game():
Notes = load_data()
global imagelist
imagelist = [np.zeros((250,162),dtype=np.uint8)]
t = threading.Thread(target=create_gui)
t.start()
time.sleep(1)
while True:
random.shuffle(Notes)
start = time.time()
correct_answers = 0
for i in Notes:
imagelist[0] = cv2.cvtColor(i.mat, cv2.COLOR_BGR2GRAY)
answer = input("Answer: ")
if answer == i.answer or answer.lower() == i.answer or answer.capitalize() == i.answer:
print("Correct")
correct_answers += 1
else:
print(f"Incorrect. Answer is {i.answer}")
time.sleep(5)
end = time.time()
print(f"Took {end-start} and got {correct_answers/len(Notes)*100} % correct")
if __name__ == "__main__":
game()