-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
158 lines (118 loc) · 5.15 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
import cv2
import os
import uvicorn
import shutil
import datetime
from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel
from model.img_enhancement import i_enhance
from upload_img_gcs import download_gcs, upload_gcs
app = FastAPI()
DOWNLOAD_DIR = './donut-zpe-bucket-v2'
class image_info(BaseModel):
giftId: int = Form()
image: UploadFile = File(...)
@app.get("/")
async def test():
return {"message": "Hello World"}
# 모든 이미지
@app.post("/api/server/enhancement")
async def enhancement(image: UploadFile = File(...)):
print("Get Image, time : " + datetime.datetime.now())
UPLOAD_DIR = './output'
if image != None:
os.makedirs(UPLOAD_DIR, exist_ok=True) # 디렉토리 생성
local_path = os.path.normpath(os.path.join(UPLOAD_DIR, image.filename))
print("local_path")
print(local_path)
with open(local_path, 'wb') as buffer:
shutil.copyfileobj(image.file, buffer)
# 이미지 강화
enhanced_path = i_enhance(local_path)
print("Successfully enhance image, time: "+ datetime.datetime.now())
# 강화한 이미지 업로드
imgUrl = upload_gcs(enhanced_path)
print("Successfully upload image, time: "+ datetime.datetime.now())
return {"resultUrl": imgUrl}
# 버킷에서 다운로드 받고 강화
@app.post("/api/server/bucket/enhancement")
async def enhancement_bucket(request: image_info):
# 이미지 다운로드
print("다운로드 진입")
local_path = download_gcs(DOWNLOAD_DIR, request.image)
print("Successfully download image")
print(local_path)
# 이미지 강화
enhanced_path = i_enhance(local_path)
print("Successfully enhance image")
# 강화한 이미지 업로드
imgUrl = upload_gcs(enhanced_path)
print("Successfully upload image")
return {"giftId":request.giftId, "imageUrl" : imgUrl}
# actually using function
@app.post("/api/server/enhancement/optional")
async def enhancement_optional(giftId: int = Form(), image: UploadFile = File(...)):
print("Get Image, time : " + str(datetime.datetime.now()))
UPLOAD_DIR = './output'
if image != None:
os.makedirs(UPLOAD_DIR, exist_ok=True) # Create directory
local_path = os.path.normpath(os.path.join(UPLOAD_DIR, image.filename))
print("local_path")
print(local_path)
with open(local_path, 'wb') as buffer:
shutil.copyfileobj(image.file, buffer)
print("Successfully download image, time : " + str(datetime.datetime.now()))
# super resolution
enhanced_path = i_enhance(local_path)
print("Successfully enhance image, time : " + str(datetime.datetime.now()))
# upload result image to gcs
imgUrl = upload_gcs(enhanced_path)
print("Successfully upload image, time : " + str(datetime.datetime.now()))
return {"giftId":giftId, "imageUrl" : imgUrl}
# 선택된 것만 강화
@app.post("/api/server/enhancement/optional/v")
async def enhancement_optional(giftId: int = Form(), image: UploadFile = File(...)):
print("Get Image, time : " + str(datetime.datetime.now()))
UPLOAD_DIR = './output'
if image != None:
os.makedirs(UPLOAD_DIR, exist_ok=True) # 디렉토리 생성
local_path = os.path.normpath(os.path.join(UPLOAD_DIR, image.filename))
print("local_path")
print(local_path)
with open(local_path, 'wb') as buffer:
shutil.copyfileobj(image.file, buffer)
print("Successfully download image, time : " + str(datetime.datetime.now()))
#탬플릿
#barcode = cv2.imread('./templete/only_barcode.png')
# 이미지 강화
enhanced_path = i_enhance('./templete/only_barcode.png')
print("Successfully enhance image, time : " + str(datetime.datetime.now()))
# 강화한 이미지 업로드
imgUrl = upload_gcs(enhanced_path)
print("Successfully upload image, time : " + str(datetime.datetime.now()))
return {"giftId":giftId, "imageUrl" : imgUrl}
# 선택된 것만 강화
@app.post("/api/server/enhancement/optional/v")
async def enhancement_optional(giftId: int = Form(), image: UploadFile = File(...)):
print("Get Image, time : " + str(datetime.datetime.now()))
UPLOAD_DIR = './output'
if image != None:
os.makedirs(UPLOAD_DIR, exist_ok=True) # 디렉토리 생성
local_path = os.path.normpath(os.path.join(UPLOAD_DIR, image.filename))
print("local_path")
print(local_path)
with open(local_path, 'wb') as buffer:
shutil.copyfileobj(image.file, buffer)
print("Successfully download image, time : " + str(datetime.datetime.now()))
#탬플릿
#barcode = cv2.imread('./templete/only_barcode.png')
# 이미지 강화
enhanced_path = i_enhance('./templete/only_barcode.png')
print("Successfully enhance image, time : " + str(datetime.datetime.now()))
# 강화한 이미지 업로드
imgUrl = upload_gcs(enhanced_path)
print("Successfully upload image, time : " + str(datetime.datetime.now()))
return {"giftId":giftId, "imageUrl" : imgUrl}
if __name__ == '__main__':
app_str = 'app:app'
uvicorn.run(app_str, host='0.0.0.0', port=8000, reload=True, workers=1)