-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
198 lines (155 loc) · 5.82 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 os
import sys
import PyQt5
from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QRadioButton
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5 import QtCore # , QtGui, QtWidgets
import csv
PATH = 'C:/Users/ykest/Desktop/EngSci Year 3/ECE324/project_test'
#* Grabs *.jpg files in the directory PATH
def getImgFiles(PATH):
directory = os.listdir(PATH)
imgs = []
for f in directory:
name, ext = os.path.splitext(f)
if ext == '.jpg':
imgs.append(f)
return imgs
class LabelApp(QWidget):
def __init__(self,parent = None):
super(LabelApp,self).__init__(parent)
self.setWindowTitle('Label Triplets')
self.setGeometry(0, 0, 600, 400)
self.setFixedWidth(1200)
self.output = []
layout1 = QHBoxLayout()
self.files = getImgFiles(PATH)
for img_name in self.files:
image = QLabel()
pixmap = QPixmap(os.path.join(PATH, img_name))
image.resize(200, 200)
image.setPixmap(pixmap.scaled(image.size(), QtCore.Qt.KeepAspectRatio))
layout1.addWidget(image)
layout2 = QVBoxLayout()
btnLabel = QLabel(
"Select the expression that is the most different from the others:\n"
)
layout2.addWidget(btnLabel)
layout3 = QHBoxLayout()
layout4 = QVBoxLayout()
self.b1 = QRadioButton("Image 1")
self.b1.toggled.connect(lambda:self.btnstate(self.b1))
layout4.addWidget(self.b1)
self.b2 = QRadioButton("Image 2")
self.b2.toggled.connect(lambda:self.btnstate(self.b2))
layout4.addWidget(self.b2)
self.b3 = QRadioButton("Image 3")
self.b3.toggled.connect(lambda:self.btnstate(self.b3))
layout4.addWidget(self.b3)
layout3.addLayout(layout4)
self.save = QPushButton("Save")
self.save.clicked.connect(lambda:self.register(self.b1,self.b2,self.b3))
layout3.addWidget(self.save)
layout2.addLayout(layout3)
layout1.addLayout(layout2)
self.setLayout(layout1)
#* Callback for save button
def register(self,*argv):
self.out = None
for button in argv:
if button.isChecked() == True:
self.out = button.text() + " SAVED"
break
if self.out != None:
self.label = [self.files,self.out] #! Esther: here is the saved variable
# print(self.label)
else:
print("Select Image before saving!")
self.output = self.output + [self.label]
return self.output
#* Callback for buttons 1 2 3
def btnstate(self,b):
if b.isChecked() == True:
print(b.text() + " is selected")
else:
print(b.text() + " is deselected")
#* Trigger for keypress
def keyPressEvent(self, event):
#* Press number keys to select image
if event.key() == QtCore.Qt.Key_1:
print("Image 1")
self.b1.toggle()
elif event.key() == QtCore.Qt.Key_2:
print("Image 2")
self.b2.toggle()
elif event.key() == QtCore.Qt.Key_3:
print("Image 3")
self.b3.toggle()
#* Pressing either enter key
elif event.key() == QtCore.Qt.Key_Enter or event.key() == QtCore.Qt.Key_Return:
print("ENTER")
self.save.click()
event.accept()
def main():
app = QApplication(sys.argv)
demo = LabelApp()
demo.show()
app.exec_()
# sys.exit(app.exec_())
if os.path.exists(PATH + "/output.csv"):
mode = "a+"
else:
mode = "w"
with open(PATH + "/output.csv", mode, newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_NONE)
for i in demo.output:
label = int(i[1].split("Image ")[1].split(" ")[0])
writer.writerow([i[0][0], i[0][1], i[0][2], label])
sys.exit()
if __name__ == "__main__":
main()
#*****************************************************
#TODO: Stuff to remove ...
#? Textbox in GUI -> Use QLineEdit
#? Button in GUI -> QPushButton
# window = QWidget()
# window.setWindowTitle('PyQt5 App')
# #* (x position,y position,width,height)
# # window.setGeometry(100, 100, 280, 80)
# # window.move(60, 15)
# # helloMsg = QLabel('<h1>Hello World!</h1>', parent=window)
# # helloMsg.move(60, 15)
# # helloMsg.show()
# hlay = QHBoxLayout()
# for img_name in ('26_K_1.jpg',
# '26_K_2.jpg',
# '26_K_3.jpg'):
# label = QLabel()
# pixmap = QPixmap(os.path.join(PATH, img_name))
# label.resize(250, 250)
# label.setPixmap(pixmap.scaled(label.size(), QtCore.Qt.KeepAspectRatio))
# hlay.addWidget(label)
# # label = QLabel('<h1>Hello World!</h1>', parent=window)
# # pixmap = QPixmap(PATH)
# # label.setPixmap(pixmap)
# #button test
# btnLabel = QLabel(
# "Identify which two of the three images shown have more similar facial"
# " expressions. Select the odd one out using the buttons:"
# )
# btn1 = QRadioButton("1")
# btn2 = QRadioButton("2")
# btn3 = QRadioButton("3")
# # radio = RadioButton(hlay)
# hlay.addWidget(btnLabel)
# hlay.addWidget(btn1)
# hlay.addWidget(btn2)
# hlay.addWidget(btn3)
# window.setLayout(hlay)
# window.show()