-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
executable file
·230 lines (171 loc) · 6.46 KB
/
controller.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
from flask import Flask, render_template, request, jsonify
import requests
import uuid, os
from flask_cors import CORS, cross_origin
from DAO import DAO
from model import *
from config.BaseResponse import *
import config.config as conf
from PIL import Image
import pillow_heif
from PIL import UnidentifiedImageError
import cv2, numpy as np
import json
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
cors = CORS(app, resources={r"*": {"origins": "*"}})
Dao = DAO()
CLASSIFIER_LABELS = [
"Asian",
"Beach",
"Contemporary",
"Craftsman",
"Eclectic",
"Farmhouse",
"Industrial",
"Mediterranean",
"Midcentury",
"Modern",
"Rustic",
"Scandinavian",
"Southwestern",
"Traditional",
"Transitional",
"Tropical",
"Victorian",
]
# 메인화면
@app.route('/')
def index():
return 'hello world'
'''
연결 확인 API
'''
@app.route('/test')
def testapi():
return "연결 성공"
'''
상품 조회 API
[GET] /api/products/<productIdx> :
'''
@app.route('/api/products/<productIdx>', methods=['GET'])
def getProduct(productIdx):
# req_data = request.get_json()
getProductRes = Dao.getProduct(productIdx)
if isinstance(getProductRes, BaseResponseStatus):
return jsonify(BaseResponse(None, status=getProductRes).serialize(False))
#return json.dumps(BaseResponse(getProductRes.serialize()).serialize(), ensure_ascii=False, indent=4)
return jsonify(BaseResponse(getProductRes.serialize()).serialize())
'''
이미지 업로드 API
[POST] /api/images
테스트 : curl -F "file=@test1.jpg" http://localhost:9875/api/images
'''
@app.route('/api/images', methods=['POST'])
def postImage():
if 'file' not in request.files: # REQUEST_FORM_ERROR
return jsonify(BaseResponse(None, status=BaseResponseStatus.REQUEST_FORM_ERROR).serialize(False))
# req 받아오기
file = request.files.get('file')
# uuid 생성
file_uuid = uuid.uuid4() # 파일 Uuid 변환... (확장자X) ==> 사용자마다 다른 확장파일 ... 는 jpg 로 확정.
try:
# 이미지 서버 저장 (확장자 변환)
file_type = file.filename.split('.')[-1]
if file_type.upper() == 'HEIC':
heif_file = pillow_heif.read_heif(file)
im = Image.frombytes(
heif_file.mode,
heif_file.size,
heif_file.data,
"raw",
)
else:
im = Image.open(file).convert('RGB')
file_name = f"/static/img/uploads/{file_uuid}.jpg"
#file.save(os.path.join('./UPLOAD_FOLDER', str(file_uuid)))
im.save(f".{file_name}", 'jpeg')
file_path = f"https://{conf.db['domain']}{file_name}"
postImageRes = PostImageRes(file_uuid, file_path)
return jsonify(BaseResponse(postImageRes.serialize()).serialize())
except UnidentifiedImageError as e:
print(e)
return jsonify(BaseResponse(None, status=BaseResponseStatus.REQUEST_FORM_TYPE_ERROR).serialize(False))
except Exception as e:
print(e)
return jsonify(BaseResponse(None, status=BaseResponseStatus.UNKNOWN_ERROR).serialize(False))
'''
이미지 결과 요청 API
[GET] /api/images/<uuid>
형태 : style(Style), detect(list<Detect_furniture>)
recommend_list(list<Recommend_furniture>), recommend_furniture(Recommend_furniture)
'''
@app.route('/api/images/<uuid>', methods=['GET'])
def getImage(uuid):
if 'type' not in request.args:
type_ = 0
else:
type_ = request.form.get("type")
url = "http://34.64.109.102:3000/unified"
files = {'image': open(f'./static/img/uploads/{uuid}.jpg', 'rb')}
res = requests.post(url, files=files)
res = res.json()
# 디텍션 결과
#print(f'type: {type(res)}, len: {len(res)}')
objects = res["detected_object_location"]
detect = []
input_img = Image.open(f'./static/img/uploads/{uuid}.jpg').convert("RGB")
img = np.array(input_img, dtype=np.uint8)
layers = make_bbox_images_json(objects, img)
for i, ly in enumerate(layers):
# {"label": label, "unique_id": obj_id, "img": clipped_result}
idx = ly['label']
img = ly['img']
# uuid 사용
file_uuid = uuid + str(i)
# 파일 저장
file_path = f"/static/img/detect/{file_uuid}.jpg"
cv2.imwrite(f'.{file_path}', img)
detect.append(Detect_furniture(idx, f"https://{conf.db['domain']}{file_path}"))
# 스타일
styles = res['style']
style = []
for i_, (stt) in enumerate(styles.items()):
#print(stt)
for i, st in enumerate(stt[1]):
if st != 0.0:
style.append(Style(CLASSIFIER_LABELS[i], st))
break
#print(style)
# 리커멘드
recomm = res['recom']
recommend_list = []
for i_, (stt) in enumerate(recomm.items()):
for rl in stt[1]: # productName, productPrice, productDescrip, productUrl
getprod = Dao.getProduct(rl)
# productIdx, productName, productPrice, productDescrip, productUrl, productImgs
recommend_list.append(Recommend_furniture(rl, getprod.productName, getprod.productPrice, getprod.productImgs[0], getprod.productDescrip, getprod.productUrl, ""))
break
# style: Style, detect: list[Detect_furniture], recommend_list: list[Recommend_furniture]
getImageRes = GetImageRes(style, detect, recommend_list)
return jsonify(BaseResponse(getImageRes.serialize()).serialize())
def make_bbox_images_json(detected_objects, img):
layers = []
for i_, (label, objs) in enumerate(detected_objects.items()):
objs = dict(objs)
for obj_id, segms in objs.items():
x, y, w, h = segms["bbox"]
clipped_result = np.zeros(shape=img.shape, dtype=np.uint8)
for segm in segms["segms"]:
s = np.array(
list(zip(segm["segm"]["x"], segm["segm"]["y"])), dtype=np.int32
)
mask = np.zeros_like(clipped_result)
mask = cv2.fillPoly(mask, [s], (255, 255, 255))
clipped_result = cv2.add(clipped_result, cv2.bitwise_and(img, mask))
clipped_result = clipped_result[y: y + h, x: x + w]
clipped_result = cv2.resize(clipped_result, (100, 100))
layers.append({"label": label, "unique_id": obj_id, "img": clipped_result})
return layers
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=9875)