-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
235 lines (181 loc) ยท 7.83 KB
/
app.py
File metadata and controls
235 lines (181 loc) ยท 7.83 KB
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
from flask import Flask, request, jsonify
from flask_cors import CORS
from werkzeug.utils import secure_filename
import tensorflow as tf
from keras.models import load_model
import cv2
import os
import glob
import numpy as np
from DB import Database
from ColorExt import ColorExt
from WorkProcess import WorkProcess
from MetaImage import MetaImage
import json
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'img/'
CORS(app, resources={r"/api/*": {"origins": "*"}})
# TODO : ์นดํ
๊ณ ๋ฆฌ ํ์ผ db ์ฐ๊ฒฐํ๊ธฐ
file = open('category.text', 'r', encoding='UTF8')
class_name = [f.strip('\n') for f in file.readlines()]
file.close()
image_meta = {}
my_database_class = Database()
# ์ด๋ฏธ์ง ํ์ฉ๊ฐ๋ฅํ ํ์ฅ์
img_ext = ['jpg', 'jpeg', 'JPG', 'png', 'bmp']
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ํจ์
def image_classification(image_list):
image = []
image_predict = []
for i in range(len(image_list)):
img = cv2.imread(image_list[i], cv2.IMREAD_COLOR)
img = cv2.resize(img, dsize=(224, 224))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255.0
image.append(img)
# ๋ชจ๋ธ load
model = load_model(
'cats_and_dogs_filtered_Xception_Colab.h5', compile=False)
pred = model.predict(np.array(image))
# ์์ธก . ์ผ์น์จ% ํํ๋ก ์ถ๋ ฅ
for i in range(len(pred)):
prediction = str(class_name[np.argmax(pred[i])])
probility = '{0:0.2f}'.format(100*max(pred[i]))
# r['prediction'] = prediction
# r["probility"] = probility
image_predict.append(prediction)
return image_predict
# mysql ์ฟผ๋ฆฌ์ ์ด๋ฏธ์ง ์ ๋ณด ์ ์ฅ
def save_db(uuid, result, gallery_yn):
if "datetime" not in result:
sql = "INSERT INTO capstonedb.ImageInfo(uid,image_url,image_location,image_width,image_height,wallpaper_yn,gallery_yn) \
VALUES('%s','%s','%s','%d','%d','%s','%c')" % (uuid, result['remote'], result['address'], result['width'], result['height'], result['wallpaper'], gallery_yn)
elif "address" not in result:
sql = "INSERT INTO capstonedb.ImageInfo(uid,image_url,image_date,image_width,image_height,wallpaper_yn,gallery_yn) \
VALUES('%s','%s','%s','%d','%d','%s','%c')" % (uuid, result['remote'], result['datetime'], result['width'], result['height'], result['wallpaper'], gallery_yn)
elif "address" not in result and "datetime" not in result:
sql = "INSERT INTO capstonedb.ImageInfo(uid,image_url,image_width,image_height,wallpaper_yn,gallery_yn) \
VALUES('%s','%s','%d','%d','%s','%c')" % (uuid, result['remote'], result['width'], result['height'], result['wallpaper'], gallery_yn)
else:
sql = "INSERT INTO capstonedb.ImageInfo(uid,image_url,image_date,image_location,image_width,image_height,wallpaper_yn,gallery_yn) \
VALUES('%s','%s','%s','%s','%d','%d','%s','%c')" % (uuid, result['remote'], result['datetime'], result['address'], result['width'], result['height'], result['wallpaper'], gallery_yn)
print(sql)
my_database_class.execute(sql)
my_database_class.commit()
# db์ ์นดํ
๊ณ ๋ฆฌ
def save_category(image_id, result):
print(result)
category = "INSERT INTO capstonedb.ImageCategory(category_name,image_id) VALUES('%s','%d')" % (
result, int(image_id))
my_database_class.execute(category)
my_database_class.commit()
# db์ ์ปฌ๋ฌ ์ด๋ฏธ์ง ์ ์ฅ
def save_color(image_id, result):
param_list = []
for p in result:
item = [image_id, p['r'], p['g'], p['b'], p['type']]
t = tuple(item)
param_list.append(t)
print(param_list)
sql = "INSERT INTO `capstonedb`.`Palette` VALUES(%s,%s,%s,%s,%s)"
my_database_class.executeMany(sql, param_list)
my_database_class.commit()
# image_id ๋ฐํ
def get_image_id(image_url):
sql = "SELECT id FROM ImageInfo WHERE image_url='%s'" % (image_url)
print(sql)
image_id = my_database_class.executeOne(sql)
return image_id['id']
# ๋ฐฐ๊ฒฝํ๋ฉด ์ถ์ฒ ๋ก์ง
def get_aspect_ratio(width, height, category_list):
category = category_list[0]
minRatio = 1.6
maxRatio = 1.9
ratio = round(width / height, 3)
if ratio >= minRatio and ratio <= maxRatio and width >= 800 and (category == "๋๋ฌผ" or category == "ํ๊ฒฝ"):
return 'Y'
else:
return 'N'
# ํ์ผ ์
๋ก๋ ํ ์นดํ
๊ณ ๋ฆฌ json๋ก ๋ฆฌํด
@app.route('/api/image_upload', methods=['POST'])
def image_upload():
print('ํ์ผ ์
๋ก๋ start')
# ํ์ผ ์
๋ก๋ ํ
uuid = request.form['uid']
file_list = request.files.getlist("file_list")
# Y : gallery์ ์ฌ๋ฆฌ๋ ์ด๋ฏธ์ง , N : gallery์ ์ฌ๋ฆฌ์ง ์๋ ์ด๋ฏธ์ง
gallery_yn = request.form['gallery_yn']
image_list = []
result = []
print(file_list)
for file in file_list:
ext = file.filename.split('.')[1]
if ext not in img_ext:
return {'code': '402', 'message': 'error', 'result': '์ง์ํ์ง ์๋ ์ด๋ฏธ์ง ํ์ฅ์ ์
๋๋ค.\njpg, png, bmp ํ์ฅ์๋ง ์ง์ํฉ๋๋ค'}
filename = os.path.join(
app.config['UPLOAD_FOLDER'], file.filename)
print(filename)
file.save(filename)
image_list.append(filename)
# ํ๋ฒ์ ์ด๋ฏธ์ง ์
๋ก๋ ํ ๊ฒฐ๊ณผ๊ฐ ๋ฆฌํด
data = WorkProcess().multi_upload(image_list)
print(data)
if "error image upload" in data:
return {'code': '401', 'message': 'error', 'result': '์๋ฒ์ ์ด๋ฏธ์ง ์
๋ก๋๋ฅผ ์คํจํ์ต๋๋ค.'}
image_list = []
image_class = []
for i in range(len(data)):
print(data[i])
data_dic = data[i]
image_name = []
print(data_dic.get('image_name'))
# ๋ฉํ๋ฐ์ดํฐ ์ถ์ถ
image_meta = MetaImage(data_dic.get('image_name')).get_meta_info()
print(image_meta)
if image_meta is not None:
data_dic['width'] = image_meta["width"]
data_dic['height'] = image_meta["height"]
data_dic['datetime'] = image_meta["datetime"]
if "address" in image_meta:
data_dic['address'] = image_meta["address"]
# ์ด๋ฏธ์ง ์์ ์ถ์ถ
color_ext = ColorExt(data_dic.get('image_name'))
data_dic['color'] = color_ext.get_color(5)
# ๋ฐฐ๊ฒฝํ๋ฉด ์ถ์ฒ
image_name.append(data_dic.get('image_name'))
category = image_classification(image_name)
data_dic['wallpaper'] = get_aspect_ratio(
data_dic['width'], data_dic['height'], category)
# ๋์
๋๋ฆฌ ์ถ๊ฐ
image_list.append(data_dic.get('image_name'))
result.append(data_dic)
# ์ด๋ฏธ์ง ์นดํ
๊ณ ๋ฆฌ ๋ถ๋ฅ
image_class = image_classification(image_list)
for i in range(len(result)):
# db ์ ์ฅ
save_db(uuid, result[i], gallery_yn)
# image_url๋ก image_id ๋ฐํ
image_id = get_image_id(result[i].get('remote'))
# ์์ ์ ์ฅ
save_color(image_id, result[i].get('color'))
# ์นดํ
๊ณ ๋ฆฌ ์ ์ฅ
save_category(image_id, image_class[i])
# ๋ถ์ ๋๋ ์ด๋ฏธ์ง ์ญ์
try:
os.remove(result[i].get('image_name'))
except:
result_msg = {'code': '403', 'message': '',
'result': '์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.'}
json.dumps(result_msg)
result1 = {'code': '201', 'message': '', 'result': '์ด๋ฏธ์ง ๋ฑ๋ก ์๋ฃ'}
return json.dumps(result1)
@app.route('/api/image_Remove', methods=['POST'])
def image_remove():
url_list = request.json['img_url']
result = WorkProcess().remove(url_list)
if "error image upload" in result:
return {'code': '401', 'message': 'error', 'result': '์๋ฒ์ ์ด๋ฏธ์ง ์
๋ก๋๋ฅผ ์คํจํ์ต๋๋ค.'}
else:
return {'code': '201', 'message': '', 'result': 'cloudinary ์ญ์ ์๋ฃ'}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8082, debug=True)