-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
248 lines (208 loc) · 8.22 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
import os #디렉토리 절대 경로
import shutil
from flask import Flask, render_template, jsonify, request, redirect
from werkzeug.utils import secure_filename
#로그인, 회원가입 관련
from flask_sqlalchemy import SQLAlchemy
from models import db
from models import Fcuser
from flask import session #세션
from flask_wtf.csrf import CSRFProtect
from forms import ChangePWForm, RegisterForm, LoginForm, ChangeProfile
#StarGAN
from StarGAN import model
from StarGAN import main as mainPy
from StarGAN import solver
from WhiteBox.test_code import cartoonize as mainPy4
from OpenCV import main as mainPy2
from CartoonGan import main as mainPy3
app = Flask(__name__)
## HTML을 주는 부분
@app.route('/')
def MainSplash():
celeba_image_dir = 'static/images/input'
result_dir = 'static/images/result'
if os.path.exists(celeba_image_dir) :
shutil.rmtree(celeba_image_dir)
if os.path.exists(result_dir) :
shutil.rmtree(result_dir)
return render_template('MainSplash.html')
@app.route('/main')
def main():
userid = session.get('userid', None)
username = session.get('username')
return render_template('main.html', userid=userid, username=username)
@app.route('/changePW', methods = ['GET', 'POST'])
def changePW() :
form = ChangePWForm()
if form.validate_on_submit() :
userid = session.get('userid', None)
fcuser = Fcuser()
user = fcuser.query.filter_by(userid=userid).first()
user.password = form.data.get('password')
db.session.commit()
return redirect('/Settings')
return render_template('changePW.html', form=form)
@app.route('/forgotId')
def forgotId():
return render_template('forgotID.html')
@app.route('/forgotPW')
def forgotPW():
return render_template('forgotPW.html')
@app.route('/login', methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
session['userid'] = form.data.get('userid')
session['username'] = form.data.get('username')
return redirect('/main')
return render_template('login.html', form=form)
@app.route('/logout', methods=['GET'])
def logout():
session.pop('userid', None)
return redirect('/main')
#회원가입
@app.route('/signup', methods = ['GET', 'POST'])
def signup():
form = RegisterForm()
if form.validate_on_submit() :
fcuser = Fcuser()
fcuser.userid = form.data.get('userid')
fcuser.username = form.data.get('username')
fcuser.password = form.data.get('password')
fcuser.stateM = "상태메세지를 입력해주세요"
fcuser.profileIMG = "images/profile.jpg"
db.session.add(fcuser)
db.session.commit()
return redirect('/main')
return render_template('signup.html', form=form)
@app.route('/balloon_example')
def balloon_example():
return render_template('BalloonExample.html')
@app.route('/option_hair')
def option_hair():
return render_template('option_hair.html')
@app.route('/option_cartoon', methods =['GET', 'POST'])
def option_cartoon():
if request.method == 'GET':
value = request.args.get('decodename')
value = str(value)
if value=="1" :
os.rename("static/images/result/imgBlack.jpg", "static/images/input/imageTemp1.jpg")
elif value=="2" :
os.rename("static/images/result/imgBlond.jpg", "static/images/input/imageTemp1.jpg")
elif value=="3" :
os.rename("static/images/result/imgBrown.jpg", "static/images/input/imageTemp1.jpg")
return render_template('option_cartoon.html', decodename = value)
@app.route('/select_image')
def select_image():
celeba_image_dir = 'static/images/input'
result_dir = 'static/images/result'
if os.path.exists(celeba_image_dir) :
shutil.rmtree(celeba_image_dir)
if os.path.exists(result_dir) :
shutil.rmtree(result_dir)
return render_template('select_image.html')
@app.route('/select_image2')
def select_image2():
celeba_image_dir = 'static/images/input'
result_dir = 'static/images/result'
if os.path.exists(celeba_image_dir) :
shutil.rmtree(celeba_image_dir)
if os.path.exists(result_dir) :
shutil.rmtree(result_dir)
return render_template('select_image2.html')
@app.route('/Settings')
def Settings():
return render_template('Settings.html')
@app.route('/camera2')
def camera2():
return render_template('camera2.html')
@app.route('/camera')
def camera():
return render_template('camera.html')
@app.route('/ViewProfile')
def ViewProfile():
fcuser = Fcuser()
userid = session.get('userid', None)
user = fcuser.query.filter_by(userid=userid).first()
username = user.username
stateM = user.stateM
profileIMG=user.profileIMG
return render_template('ViewProfile.html', userid=userid, username=username, stateM=stateM, profileIMG=profileIMG)
@app.route('/changeProfile', methods = ['GET', 'POST'])
def changeProfile():
form = ChangeProfile()
fcuser = Fcuser()
userid = session.get('userid', None)
user = fcuser.query.filter_by(userid=userid).first()
profileIMG = user.profileIMG
if form.validate_on_submit():
user.username = form.data.get('username')
user.stateM = form.data.get('stateM')
db.session.commit()
return redirect('/ViewProfile')
return render_template('changeProfile.html', form=form, profileIMG=profileIMG)
@app.route('/result')
def result():
return render_template('result.html')
@app.route('/run_model')
def run_model():
mainPy.main()
if(os.path.exists("static/images/completeImage.png")):
os.rename("static/images/completeImage.png", "static/images/cmImage.png")
return render_template('result.html')
@app.route('/result_download')
def result_download():
download_dir = "static/images/download"
if(os.path.exists("static/images/cmImage.png")):
os.rename("static/images/cmImage.png", "static/images/completeImage.png")
if not os.path.exists(download_dir):
os.makedirs(download_dir)
while(True) :
if(os.path.exists("static/images/input/ResultImage.png")) : break
shutil.copy("static/images/input/ResultImage.png", "static/images/download/ResultImage.png")
return render_template('result.html')
@app.route('/result_download2')
def result_download2():
download_dir = "static/images/download"
if(os.path.exists("static/images/cmImage.png")):
os.rename("static/images/cmImage.png", "static/images/completeImage.png")
if not os.path.exists(download_dir):
os.makedirs(download_dir)
while(True) :
if(os.path.exists("static/images/input/ResultImage.png")) : break
shutil.copy("static/images/input/ResultImage.png", "static/images/download/ResultImage.png")
return render_template('result2.html')
@app.route("/result2")
def result2():
return render_template('result2.html')
@app.route('/run_model2')
def run_model2():
if(os.path.exists("static/images/completeImage.png")):
os.rename("static/images/completeImage.png", "static/images/cmImage.png")
# option_cartoon.html 에서 불러온 option 값
option = request.args.get("option")
if option=="0":
mainPy4.cartoonize()
elif option=="1":
mainPy2.main()
else:
mainPy3.main()
return render_template('result2.html')
## 웹에서 사용할 APIs
## 서버 연결
if __name__ == '__main__':
#데이터베이스
basedir = os.path.abspath(os.path.dirname(__file__)) #현재 파일이 있는 디렉토리 절대 경로
dbfile = os.path.join(basedir, 'db.sqlite') #데이터베이스 파일을 만든다
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + dbfile
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True #사용자에게 정보 전달완료하면 teadown. 그 때마다 커밋=DB반영
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False #추가 메모리를 사용하므로 꺼둔다
app.config['SECRET_KEY'] = '123'
csrf = CSRFProtect()
csrf.init_app(app)
db.init_app(app) #app설정값 초기화
db.app = app #Models.py에서 db를 가져와서 db.app에 app을 명시적으로 넣는다
db.create_all() #DB생성
app.run('0.0.0.0',port=5002,debug=True)