Matthew-Gallardo
/
Detection-of-GAN-Generated-Images-using-Spatial-Frequency-Domain-Fusion-Data
Public
forked from Deynnnyellll/Detection-of-GAN-Generated-Images-using-Spatial-Frequency-Domain-Fusion-Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
688 lines (593 loc) · 30 KB
/
app.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import (QFileDialog, QWidget, QLabel, QMainWindow, QApplication, QScrollArea, QTableWidget, QTableWidgetItem,QVBoxLayout,)
from PyQt6.QtGui import QPixmap, QImage , QIcon
import sys
from pathlib import Path
import os
from liblinear_model import linear_predict_proba
from liblinear.liblinearutil import load_model
from tkinter import messagebox
import fitz
import pandas
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
os.system('cls')
#Container for Real and Gan Images
self.images = []
# initialize value of model and clf
self.loaded_model = None
self.clf = None
# result and probability estimates
self.result = []
self.prob = []
# true labels
self.true_labels = []
QMainWindow().__init__(self)
self.ui = MainWindow
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 646)
self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
#Get Started Button
self.getStarted = QtWidgets.QPushButton(parent=self.centralwidget)
self.getStarted.setGeometry(QtCore.QRect(440, 330, 121, 51))
self.getStarted.setStyleSheet("* {\n""background: transparent;\n""}")
self.getStarted.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("resources/Button1.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.getStarted.setIcon(icon)
self.getStarted.setIconSize(QtCore.QSize(110, 110))
self.getStarted.setObjectName("getStarted")
#Welcome Page Icons & Labels
self.icon = QtWidgets.QLabel(parent=self.centralwidget)
self.icon.setGeometry(QtCore.QRect(370, 340, 261, 261))
self.icon.setText("")
self.icon.setPixmap(QtGui.QPixmap("resources/Icon.png"))
self.icon.setScaledContents(True)
self.icon.setObjectName("icon")
self.gallery1 = QtWidgets.QLabel(parent=self.centralwidget)
self.gallery1.setGeometry(QtCore.QRect(660, 510, 171, 121))
self.gallery1.setText("")
self.gallery1.setPixmap(QtGui.QPixmap("resources/gallery1.png"))
self.gallery1.setScaledContents(True)
self.gallery1.setObjectName("gallery1")
self.eye1 = QtWidgets.QLabel(parent=self.centralwidget)
self.eye1.setGeometry(QtCore.QRect(670, 370, 171, 161))
self.eye1.setText("")
self.eye1.setPixmap(QtGui.QPixmap("resources/eye.png"))
self.eye1.setScaledContents(True)
self.eye1.setObjectName("eye1")
self.gallery2 = QtWidgets.QLabel(parent=self.centralwidget)
self.gallery2.setGeometry(QtCore.QRect(180, 370, 171, 171))
self.gallery2.setText("")
self.gallery2.setPixmap(QtGui.QPixmap("resources/gallery.png"))
self.gallery2.setScaledContents(True)
self.gallery2.setObjectName("gallery2")
self.eye2 = QtWidgets.QLabel(parent=self.centralwidget)
self.eye2.setGeometry(QtCore.QRect(170, 510, 171, 121))
self.eye2.setText("")
self.eye2.setPixmap(QtGui.QPixmap("resources/eye1.png"))
self.eye2.setScaledContents(True)
self.eye2.setObjectName("eye2")
self.title = QtWidgets.QLabel(parent=self.centralwidget)
self.title.setGeometry(QtCore.QRect(210, 120, 601, 51))
self.title.setStyleSheet("* {\n""font: 700 45pt \"Arial\";\n""}")
self.title.setText("")
self.title.setPixmap(QtGui.QPixmap("resources/Welcome to App Name.png"))
self.title.setScaledContents(True)
self.title.setObjectName("title")
self.logoName = QtWidgets.QLabel(parent=self.centralwidget)
self.logoName.setGeometry(QtCore.QRect(50, 30, 111, 16))
self.logoName.setStyleSheet("*{\n""font: 700 10pt \"Arial\";\n""color:rgb(255, 255, 255);\n""}")
self.logoName.setObjectName("logoName")
self.logo = QtWidgets.QLabel(parent=self.centralwidget)
self.logo.setGeometry(QtCore.QRect(10, 20, 31, 31))
self.logo.setText("")
self.logo.setPixmap(QtGui.QPixmap("resources/openAI-logo.png"))
self.logo.setScaledContents(True)
self.logo.setObjectName("logo")
self.socmedIcons = QtWidgets.QLabel(parent=self.centralwidget)
self.socmedIcons.setGeometry(QtCore.QRect(870, 30, 101, 16))
self.socmedIcons.setText("")
self.socmedIcons.setPixmap(QtGui.QPixmap("resources/Group 2.png"))
self.socmedIcons.setScaledContents(True)
self.socmedIcons.setObjectName("socmedIcons")
#Navbar
self.homeBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.homeBar.setGeometry(QtCore.QRect(240, 30, 75, 24))
self.homeBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(85, 255, 255)\n""}")
self.homeBar.setObjectName("homeBar")
self.datasetBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.datasetBar.setGeometry(QtCore.QRect(340, 30, 75, 24))
self.datasetBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.datasetBar.setObjectName("datasetBar")
self.modelBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.modelBar.setGeometry(QtCore.QRect(450, 30, 75, 24))
self.modelBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.modelBar.setObjectName("modelBar")
self.docuBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.docuBar.setGeometry(QtCore.QRect(560, 30, 91, 24))
self.docuBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.docuBar.setObjectName("docuBar")
self.aboutBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.aboutBar.setGeometry(QtCore.QRect(690, 30, 51, 24))
self.aboutBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.aboutBar.setObjectName("aboutBar")
self.imageLabel = QtWidgets.QLabel(parent=self.centralwidget)
self.imageLabel.setGeometry(QtCore.QRect(70, 90, 51, 41))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
self.imageLabel.setFont(font)
self.imageLabel.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.imageLabel.setObjectName("imageLabel")
self.resultLabel = QtWidgets.QLabel(parent=self.centralwidget)
self.resultLabel.setGeometry(QtCore.QRect(550, 100, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
self.resultLabel.setFont(font)
self.resultLabel.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.resultLabel.setObjectName("resultLabel")
self.aibutton = QtWidgets.QPushButton(parent=self.centralwidget)
self.aibutton.setGeometry(QtCore.QRect(270, 540, 121, 51))
self.aibutton.setStyleSheet("* {\n""background: transparent\n""}")
self.aibutton.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("resources/Ai.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.aibutton.setIcon(icon)
self.aibutton.setIconSize(QtCore.QSize(105, 105))
self.aibutton.setObjectName("aibutton")
self.clearbutton = QtWidgets.QPushButton(parent=self.centralwidget)
self.clearbutton.setGeometry(QtCore.QRect(130, 540, 121, 51))
self.clearbutton.setStyleSheet("* {\n""background: transparent\n""}")
self.clearbutton.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("resources/Clear.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.clearbutton.setIcon(icon1)
self.clearbutton.setIconSize(QtCore.QSize(105, 105))
self.clearbutton.setObjectName("clearbutton")
self.Rectangle1 = QtWidgets.QLabel(parent=self.centralwidget)
self.Rectangle1.setGeometry(QtCore.QRect(60, 90, 401, 521))
self.Rectangle1.setText("")
self.Rectangle1.setPixmap(QtGui.QPixmap("resources/Rectangle.png"))
self.Rectangle1.setScaledContents(True)
self.Rectangle1.setObjectName("Rectangle1")
self.Rectangle1.lower()
self.Rectangle2 = QtWidgets.QLabel(parent=self.centralwidget)
self.Rectangle2.setGeometry(QtCore.QRect(540, 90, 401, 521))
self.Rectangle2.setText("")
self.Rectangle2.setPixmap(QtGui.QPixmap("resources/Rectangle.png"))
self.Rectangle2.setScaledContents(True)
self.Rectangle2.setObjectName("Rectangle2")
self.uploadBar = QtWidgets.QPushButton(parent=self.centralwidget)
self.uploadBar.setGeometry(QtCore.QRect(10, 95, 511, 511))
self.uploadBar.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.uploadBar.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("resources/Wrapper1.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.uploadBar.setIcon(icon2)
self.uploadBar.setIconSize(QtCore.QSize(580, 490))
self.uploadBar.setObjectName("uploadBar")
self.Wrapper = QtWidgets.QLabel(parent=self.centralwidget)
self.Wrapper.setGeometry(QtCore.QRect(520, 102, 451, 561))
self.Wrapper.setText("")
self.Wrapper.setPixmap(QtGui.QPixmap("resources/Wrapper1.png"))
self.Wrapper.setScaledContents(True)
self.Wrapper.setObjectName("Wrapper")
self.uploadButton = QtWidgets.QPushButton(parent=self.centralwidget)
self.uploadButton.setGeometry(QtCore.QRect(200, 240, 131, 121))
self.uploadButton.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.uploadButton.setText("")
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("resources/gallery2.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.uploadButton.setIcon(icon3)
self.uploadButton.setIconSize(QtCore.QSize(110, 110))
self.uploadButton.setObjectName("uploadButton")
self.eye3 = QtWidgets.QLabel(parent=self.centralwidget)
self.eye3.setGeometry(QtCore.QRect(650, 300, 191, 211))
font = QtGui.QFont()
font.setPointSize(12)
self.eye3.setFont(font)
self.eye3.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
self.eye3.setText("")
self.eye3.setPixmap(QtGui.QPixmap("resources/eye3.png"))
self.eye3.setScaledContents(True)
self.eye3.setObjectName("eye3")
self.eye4 = QtWidgets.QLabel(parent=self.centralwidget)
self.eye4.setGeometry(QtCore.QRect(690, 300, 191, 211))
font = QtGui.QFont()
font.setPointSize(12)
self.eye4.setFont(font)
self.eye4.setText("")
self.eye4.setObjectName("eye4")
self.eye3.setStyleSheet("* {\ncolor: rgb(255, 255, 255)\n""}")
self.aboutBar_2 = QtWidgets.QPushButton(parent=self.centralwidget)
self.aboutBar_2.setGeometry(QtCore.QRect(130, 340, 261, 111))
self.aboutBar_2.setStyleSheet("* {\n""background: transparent;\n""color: rgb(169,169,169)\n""}")
self.aboutBar_2.setObjectName("aboutBar_2")
self.aboutBar_2.setText("Click to upload your Image")
self.uploadLabel = QtWidgets.QLabel(parent=self.centralwidget)
self.uploadLabel.setGeometry(QtCore.QRect(110, 575, 321, 41))
self.uploadLabel.setText("By uploading an image, you agree to our Terms of Service")
font = QtGui.QFont()
font.setPointSize(9)
self.uploadLabel.setFont(font)
self.uploadLabel.setStyleSheet("* {\n""background: transparent;\n""color: rgb(255, 255, 255)\n""}")
# notifications
self.notif = QtWidgets.QLabel(parent=self.centralwidget)
self.notif.setGeometry(QtCore.QRect(855, 555, 150, 45))
self.notif.setText("")
self.notif.setPixmap(QtGui.QPixmap("resources/loading.png"))
self.notif.setScaledContents(True)
self.notif.setObjectName("eye2")
self.setWelcomePage()
# select model
self.modelBar.clicked.connect(self.select_model)
self.docuBar.clicked.connect(self.setDocumentationPage)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "illuscan"))
self.logoName.setText(_translate("MainWindow", "illuscan"))
self.homeBar.setText(_translate("MainWindow", "Home"))
self.datasetBar.setText(_translate("MainWindow", "Dataset"))
self.modelBar.setText(_translate("MainWindow", "Model"))
self.docuBar.setText(_translate("MainWindow", "Documentation"))
self.aboutBar.setText(_translate("MainWindow", "About"))
def setWelcomePage(self):
MainWindow.setStyleSheet("#centralwidget {\n""background-image: url(resources/Home page.png);\n""}")
self.icon.hide()
self.getStarted.show()
self.title.hide()
self.gallery1.hide()
self.gallery2.hide()
self.eye1.hide()
self.eye2.hide()
self.aboutBar.clicked.connect(self.setAboutPage)
self.imageLabel.hide()
self.resultLabel.hide()
self.aibutton.hide()
self.clearbutton.hide()
self.Rectangle1.hide()
self.Rectangle2.hide()
self.uploadBar.hide()
self.Wrapper.hide()
self.uploadButton.hide()
self.eye3.hide()
self.uploadLabel.hide()
self.aboutBar_2.hide()
self.eye4.hide()
self.notif.hide()
scroll_area = self.centralwidget.findChild(QScrollArea)
if scroll_area and scroll_area.isVisible():
scroll_area.hide()
self.getStarted.clicked.connect(self.setHomePage)
#MainWindow.setStyleSheet("#centralwidget {\n""background-image: url(resources/Get Started.jpg);\n""}")
self.aboutBar.clicked.connect(self.setAboutPage)
def setAboutPage(self):
MainWindow.setStyleSheet("#centralwidget {\n""background-image: url(resources/Get Started.jpg);\n""}")
self.icon.show()
self.getStarted.hide()
self.title.show()
self.gallery1.show()
self.gallery2.show()
self.eye1.show()
self.eye2.show()
self.imageLabel.hide()
self.resultLabel.hide()
self.aibutton.hide()
self.clearbutton.hide()
self.Rectangle1.hide()
self.Rectangle2.hide()
self.uploadBar.hide()
self.Wrapper.hide()
self.uploadButton.hide()
self.eye3.hide()
self.aboutBar_2.hide()
self.uploadLabel.hide()
self.notif.hide()
self.homeBar.clicked.connect(self.setWelcomePage)
def setHomePage(self):
MainWindow.setStyleSheet("#centralwidget {\n""background-image: url(resources/Get Started.jpg);\n""}")
self.icon.hide()
self.getStarted.hide()
self.icon.hide()
self.getStarted.hide()
self.title.hide()
self.gallery1.hide()
self.gallery2.hide()
self.eye1.hide()
self.eye2.hide()
self.notif.hide()
self.imageLabel.show()
self.resultLabel.show()
self.aibutton.raise_()
self.aibutton.show()
self.aibutton.clicked.connect(self.predict_result)
self.clearbutton.show()
self.clearbutton.raise_()
self.clearbutton.clicked.connect(self.clear_image)
self.Rectangle1.show()
self.Rectangle2.show()
self.uploadBar.show()
self.Wrapper.show()
self.uploadButton.show()
self.eye3.show()
self.uploadLabel.show()
self.aboutBar_2.show()
self.homeBar.clicked.connect(self.setWelcomePage)
self.uploadButton.clicked.connect(self.uploadImage)
self.image_container = QScrollArea(self.centralwidget)
self.image_container.setStyleSheet("background-color: transparent")
self.image_container.setGeometry(82, 110, 360, 420) # Adjust the size
self.image_container.setWidgetResizable(True)
self.image_grid_layout = QtWidgets.QGridLayout()
self.image_grid_layout.setColumnMinimumWidth(0, 0) # Set the minimum width to 0 for the first column
self.image_grid_layout.setColumnMinimumWidth(1, 100)
self.image_grid_layout.setColumnMinimumWidth(2, 100)
container_widget = QtWidgets.QWidget()
container_widget.setStyleSheet("border: none")
container_widget.setLayout(self.image_grid_layout)
self.image_container.setWidget(container_widget)
self.loadingDetection = QtWidgets.QLabel(parent=self.Wrapper)
self.loadingDetection.setGeometry(QtCore.QRect(160, 270, 261, 111))
self.loadingDetection.setScaledContents(True)
self.loadingDetection.setStyleSheet("* {\n""background: transparent;\n""color: rgb(169,169,169);\n"" font-size: 16px;}")
self.loadingDetection.setObjectName("loading")
self.loadingDetection.setText("Detecting Image")
def setDocumentationPage(self):
# Customize UI elements for the documentation page
MainWindow.setStyleSheet("#centralwidget {\n""background-image: url(resources/Get Started.jpg);\n""}")
self.getStarted.hide()
self.imageLabel.hide()
self.resultLabel.hide()
self.aibutton.hide()
self.clearbutton.hide()
self.Rectangle1.hide()
self.Rectangle2.hide()
self.uploadBar.hide()
self.Wrapper.hide()
self.uploadButton.hide()
self.eye3.hide()
self.aboutBar_2.hide()
self.uploadLabel.hide()
self.notif.hide()
self.homeBar.clicked.connect(self.setWelcomePage)
self.displayDocumentation()
def displayDocumentation(self):
doc = fitz.open('documentation/documentation.pdf')
scroll_area = QScrollArea(self.centralwidget)
scroll_area.setGeometry(150, 100, 700, 520) #Adjust the size x, y, width, height
scroll_area.setWidgetResizable(True)
content_widget = QWidget()
scroll_area.setWidget(content_widget)
layout = QVBoxLayout(content_widget)
for i in range(len(doc)):
page = doc.load_page(i)
pixmap = page.get_pixmap()
label = QLabel()
label.setPixmap(QPixmap.fromImage(
QImage(pixmap.samples, pixmap.width, pixmap.height, pixmap.stride,
QImage.Format.Format_RGB888 if pixmap.alpha == 0 else QImage.Format.Format_RGBA8888))
)
layout.addWidget(label)
scroll_area.show()
# upload images and display them in the left panel of home page
def uploadImage(self):
home_dir = str(Path.home())
fname, _ = QFileDialog.getOpenFileNames(self, 'Open file', home_dir)
if fname:
self.image_container.show()
self.uploadButton.hide()
self.aboutBar_2.hide()
for idx, file in enumerate(fname):
pixmap = QPixmap(file)
pixmap = pixmap.scaled(110, 110)
image_label = QtWidgets.QLabel()
image_label.setPixmap(pixmap)
file_label = QtWidgets.QLabel(os.path.basename(file))
file_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
# Create a layout to contain the image and file name label
self.widget = QtWidgets.QWidget()
self.widget.setFixedHeight(140)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(image_label)
layout.addWidget(file_label)
self.widget.setLayout(layout)
self.images.append(file)
row = idx // 3
col = idx % 3
if col == 0:
self.image_grid_layout.setColumnMinimumWidth(0, 0)
self.image_grid_layout.addWidget(self.widget, row, col)
# Adjust spacing between widgets
self.image_grid_layout.setVerticalSpacing(10) # Change the value as needed for desired spacing
# get the basename (example: /user/hi.png => hi.png)
def get_basename(self, images):
images_basename = [os.path.basename(images) for images in self.images]
return images_basename
def get_true_labels(self, images):
filename = os.path.relpath(images)
# read from stylegan datasets
df = pandas.read_csv('datasets.csv')
labels_dict = dict(zip(df['Filename'], df['True Labels']))
if filename in labels_dict:
true_labels = labels_dict[filename]
if true_labels == 0:
return "Real"
elif true_labels == 1:
return "GAN"
else:
return "No Label"
# detect whether an image is gan or real with probability estimates
def predict_result(self):
try:
if len(self.images) != 0:
if self.loaded_model is None:
print("No model loaded")
else:
self.loadingDetection.show() # Show the label before processing starts
QApplication.processEvents()# Show the label before processing starts
result, likelihood = linear_predict_proba(self.images, self.loaded_model, self.clf)
for prob, pred in zip(likelihood, result):
self.prob.append(prob)
self.result.append(pred)
QApplication.processEvents()
self.display_result()
except Exception as e:
print(f"Something went wrong! : {e}")
import traceback
traceback.print_exc()
finally:
self.loadingDetection.hide()
# reset
def clear_image(self):
if len(self.images) != 0:
self.prob.clear()
self.result.clear()
self.eye3.show()
self.loadingDetection.hide()
try:
try:
self.result_table.setRowCount(0)
self.result_table.clearContents()
self.result_table.hide()
self.processed_images_label.hide()
self.detected_real_label.hide()
self.detected_gan_label.hide()
except:
pass
# Clear the image labels from the layout
for i in reversed(range(self.image_grid_layout.count())):
self.image_grid_layout.itemAt(i).widget().setParent(None)
self.images.pop(i)
self.uploadButton.show()
self.image_container.hide()
self.aboutBar_2.show()
os.system('cls')
messagebox.showinfo(message=f"Images {len(self.images)}")
except Exception as e:
print(f"Something went wrong! : {e}")
else:
messagebox.showinfo(message="Images already cleared")
# display the result in table
def display_result(self):
# get the true labels of images
try:
true_labels = [self.get_true_labels(image) for image in self.images]
except Exception as e:
print(f"There is an error with the csv file: {e}")
true_labels = []
# Create labels for statistics
self.processed_images_label = QtWidgets.QLabel(parent=self.centralwidget)
self.processed_images_label.setGeometry(670, 440, 200, 20)
self.detected_real_label = QtWidgets.QLabel(parent=self.centralwidget)
self.detected_real_label.setGeometry(670, 455, 200, 20)
self.detected_gan_label = QtWidgets.QLabel(parent=self.centralwidget)
self.detected_gan_label.setGeometry(670, 470, 200, 20)
self.processed_images_label.setStyleSheet("color:white; font-size: 11pt")
self.detected_real_label.setStyleSheet("color:white; font-size: 11pt")
self.detected_gan_label.setStyleSheet("color:white; font-size: 11pt")
if len(self.result) > 0:
if all(l == "No Label" for l in true_labels) or len(true_labels) == 0:
self.result_table = QTableWidget(self.centralwidget)
self.result_table.setRowCount(len(self.result))
self.result_table.setColumnCount(4)
self.result_table.setHorizontalHeaderLabels(["Image Name", "GAN Probability", "Real Probability", "Prediction"])
self.result_table.setStyleSheet("background-color: transparent")
self.result_table.setGeometry(560, 115, 363, 320) # Adjust the size x, y, width, height
self.result_table.horizontalHeader().setStyleSheet("background-color: transparent")
self.result_table.verticalHeader().setStyleSheet("background-color: transparent")
for row, (image_name, prediction, probability) in enumerate(zip(self.get_basename(self.images), self.result, self.prob)):
self.result_table.setItem(row, 0, QTableWidgetItem(image_name))
self.result_table.setItem(row, 1, QTableWidgetItem(f"{(probability[0] * 100):.2f}"))
self.result_table.setItem(row, 2, QTableWidgetItem(f"{(probability[1] * 100):.2f}"))
self.result_table.setItem(row, 3, QTableWidgetItem(prediction))
else:
self.result_table = QTableWidget(self.centralwidget)
self.result_table.setRowCount(len(self.result))
self.result_table.setColumnCount(5)
self.result_table.setHorizontalHeaderLabels(["Image Name", "GAN Probability", "Real Probability", "True Labels", "Prediction"])
self.result_table.setStyleSheet("background-color: transparent")
self.result_table.setGeometry(560, 115, 363, 320) # Adjust the size x, y, width, height
self.result_table.horizontalHeader().setStyleSheet("background-color: transparent")
self.result_table.verticalHeader().setStyleSheet("background-color: transparent")
for row, (image_name, prediction, probability, labels) in enumerate(zip(self.get_basename(self.images), self.result, self.prob, true_labels)):
self.result_table.setItem(row, 0, QTableWidgetItem(image_name))
self.result_table.setItem(row, 1, QTableWidgetItem(f"{(probability[0] * 100):.2f}"))
self.result_table.setItem(row, 2, QTableWidgetItem(f"{(probability[1] * 100):.2f}"))
self.result_table.setItem(row, 3, QTableWidgetItem(labels))
self.result_table.setItem(row, 4, QTableWidgetItem(prediction))
self.eye3.hide()
self.result_table.resizeColumnsToContents()
self.result_table.show()
# Update labels for statistics
self.processed_images_text = f"Processed Images: {len(self.images)}"
self.detected_real_text = f"Detected Real: {self.result.count('Real')}"
self.detected_gan_text = f"Detected GAN: {self.result.count('GAN')}"
self.processed_images_label.setText(self.processed_images_text)
self.detected_real_label.setText(self.detected_real_text)
self.detected_gan_label.setText(self.detected_gan_text)
# Show labels
self.processed_images_label.show()
self.detected_real_label.show()
self.detected_gan_label.show()
else:
self.processed_images_label.setText("Processed Images: 0")
self.detected_real_label.setText("Detected Real: 0")
self.detected_gan_label.setText("Detected GAN: 0")
# Show labels
self.processed_images_label.show()
self.detected_real_label.show()
self.detected_gan_label.show()
messagebox.showinfo(message="No results to display. Please predict results first.")
self.processed_images_label.hide()
self.detected_real_label.hide()
self.detected_gan_label.hide()
# select model
def select_model(self):
self.type = None
self.notif.show()
try:
home_dir = str(Path.home())
self.model_file, _ = QFileDialog.getOpenFileNames(self, 'Open file', home_dir)
if ".model" in self.model_file[0]:
self.loaded_model = load_model(self.model_file[0])
self.notif.hide()
messagebox.showinfo(message=f"Model Loaded Successfully")
print(self.model_file)
# load trained platt scale (include own directory)
if "faces" in self.model_file[0]:
self.type = "platt scaler/platt_scale_faces.model"
elif "animals" in self.model_file[0]:
self.type = "platt scaler/platt_scale_animals.model"
elif "objects" in self.model_file[0]:
self.type = "platt scaler/platt_scale_objects.model"
elif "scenes" in self.model_file[0]:
self.type ="platt scaler/platt_scale_scenes.model"
else:
self.type = "platt scaler/platt_scale_combinedclass.model"
# load the clf
print(self.type)
self.clf = load_model(self.type)
else:
self.notif.hide()
messagebox.showinfo(message="Incompatible model file")
except:
print("Something went wrong!")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
icon_path = "resources/openAI-logo.png"
icon = QtGui.QIcon(icon_path)
MainWindow.setWindowIcon(icon)
MainWindow.show()
sys.exit(app.exec())