-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
198 lines (157 loc) · 5.84 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import tkinter as tk
from PIL import Image
from PIL import ImageTk
import threading
import cv2
from cv2 import aruco
from source.utils.frames import get_frames
from tkinter import scrolledtext
from source.markers import *
import keyboard
cap = cv2.VideoCapture(0)
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
parameters = aruco.DetectorParameters_create()
# store a dictionary of detected markers outside the main loop
# so we can store their values
detected = {}
# initialize the dictionary
for i in range(50):
detected[i] = False
# keep lists of different markers
variables = {}
operators = {}
loops = {}
# keep track of whether or not an operation has been performed
# and set a timer between them
timeout = 0
updated = False
class Console():
def __init__(self, x, y, window):
self.x = x
self.y = y
self.cursor = '> '
self.window = window
self.textfield = scrolledtext.ScrolledText(self.window,
wrap = tk.WORD,
width = 30,
height = 27,
font = ("Times New Roman",
15))
def show(self):
"""Displays console"""
self.textfield.place(relx=self.x, rely=self.y)
def update(self, text):
"""Updates console text"""
self.textfield.insert(tk.INSERT, "\n" + text)
def get_text(self):
"""Retrieves console text"""
a = self.textfield.get('1.0', 'end-1c')
return a
def get_input(self, text):
"""Retrieves user input"""
self.textfield.delete('1.0', 'end')
self.update(text)
# must then wait for enter key to be pressed
while True:
if keyboard.is_pressed('enter'):
break
val = self.get_text()
ind = len(text) + 1
self.textfield.delete('1.0', 'end')
return val[ind:]
class Menu(tk.Frame):
def __init__(self, window):
self.window = window
def show(self):
"""Displays menu"""
title = tk.Label(self.window, text="ARCVision",
anchor="center",
font=("Times New Roman", 20))
title.place(relx=0.35, rely=0.3)
checked = tk.IntVar()
box = tk.Checkbutton(self.window, text="Enable descriptors", variable=checked)
box.place(relx=0.35, rely=0.4)
start = tk.Button(self.window,
text="Start Application",
command=self.destroy,
bg="green")
start.place(relx=0.35, rely=0.5)
def destroy(self):
"""Destorys introductory menu"""
self.window.destroy()
class App():
def __init__(self, window, vs):
self.window = window
self.vs = vs
self.frames = []
self.thread = None
self.stopEvent = None
self.panel1 = None
self.panel2 = None
self.console = Console(0.6, 0.15, self.window)
self.stopEvent = threading.Event()
self.thread = threading.Thread(target=self.videoLoop, args=())
self.thread.start()
def show(self):
"""Dislpays app"""
title = tk.Label(self.window, text="ARCVision",
anchor="center",
font=("Times New Roman", 20))
title.place(relx=0.4, rely=0.05)
subtitle = tk.Label(self.window, text="Commands",
font=("Time New Roman", 15))
subtitle.place(relx=0.65, rely=0.1)
self.console.show()
def videoLoop(self):
"""
Main video loop that generates and augments
the two threads that will be displayed in the
panels
"""
try:
while not self.stopEvent.is_set():
self.frames = get_frames(self.vs, aruco_dict, parameters, detected,
variables, operators, loops, timeout, updated, self.console)
self.frames[0] = cv2.resize(self.frames[0], (300,300))
self.frames[1] = cv2.resize(self.frames[1], (300,300))
# swap the channels becuase openCV uses BGR whereas PIL
# uses RGB
image1 = cv2.cvtColor(self.frames[0], cv2.COLOR_BGR2RGB)
image1 = Image.fromarray(image1)
image1 = ImageTk.PhotoImage(image1)
image2 = cv2.cvtColor(self.frames[1], cv2.COLOR_BGR2RGB)
image2 = Image.fromarray(image2)
image2 = ImageTk.PhotoImage(image2)
# create the panel
if self.panel1 is None:
self.panel1 = tk.Label(image=image1)
self.panel1.image = image1
self.panel1.place(relx=0.05, rely=0.15)
else:
# update the panel
self.panel1.configure(image=image1)
self.panel1.image = image1
if self.panel2 is None:
self.panel2 = tk.Label(image=image2)
self.panel2.image = image2
self.panel2.place(relx=0.05, rely=0.55)
else:
self.panel2.configure(image=image2)
self.panel2.image = image2
except RuntimeError:
print("[INFO] caught a RuntimeError")
# run from the same file
if (__name__ == "__main__"):
window = tk.Tk()
window.title("ARCVision")
window.geometry("500x500")
menu = Menu(window)
menu.show()
menu.window.mainloop()
# new window
window = tk.Tk()
window.title("ARCVision")
window.geometry("800x800")
app = App(window, cap)
app.show()
app.window.mainloop()