forked from OverStruck/deep-dream-maker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeepDreamMaker.py
executable file
·126 lines (106 loc) · 3.83 KB
/
DeepDreamMaker.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
#!/usr/bin/env python
import sys
#import DeepDreamThread
import DeepDreamThread
from PyQt4 import QtGui, QtCore, uic
class Window(QtGui.QMainWindow):
inputImage = "" # input image path
outputLoc = "" # output image path
ddArgs = {
"preview-mode": True # chkPreviewMode
}
def __init__(self):
super(Window, self).__init__()
# gui.ui made with qt designer
self.ui = uic.loadUi("gui.ui", self)
# menu bar actions
self.ui.actionAbout.triggered.connect(self.aboutMsg)
self.ui.actionExit.triggered.connect(self.closeApp)
# butons
self.ui.btnInputFile.clicked.connect(self.openFile)
self.ui.btnSetOutputLoc.clicked.connect(self.setOutputLoc)
self.ui.btnMakeitDream.clicked.connect(self.makeItDream)
self.ui.btnStopDream.clicked.connect(self.stopDream)
self.ui.show()
def openFile(self):
"""
Opens a file a file dialog to pick the input image
A filter to only allow .png and .jpg images is used
TODO: check file actually exits?
"""
self.inputImage = QtGui.QFileDialog.getOpenFileName(self, "Open File", "", "Images (*.png *.jpg)")
if self.inputImage == "":
self.ui.lblInputImg.setText("No image selected")
else:
self.ui.lblInputImg.setText(self.inputImage)
self.enableMakeitDream()
def setOutputLoc(self):
"""
Opens a file a file dialog to pick the DIRECTORY/FOLDER
where the proccesed image is to be saved.
TODO: check directory actually exits?
"""
self.outputLoc = QtGui.QFileDialog.getExistingDirectory(self, 'Select a folder:', '', QtGui.QFileDialog.ShowDirsOnly)
if self.outputLoc == "":
self.ui.lblOutputLoc.setText("No folder selected")
else:
self.ui.lblOutputLoc.setText(self.outputLoc)
self.enableMakeitDream()
def updateConsole(self, outputTxt):
"""
Updates the "console" (QPlainTextEdit) contents
"""
self.ui.consoleContainer.appendPlainText(outputTxt)
def aboutMsg(self):
"""
Display a message box showing information about this application
when the user clicks the "about" menu btn
"""
msgBoxTxt = "Deep Dream Maker v0.1\n\n"
msgBoxTxt += "This application allows you to use Google's Deep Dream python script\n"
msgBoxTxt += "without needed to use a command line\n\n"
msgBoxTxt += "This is an open source project maintained by @overstruck\n\n"
msgBoxTxt += "Repository: https://github.com/OverStruck/deep-dream-maker"
msgBox = QtGui.QMessageBox()
msgBox.setText(msgBoxTxt)
msgBox.exec_()
def closeApp(self):
sys.exit()
def enableMakeitDream(self):
"""
Enables the main button that initializes deep dream
only if we have a valid input image path and an output directory
"""
if self.inputImage != "" and self.outputLoc !="":
self.ui.btnMakeitDream.setEnabled(True)
else:
self.ui.btnMakeitDream.setDisabled(True)
def stopDream(self):
"""
kills thread running current dream. Disables btnStopDream. Enables btnMakeitDream
"""
self.deepDreamThread.killSubProcess()
self.ui.btnMakeitDream.setEnabled(True)
self.ui.btnStopDream.setDisabled(True)
def makeItDream(self):
"""
Main function that calls a thread class which calls Google's deep dream method
We use a thread so that our user interface is not frozen and so that we can
update our console with information about the current proccess
"""
# disable make it dream btn
self.ui.btnMakeitDream.setDisabled(True)
# enable stop dream btn to kill process
self.ui.btnStopDream.setEnabled(True)
# need to convert from Qstring to a normal string
inputImg = str(self.inputImage)
outputLoc = str(self.outputLoc)
self.deepDreamThread = DeepDreamThread.DeepDreamThread(self, inputImg, outputLoc, self.ddArgs)
# connect signal to slot to listen for changes and update cosole contents
self.deepDreamThread.consoleUpdated.connect(self.updateConsole)
self.deepDreamThread.start()
def main():
app = QtGui.QApplication([])
Window()
sys.exit(app.exec_())
main()