-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_evolution.py
executable file
·1402 lines (1265 loc) · 56.5 KB
/
abstract_evolution.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
"""
Abstract evolution
Made for VIN class at Brno University of Technology
Uses genetic programming to evolve image into passes in image,
generating similar images yet abstract and always unique
"""
__author__ = "Marek Sedlacek (xsedla1b)"
__date__ = "September 2021"
__version__ = "0.2.1"
__email__ = ("[email protected]", "[email protected]")
import PyQt5
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QMainWindow,
QApplication,
QAction,
QPushButton,
QFileDialog,
QLabel,
QLineEdit,
QFormLayout,
QSlider,
QCheckBox,
QProgressBar,
QComboBox,
QWidgetAction,
QSizePolicy,
QTextBrowser,
QMessageBox)
from PyQt5.QtGui import (QPixmap,
QIntValidator)
import webbrowser
import gp
import json
class Lang:
"""
Class holding GUI translations
"""
TEXT = {
"save": {
"cz": "Uložit",
"en": "Save",
"fr": "Enregistrer"
},
"evolve_new": {
"cz": "Nová evoluce",
"en": "Evolve new",
"fr": "Évoluer nouveau"
},
"evolve_again": {
"cz": "Evolvuj znovu",
"en": "Evolve again",
"fr": "Évoluer à nouveau"
},
"save_params": {
"cz": "Uložit konfiguraci",
"en": "Save configuration",
"fr": "Enregistrer configuration"
},
"load_config": {
"cz": "Nahrát konfiguraci",
"en": "Load configuration",
"fr": "Charger configuration"
},
"error": {
"cz": "Chyba",
"en": "Error",
"fr": "Erreur"
},
"please_wait": {
"cz": "In silico evoluce může trvat několik minut...",
"en": "In silico evolution might take a few minutes...",
"fr": "L'évolution peut prendre quelques minutes..."
},
"file": {
"cz": "Soubor",
"en": "File",
"fr": "Ficher"
},
"language": {
"cz": "Jazyk",
"en": "Language",
"fr": "Langue"
},
"about": {
"cz": "Informace",
"en": "About",
"fr": "À propos"
},
"upload_image": {
"cz": "Nahrát obrázek",
"en": "Upload image",
"fr": "Télécharger l'image"
},
"evolution_parameters": {
"cz": "Parametry evoluce",
"en": "Evolution parameters",
"fr": "Paramètres d'évolution"
},
"quit": {
"cz": "Ukončit Abstract Evolution",
"en": "Quit Abstract Evolution",
"fr": "Quitter Abstract Evolution"
},
"select_image": {
"cz": "Vyberte obrázek nad kterým provádět evoluci",
"en": "Select file to evolve into",
"fr": "Sélectionnez une image pour évoluer vers"
},
"select_file": {
"cz": "Vyberte konfigurační soubor",
"en": "Select a configuration file",
"fr": "Sélectionnez un fichier de configuration"
},
"params_window_title": {
"cz": "Parametry evoluce",
"en": "Evolution parameters",
"fr": "Paramètres d'évolution"
},
"help": {
"cz": "Pomoc",
"en": "Help",
"fr": "Aider"
},
"iterations": {
"cz": "Iterace",
"en": "Iterations",
"fr": "Itérations"
},
"population_size": {
"cz": "Velikost populace",
"en": "Population size",
"fr": "Taille de la population"
},
"random_colors": {
"cz": "Náhodné barvy",
"en": "Random colors",
"fr": "Couleurs aléatoires"
},
"unique_colors": {
"cz": "Unikátní barvy",
"en": "Unique colors",
"fr": "Couleurs uniques"
},
"elitism": {
"cz": "Elitářství",
"en": "Elitism",
"fr": "Élitisme"
},
"crossover": {
"cz": "Křížení",
"en": "Crossover",
"fr": "Croisement"
},
"ch": {
"cz": "CH (Histogram barev)",
"en": "CH (Color histogram)",
"fr": "CH (Histogramme des couleurs)"
},
"rpm": {
"cz": "RPM (Náhodné body)",
"en": "RPM (Random point match)",
"fr": "RPM (Points aléatoires)"
},
"upm": {
"cz": "UPM (Rovnoměrně rozprostřené body)",
"en": "UPM (Uniform point match)",
"fr": "UPM (Points régulièrement espacés)"
},
"fitness_function": {
"cz": "Fitness funkce",
"en": "Fitness function",
"fr": "Fonction fitness"
},
"amount_of_points": {
"cz": "Počet bodů",
"en": "Amount of points",
"fr": "Nombre de points"
},
"point_size": {
"cz": "Velikost bodu",
"en": "Point size",
"fr": "Taille d'un point"
},
"update_every": {
"cz": "Obnov každých",
"en": "Update image every",
"fr": "Mise à jour tous les"
},
"set_defaults": {
"cz": "Výchozí nastavení",
"en": "Set defaults",
"fr": "Paramètres par défaut"
},
"ok": {
"cz": "OK",
"en": "OK",
"fr": "D'accord"
},
"start_evolution": {
"cz": "Začít evoluci!",
"en": "Start evolution!",
"fr": "Commencer l'évolution!"
},
"finished": {
"cz": "Dokončeno",
"en": "Finished",
"fr": "Fini"
},
"evolve_lines": {
"cz": "Evolvuj křivky",
"en": "Evolve lines",
"fr": "Évoluer des lignes"
},
"max_seeds": {
"cz": "Maximum semínek",
"en": "Maximum line seeds",
"fr": "Maximum de graines"
},
"min_seeds": {
"cz": "Minimum semínek",
"en": "Minimum line seeds",
"fr": "Minimum de graines"
},
"max_seed_w": {
"cz": "Maximální šířka křivky",
"en": "Maximum line width",
"fr": "Largeur de ligne maximale"
},
"min_seed_w": {
"cz": "Minimální šířka křivky",
"en": "Minimum line width",
"fr": "Largeur de ligne minimale"
},
"min_seed_l": {
"cz": "Minimální délka křivky",
"en": "Minimum line length",
"fr": "Longueur de ligne minimale"
},
"dir_change_chance": {
"cz": "Šance na změnu směru",
"en": "Direction change chance",
"fr": "Chance de changement de direction"
},
"grow_during_evolution": {
"cz": "Růst semínek během evoluce",
"en": "Grow seeds during evolution",
"fr": "Pousser des graines pendant l'évolution"
},
"new_window": {
"cz": "Nový projekt",
"en": "New project",
"fr": "Nouveau projet"
},
"mutation_chance": {
"cz": "Šance mutace",
"en": "Mutation chance",
"fr": "Chance de mutation"
},
"exact_pm": {
"cz": "Uznej pouze stejné barvy",
"en": "Match only same colors",
"fr": "Reconnaître uniquement les mêmes couleurs"
},
"max_color_diff": {
"cz": "Maximální barevná odchylka",
"en": "Maximal color difference",
"fr": "Différence de couleur maximale"
},
"abstract": {
"cz": "Abstraktní",
"en": "Abstract",
"fr": "Abstrait"
},
"detailed": {
"cz": "Detailní",
"en": "Detailed",
"fr": "Détaillé"
},
"rushed": {
"cz": "Uspěchané",
"en": "Rushed",
"fr": "Précipité"
},
"developed": {
"cz": "Rozvinuté",
"en": "Developed",
"fr": "Développé"
},
"sliders_title": {
"cz": "Rychlé nastavení parametrů",
"en": "Quick parameters setup",
"fr": "Paramétrage rapide"
},
"show_advanced": {
"cz": "Ukaž pokročilé nastavení parametrů",
"en": "Show advanced parameters",
"fr": "Afficher les paramètres avancés"
},
"about_text": {
"cz": """
<div style=\"text-align:center\">
<h3>Abstract evolution</h3>
<p>Program pro generovaní abstraktního počítačového umění pomocí genetického programování.</p><br>
<b>Verze: </b>{}<br>
<b>Autor: </b>Marek Sedláček<br>
Navštivte <a href=https://github.com/mark-sed/abstract-evolution>GitHub stránky</a> pro nové verze programu.</div>
<br><br><small><i>Copyright (c) 2021 Marek Sedláček</i></small>
""".format(__version__),
"en": """
<div style=\"text-align:center\">
<h3>Abstract evolution</h3>
<p>A program for generating abstract computer art using genetic programming.</p><br>
<b>Version: </b>{}<br>
<b>Author: </b>Marek Sedlacek<br>
Visit <a href=https://github.com/mark-sed/abstract-evolution>GitHub page</a> to check for a new version.</div>
<br><br><small><i>Copyright (c) 2021 Marek Sedláček</i></small>
""".format(__version__),
"fr": """
<div style=\"text-align:center\">
<h3>Abstract evolution</h3>
<p>Un programme pour générer de l'art informatique abstrait en utilisant la programmation génétique.</p><br>
<b>Version: </b>{}<br>
<b>Auteur: </b>Marek Sedlacek<br>
Visitez la <a href=https://github.com/mark-sed/abstract-evolution>page GitHub</a> pour rechercher une nouvelle version.</div>
<br><small><i>Copyright (c) 2021 Marek Sedláček</i></small>
""".format(__version__),
}
}
"""
"": {
"cz": "",
"en": "",
"fr": ""
}
"""
# TODO: Add stop evolution button
# TODO: Test different picture formats and wirte the ones that can be used to the readme
# TODO: Display text what's initializing before the evolution start
# TODO: Section out the parameters windows to make it easier to use
# TODO: Min/Max mutation size?
# TODO: Icon/Logo
# TODO: Compile it also to binary
# TODO: Add (?) mouseover to params so people know what it does and if more or less is better
# TODO: Add to pip
# TODO: Determine the values based on the image size
# TODO: Add option to continue evolution with more cycles or even load image on which to continue
# TODO: Use QThread to run evolution so that the GUI does not freez up
# TODO: Scale up option? Would evolve smaller image and then user could scale it up
# TODO: Option to save in progress image (maybe even option to automatically save every update)
class MainWindow(QMainWindow):
"""
Main program windows
"""
# Holds amount of projects created this session (to offset window)
INSTANCES_CREATED = 0
def __init__(self, width=1024, height=600, lang="en"):
"""
Constructor
:param width Main window width
:param height Main window height
:param lang Language in which to display the GUI ("en"/"fr"/"cz"). "en" by default.
"""
super(MainWindow, self).__init__()
MainWindow.INSTANCES_CREATED += 1
self.setWindowIcon(QtGui.QIcon('icon.png'))
self.lang = lang
self.setFixedSize(width, height)
# Center the screen
screen = QApplication.desktop().screenNumber(QApplication.desktop().cursor().pos())
center = QApplication.desktop().screenGeometry(screen).center()
self.move(center.x() - self.width() // 2 + (MainWindow.INSTANCES_CREATED % 5)*20,
center.y() - self.height() // 2 - (MainWindow.INSTANCES_CREATED % 5)*20)
self.evolution_params = EvolutionParams(self)
self.reference_image = None
self.image_label = QLabel(self)
self.original_image_label = QLabel(self)
self.save_image_button = QPushButton(Lang.TEXT["save"][self.lang], self)
self.save_image_button.pressed.connect(self.save_image)
self.save_image_button.adjustSize()
self.save_image_button.hide()
self.save_params_button = QPushButton(Lang.TEXT["save_params"][self.lang], self)
self.save_params_button.pressed.connect(self.save_params)
self.save_params_button.adjustSize()
self.save_params_button.hide()
self.current_displyed_image = None
self.evolve_another_button = QPushButton(Lang.TEXT["evolve_new"][self.lang], self)
self.evolve_another_button.pressed.connect(self.evolve_new)
self.evolve_another_button.adjustSize()
self.evolve_another_button.hide()
self.evolve_again_button = QPushButton(Lang.TEXT["evolve_again"][self.lang], self)
self.evolve_again_button.pressed.connect(self.evolve_again)
self.evolve_again_button.adjustSize()
self.evolve_again_button.hide()
self.progress_bar = QProgressBar(self)
self.progress_bar.setGeometry(0, 0, 400, 25)
self.progress_bar.move(self.width()//2-self.progress_bar.width()//2, self.height()-180)
self.please_wait_label = QLabel(Lang.TEXT["please_wait"][self.lang], self)
self.please_wait_label.adjustSize()
self.please_wait_label.move(self.width()//2-self.please_wait_label.width()//2,
self.progress_bar.y()-self.please_wait_label.height()-5)
self.about_window = AboutWindow(self)
self.initUI()
def initUI(self):
"""
Initializes UI
"""
self.setWindowTitle("Abstract Evolution")
# Adding a menu bar
self.menuBar().clear()
self.menu_bar = self.menuBar()
self.mb_file = self.menu_bar.addMenu(Lang.TEXT["file"][self.lang])
self.mb_language = self.menu_bar.addMenu(Lang.TEXT["language"][self.lang])
self.button_about = QAction(Lang.TEXT["about"][self.lang])
self.mb_about = self.menu_bar.addAction(self.button_about)
self.button_about.triggered.connect(self.show_about)
# Help
self.button_help = QAction(Lang.TEXT["help"][self.lang])
self.mb_help = self.menu_bar.addAction(self.button_help)
self.button_help.triggered.connect(self.show_help)
# Setting up File menu bar
# New window
self.mb_new_window = QAction(Lang.TEXT["new_window"][self.lang])
self.mb_file.addAction(self.mb_new_window)
self.mb_new_window.triggered.connect(self.evolve_new)
# Upload image
self.mb_upload_image = QAction(Lang.TEXT["upload_image"][self.lang])
self.mb_file.addAction(self.mb_upload_image)
self.mb_upload_image.triggered.connect(self.upload_image)
# Evolution params
self.mb_evolution_params = QAction(Lang.TEXT["evolution_parameters"][self.lang])
self.mb_file.addAction(self.mb_evolution_params)
self.mb_evolution_params.triggered.connect(lambda: self.evolution_params.show())
# Quit
self.mb_quit = QAction(Lang.TEXT["quit"][self.lang])
self.mb_file.addAction(self.mb_quit)
self.mb_quit.triggered.connect(lambda: app.exit())
# Languages
self.mb_czech = QAction("Čeština")
self.mb_language.addAction(self.mb_czech)
self.mb_czech.triggered.connect(lambda: self.change_language("cz"))
self.mb_english = QAction("English")
self.mb_language.addAction(self.mb_english)
self.mb_english.triggered.connect(lambda: self.change_language("en"))
self.mb_french = QAction("Français")
self.mb_language.addAction(self.mb_french)
self.mb_french.triggered.connect(lambda: self.change_language("fr"))
# Upload image button in the middle of the windows when nothing is open
self.button_upload_image = QPushButton(Lang.TEXT["upload_image"][self.lang], self)
self.button_upload_image.resize(200, 50)
self.button_upload_image.move(self.width() // 2 - self.button_upload_image.width() // 2,
self.height() // 2 - self.button_upload_image.height() // 2 + 50)
self.button_upload_image.pressed.connect(self.upload_image)
# Params sliders
self.abstraction_slider = QSlider(Qt.Horizontal, self)
self.abstraction_slider.move(self.button_upload_image.x()+5, self.button_upload_image.y() - 50)
self.abstraction_slider.resize(self.button_upload_image.width()-10, 30)
self.abstraction_slider_label = QLabel(Lang.TEXT["abstract"][self.lang], self)
self.abstraction_slider_label.adjustSize()
self.abstraction_slider_label.move(self.abstraction_slider.x()-self.abstraction_slider_label.width()-10,
self.abstraction_slider.y()+5)
self.abstraction_slider_label2 = QLabel(Lang.TEXT["detailed"][self.lang], self)
self.abstraction_slider_label2.adjustSize()
self.abstraction_slider_label2.move(self.abstraction_slider.x()+self.abstraction_slider.width()+10,
self.abstraction_slider.y()+5)
self.value_abstraction = 72
self.abstraction_slider.setValue(self.value_abstraction)
self.abstraction_slider.valueChanged[int].connect(self.changed_abstraction_slider)
# Speed
self.speed_slider = QSlider(Qt.Horizontal, self)
self.speed_slider.move(self.button_upload_image.x()+5, self.abstraction_slider.y() - 30)
self.speed_slider.resize(self.button_upload_image.width()-10, 30)
self.speed_slider_label = QLabel(Lang.TEXT["rushed"][self.lang], self)
self.speed_slider_label.adjustSize()
self.speed_slider_label.move(self.speed_slider.x()-self.speed_slider_label.width()-10,
self.speed_slider.y()+5)
self.speed_slider_label2 = QLabel(Lang.TEXT["developed"][self.lang], self)
self.speed_slider_label2.adjustSize()
self.speed_slider_label2.move(self.speed_slider.x()+self.speed_slider.width()+10,
self.speed_slider.y()+5)
self.value_speed = 25
self.speed_slider.setValue(self.value_speed)
self.speed_slider.valueChanged[int].connect(self.changed_speed_slider)
# Title for slider
self.sliders_label = QLabel(Lang.TEXT["sliders_title"][self.lang], self)
self.sliders_label.setStyleSheet("font-size: 28px")
self.sliders_label.adjustSize()
self.sliders_label.move(self.width() // 2 - self.sliders_label.width() // 2, self.speed_slider.y()-60)
# Show advanced
self.show_advanced_params = QCheckBox(self)
self.show_advanced_params.move(15, self.height() - 40)
self.show_advanced_params.setChecked(True)
self.show_advanced_params_label = QLabel(Lang.TEXT["show_advanced"][self.lang], self)
self.show_advanced_params_label.setStyleSheet("color: #333333")
self.show_advanced_params_label.adjustSize()
self.show_advanced_params_label.move(self.show_advanced_params.x() + 20,
self.show_advanced_params.y() + 5)
self.evolution_params.quick_params_update(self.value_abstraction, self.value_speed)
# Hide progress
self.progress_bar.hide()
self.please_wait_label.hide()
# Show the window
self.show()
def upload_image(self):
"""
Opens dialog to upload image for evolution
"""
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self, Lang.TEXT["select_image"][self.lang], "","Images (*.png *.jpg *.jpeg);;All Files (*)", options=options)
if fileName:
self.reference_image = fileName
self.evolution_params.show()
self.evolution_params.activateWindow()
def display_image(self, image):
"""
Displays image on the main window
:param image Image to be displayed
"""
self.current_displyed_image = image
# Resize window
buttons_width = 5 + self.save_image_button.width() + 5 + self.evolve_again_button.width() + 5 + self.evolve_again_button.width() + 5
min_width = image.width()*2+15 if image.width()*2+15 > buttons_width else buttons_width
#self.resize(min_width, image.height()+10 + self.save_image_button.height() + 5)
self.setFixedSize(min_width, image.height()+10 + self.save_image_button.height() + 5)
# Hide extra gui
self.button_upload_image.hide()
self.menu_bar.hide()
self.show_advanced_params_label.hide()
self.show_advanced_params.hide()
self.sliders_label.hide()
self.speed_slider.hide()
self.speed_slider_label.hide()
self.speed_slider_label2.hide()
self.abstraction_slider.hide()
self.abstraction_slider_label.hide()
self.abstraction_slider_label2.hide()
# Display the image
self.original_image_label.setPixmap(QPixmap(self.reference_image))
self.original_image_label.resize(image.width(), image.height())
self.original_image_label.move(5, 5)
self.image_label.setPixmap(image)
self.image_label.resize(image.width(), image.height())
self.image_label.move(5+5+image.width(), 5)
# Display save
# Button will be shown when image is fully rendered
self.save_image_button.move(self.width() - self.save_image_button.width() - 5,
self.height() - self.save_image_button.height() - 5)
# Display save params
self.save_params_button.move(self.width() - self.save_image_button.width() - 5*2 - self.save_params_button.width(),
self.height() - self.save_image_button.height() - 5)
# Display evolve again
self.evolve_again_button.move(self.width() - self.save_image_button.width() - 5*3 - self.evolve_again_button.width() - self.save_params_button.width(),
self.height() - self.save_image_button.height() - 5)
# Display evolve new
self.evolve_another_button.move(self.width() - self.save_image_button.width() - 5*4 - self.evolve_again_button.width() - self.save_params_button.width() - self.evolve_another_button.width(),
self.height() - self.save_image_button.height() - 5)
self.progress_bar.setGeometry(5, self.evolve_another_button.y()+2, self.evolve_another_button.x()-10, self.evolve_another_button.height()-4)
self.progress_bar.repaint()
self.please_wait_label.move(self.evolve_another_button.x()+5, self.evolve_another_button.y()+self.evolve_another_button.height()//2-self.please_wait_label.height()//2)
self.repaint()
def update_progress(self, v):
"""
Updates progress bar
:param v Int of the progress value or Str for final text
:note If passed in value is Str it is taken as the final text (after 100 %)
"""
if type(v) == str:
self.please_wait_label.hide()
self.progress_bar.setValue(100)
self.progress_bar.setFormat(v)
self.save_image_button.show()
self.save_params_button.show()
self.evolve_again_button.show()
self.evolve_another_button.show()
else:
self.please_wait_label.show()
self.progress_bar.setValue(v)
self.save_image_button.hide()
self.save_params_button.hide()
self.evolve_again_button.hide()
self.evolve_another_button.hide()
self.progress_bar.setFormat("%p %")
self.please_wait_label.repaint()
self.progress_bar.show()
def save_image(self):
"""
Opens dialog window to save and image (path is passed to evolution_params)
"""
name, _ = QFileDialog.getSaveFileName(self, Lang.TEXT["save"][self.lang], "evolved-painting.png")
if name is not None and len(name) > 0:
self.evolution_params.curr_evolution.save_image(name)
def save_params(self):
"""
Saves current evolution parameters to a config file
"""
name, _ = QFileDialog.getSaveFileName(self, Lang.TEXT["save_params"][self.lang], "evolution-params.json")
if name is not None and len(name) > 0:
self.evolution_params.save_params(name)
def evolution_running(self):
"""
This method is called when evolution is starting to run
"""
self.button_upload_image.setEnabled(False)
self.mb_file.setEnabled(False)
self.mb_language.setEnabled(False)
self.button_about.setEnabled(False)
self.button_help.setEnabled(False)
self.speed_slider.setEnabled(False)
self.abstraction_slider.setEnabled(False)
self.show_advanced_params.setEnabled(False)
def evolve_new(self):
"""
Creates new main window
"""
MainWindow()
def evolve_again(self):
self.update_progress(0)
self.evolution_params.start_evolution_pressed()
def change_language(self, name):
"""
Changes GUIs language
:param name Abbreviated name of the language (has to be in Lang.TEXT dict)
"""
self.lang = name
# Update text
self.save_image_button.setText(Lang.TEXT["save"][self.lang])
self.save_image_button.adjustSize()
self.save_params_button.setText(Lang.TEXT["save_params"][self.lang])
self.save_params_button.adjustSize()
self.evolve_another_button.setText(Lang.TEXT["evolve_new"][self.lang])
self.evolve_another_button.adjustSize()
self.evolve_again_button.setText(Lang.TEXT["evolve_again"][self.lang])
self.evolve_again_button.adjustSize()
self.please_wait_label.setText(Lang.TEXT["please_wait"][self.lang])
self.please_wait_label.adjustSize()
self.mb_file.setTitle(Lang.TEXT["file"][self.lang])
self.mb_file.adjustSize()
self.mb_language.setTitle(Lang.TEXT["language"][self.lang])
self.button_about.setText(Lang.TEXT["about"][self.lang])
self.mb_upload_image.setText(Lang.TEXT["upload_image"][self.lang])
self.mb_evolution_params.setText(Lang.TEXT["evolution_parameters"][self.lang])
self.mb_quit.setText(Lang.TEXT["quit"][self.lang])
self.button_upload_image.setText(Lang.TEXT["upload_image"][self.lang])
self.show_advanced_params_label.setText(Lang.TEXT["show_advanced"][self.lang])
self.show_advanced_params_label.adjustSize()
self.sliders_label.setText(Lang.TEXT["sliders_title"][self.lang])
self.sliders_label.adjustSize()
self.sliders_label.move(self.width() // 2 - self.sliders_label.width() // 2, self.speed_slider.y()-60)
self.speed_slider_label2.setText(Lang.TEXT["developed"][self.lang])
self.speed_slider_label2.adjustSize()
self.speed_slider_label.setText(Lang.TEXT["rushed"][self.lang])
self.speed_slider_label.adjustSize()
self.speed_slider_label.move(self.speed_slider.x()-self.speed_slider_label.width()-10,
self.speed_slider.y()+5)
self.abstraction_slider_label2.setText(Lang.TEXT["detailed"][self.lang])
self.abstraction_slider_label2.adjustSize()
self.abstraction_slider_label.setText(Lang.TEXT["abstract"][self.lang])
self.abstraction_slider_label.adjustSize()
self.abstraction_slider_label.move(self.abstraction_slider.x()-self.abstraction_slider_label.width()-10,
self.abstraction_slider.y()+5)
self.evolution_params.hide()
self.evolution_params = EvolutionParams(self)
self.about_window.hide()
self.about_window = AboutWindow(self)
def changed_abstraction_slider(self, v):
"""
Abstraction slider event handler
:param v New value
"""
self.value_abstraction = v
self.evolution_params.quick_params_update(self.value_abstraction, self.value_speed)
def changed_speed_slider(self, v):
"""
Speed slider event handler
:param v New value
"""
self.value_speed = v
self.evolution_params.quick_params_update(self.value_abstraction, self.value_speed)
def show_about(self):
"""
Displays "About" window
"""
self.about_window.show()
def show_help(self):
"""
Opens wiki with help info in users browser
"""
webbrowser.open('https://github.com/mark-sed/abstract-evolution/wiki')
class AboutWindow(QMainWindow):
"""
Displays info about the project
"""
def __init__(self, parent=None):
"""
Constructor
:param parent Window that should be this window's parent
"""
super(AboutWindow, self).__init__(parent)
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowMinimizeButtonHint)
# Center the screen
self.setWindowTitle(Lang.TEXT["about"][parent.lang])
self.setFixedSize(400, 210)
screen = QApplication.desktop().screenNumber(QApplication.desktop().cursor().pos())
center = QApplication.desktop().screenGeometry(screen).center()
self.move(center.x() - self.width() // 2, center.y() - self.height() // 2)
self.text_browser = QTextBrowser(self)
self.text_browser.resize(self.width(), self.height())
self.text_browser.setOpenExternalLinks(True)
#self.text_browser.setObjectName("about_box")
#self.text_browser.setStyleSheet("padding: 20px; color: red")
self.text_browser.append(Lang.TEXT["about_text"][parent.lang])
self.hide()
class EvolutionParams(QMainWindow):
"""
Window containing sliders and buttons for evolution param settings
"""
def __init__(self, parent=None):
"""
Constructor
:param parent Window that should be this window's parent
"""
super(EvolutionParams, self).__init__(parent)
self.set_defaults()
self.parent = parent
self.setWindowTitle(Lang.TEXT["params_window_title"][parent.lang])
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowMinimizeButtonHint)
# Input form
self.form_layout = QFormLayout()
# Iterations
self.input_iterations = QLineEdit()
it_validator = QIntValidator()
it_validator.setBottom(1)
self.input_iterations.setValidator(it_validator)
self.input_iterations.show()
self.input_iterations.textChanged.connect(self.changed_iterations)
self.form_layout.addRow(Lang.TEXT["iterations"][parent.lang], self.input_iterations)
# Population size
self.input_population_size = QLineEdit()
ps_validator = QIntValidator()
ps_validator.setBottom(1)
self.input_population_size.setValidator(ps_validator)
self.input_population_size.show()
self.input_population_size.textChanged.connect(self.changed_population_size)
self.form_layout.addRow(Lang.TEXT["population_size"][parent.lang], self.input_population_size)
# Randomize colors
self.input_randomize_colors = QCheckBox()
self.input_randomize_colors.toggled.connect(self.changed_randomize_colors)
self.form_layout.addRow(Lang.TEXT["random_colors"][parent.lang], self.input_randomize_colors)
# Unique colors
self.input_unique_colors = QCheckBox()
self.input_unique_colors.toggled.connect(self.changed_unique_colors)
self.form_layout.addRow(Lang.TEXT["unique_colors"][parent.lang], self.input_unique_colors)
# Seeds
self.input_evolve_lines = QCheckBox()
self.input_evolve_lines.toggled.connect(self.changed_evolve_lines)
self.form_layout.addRow(Lang.TEXT["evolve_lines"][parent.lang], self.input_evolve_lines)
self.input_grow_during_evolution = QCheckBox()
self.input_grow_during_evolution.toggled.connect(self.changed_grow_during_evolution)
self.form_layout.addRow(Lang.TEXT["grow_during_evolution"][parent.lang], self.input_grow_during_evolution)
self.input_max_seeds = QLineEdit()
mxs_validator = QIntValidator()
mxs_validator.setRange(1, 1000)
self.input_max_seeds.setValidator(mxs_validator)
self.input_max_seeds.show()
self.input_max_seeds.textChanged.connect(self.changed_max_seeds)
self.form_layout.addRow(Lang.TEXT["max_seeds"][parent.lang], self.input_max_seeds)
self.input_min_seeds = QLineEdit()
ms_validator = QIntValidator()
ms_validator.setRange(0, 1000)
self.input_min_seeds.setValidator(ms_validator)
self.input_min_seeds.show()
self.input_min_seeds.textChanged.connect(self.changed_min_seeds)
self.form_layout.addRow(Lang.TEXT["min_seeds"][parent.lang], self.input_min_seeds)
self.input_max_seed_w = QLineEdit()
mwm_validator = QIntValidator()
mwm_validator.setRange(1, 10000)
self.input_max_seed_w.setValidator(mwm_validator)
self.input_max_seed_w.show()
self.input_max_seed_w.textChanged.connect(self.changed_max_seed_w)
self.form_layout.addRow(Lang.TEXT["max_seed_w"][parent.lang], self.input_max_seed_w)
self.input_min_seed_w = QLineEdit()
mw_validator = QIntValidator()
mw_validator.setRange(1, 10000)
self.input_min_seed_w.setValidator(mw_validator)
self.input_min_seed_w.show()
self.input_min_seed_w.textChanged.connect(self.changed_min_seed_w)
self.form_layout.addRow(Lang.TEXT["min_seed_w"][parent.lang], self.input_min_seed_w)
self.input_min_seed_l = QLineEdit()
ml_validator = QIntValidator()
self.input_min_seed_l.setValidator(ml_validator)
self.input_min_seed_l.show()
self.input_min_seed_l.textChanged.connect(self.changed_min_seed_l)
self.form_layout.addRow(Lang.TEXT["min_seed_l"][parent.lang], self.input_min_seed_l)
self.input_dir_change_chance = QSlider(Qt.Horizontal)
self.input_dir_change_chance.setSingleStep(1)
self.input_dir_change_chance.setMinimum(0)
self.input_dir_change_chance.setMaximum(100)
self.input_dir_change_chance.valueChanged[int].connect(self.changed_dir_change_chance)
self.input_dir_change_chance_label = QLabel(Lang.TEXT["dir_change_chance"][parent.lang]+" 5 %")
self.form_layout.addRow(self.input_dir_change_chance_label, self.input_dir_change_chance)
# Elitism
self.input_elitism = QCheckBox()
self.input_elitism.setChecked(True)
self.input_elitism.toggled.connect(self.changed_elitism)
self.form_layout.addRow(Lang.TEXT["elitism"][parent.lang], self.input_elitism)
# Crossover percentage
self.input_crossover_percentage = QSlider(Qt.Horizontal)
self.input_crossover_percentage.setSingleStep(10)
self.input_crossover_percentage.setMinimum(1)
self.input_crossover_percentage.setMaximum(100)
self.input_crossover_percentage.valueChanged[int].connect(self.changed_crossover_percentage)
self.input_crossover_percentage_label = QLabel(Lang.TEXT["crossover"][parent.lang]+" 50 %")
self.form_layout.addRow(self.input_crossover_percentage_label, self.input_crossover_percentage)
# Mutation chance
self.input_mutation_chance = QSlider(Qt.Horizontal)
self.input_mutation_chance.setSingleStep(10)
self.input_mutation_chance.setMinimum(1)
self.input_mutation_chance.setMaximum(100)
self.input_mutation_chance.valueChanged[int].connect(self.changed_mutation_chance)
self.input_mutation_chance_label = QLabel(Lang.TEXT["mutation_chance"][parent.lang]+" 5 %")
self.form_layout.addRow(self.input_mutation_chance_label, self.input_mutation_chance)
# Fitness function
self.input_fitness_fun = QComboBox()
self.input_fitness_fun.addItem(Lang.TEXT["ch"][parent.lang])
self.input_fitness_fun.addItem(Lang.TEXT["rpm"][parent.lang])
self.input_fitness_fun.addItem(Lang.TEXT["upm"][parent.lang])
self.input_fitness_fun.currentIndexChanged.connect(self.changed_fitness_fun)
self.form_layout.addRow(Lang.TEXT["fitness_function"][parent.lang], self.input_fitness_fun)
# PM values
self.input_pm_amount = QLineEdit()
pma_validator = QIntValidator()
pma_validator.setRange(1, 1000000)
self.input_pm_amount.setValidator(pma_validator)
self.input_pm_amount.textChanged.connect(self.changed_pm_amount)
self.form_layout.addRow(Lang.TEXT["amount_of_points"][parent.lang], self.input_pm_amount)
self.input_pm_amount.setEnabled(False)
self.input_pm_size = QLineEdit()
pms_validator = QIntValidator()
pms_validator.setRange(1, 100000)
self.input_pm_size.setValidator(pms_validator)
self.input_pm_size.textChanged.connect(self.changed_pm_size)
self.form_layout.addRow(Lang.TEXT["point_size"][parent.lang], self.input_pm_size)
self.input_pm_size.setEnabled(False)
# PM exact pm
self.input_exact_pm = QCheckBox()
self.input_exact_pm.toggled.connect(self.changed_exact_pm)
self.form_layout.addRow(Lang.TEXT["exact_pm"][parent.lang], self.input_exact_pm)
self.input_exact_pm.setEnabled(False)
self.input_max_color_diff = QLineEdit()
mcd_validator = QIntValidator()
mcd_validator.setRange(0, 255*2)
self.input_max_color_diff.setValidator(mcd_validator)
self.input_max_color_diff.textChanged.connect(self.changed_max_color_diff)
self.form_layout.addRow(Lang.TEXT["max_color_diff"][parent.lang], self.input_max_color_diff)
self.input_max_color_diff.setEnabled(False)
# Update frequency
self.input_update_freq = QSlider(Qt.Horizontal)
self.input_update_freq.setSingleStep(5)
self.input_update_freq.setMinimum(1)
self.input_update_freq.setMaximum(100)
self.input_update_freq.valueChanged[int].connect(self.changed_update_freq)
self.input_update_freq_label = QLabel(Lang.TEXT["update_every"][parent.lang]+" 10 %")
self.form_layout.addRow(self.input_update_freq_label, self.input_update_freq)
# Set values
self.set_input_defaults()
# Load config
self.load_button = QPushButton(Lang.TEXT["load_config"][parent.lang], self)
self.load_button.pressed.connect(self.load_params)
self.form_layout.addRow(self.load_button)
# Set defaults
self.defaults_button = QPushButton(Lang.TEXT["set_defaults"][parent.lang], self)
self.defaults_button.pressed.connect(self.button_set_defaults)
self.form_layout.addRow(self.defaults_button)
# Ok button
self.start_evolution_button = QPushButton(Lang.TEXT["start_evolution"][parent.lang], self)
self.start_evolution_button.pressed.connect(self.start_evolution_pressed)
self.start_evolution_button.setStyleSheet("font-weight: bold; height: 50px")
self.form_layout.addRow(self.start_evolution_button)
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
wid.setLayout(self.form_layout)
self.move(parent.x() + parent.width()//2 - self.width(), parent.y())
self.update()
def set_defaults(self):
"""
Sets all evolution parameters to their default values
"""
self.iterations = 300
self.update_freq = 5
self.population_size = 100
self.randomize_colors = False
self.unique_colors = False
self.crossover_percentage = 50
self.fitness_fun = 2
self.pm_amount = 200
self.pm_size = 2
self.evolve_lines = False
self.max_seeds = 25
self.min_seeds = 0
self.min_seed_w = 5
self.max_seed_w = 40
self.min_seed_l = 1