-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (94 loc) · 3.26 KB
/
main.py
File metadata and controls
112 lines (94 loc) · 3.26 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
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from PIL import Image
from modules import box
import json
import numpy as np
app = FastAPI()
@app.post("/api/test")
async def get_image_size_test(image: UploadFile = File(...)):
try:
image = Image.open(image.file) # 이미지 객체 받아오기
except Exception:
return ErrorResponse(400, "이미지 처리를 할 수 없는 이미지입니다.")
try:
rvec, dist, fx, fy, cx, cy = box.calculate_camera_parameters(image)
except Exception:
return ErrorResponse(500, "인식되지않았습니다. 다시 시도해주세요.")
params = {
"rvec": rvec.tolist(),
"dist": dist.tolist(),
"fx": fx,
"fy": fy,
"cx": cx,
"cy": cy
}
# print(json.dumps(params))
# rvec, dist, fx, fy, cx, cy
return {
"status": 200,
"response": {
"params" : params
},
"errorMessage": None
}
def ErrorResponse(status, message):
return JSONResponse(status_code=status, content=
{
"status": status,
"response": None,
"errorMessage": message
})
@app.post("/api/analyze-1")
async def get_image_size1(image: UploadFile = File(...), params : str = File(...)):
try:
image = Image.open(image.file) # 이미지 객체 받아오기
except Exception:
return ErrorResponse(400, "이미지 처리를 할 수 없는 이미지입니다.")
try:
params_dict = json.loads(params)
params_list = [np.array(params_dict["rvec"]), np.array(params_dict["dist"]), params_dict["fx"], params_dict["fy"], params_dict["cx"], params_dict["cy"]]
except Exception:
return ErrorResponse(400, "/api/test 결과의 params를 그대로 돌려주세요")
try:
width, height, tall = box.calculate_box_size(image, params_list)
except Exception:
width, height, tall = 0, 0, 0
return {
"status": 200,
"response": {
"width": width,
"height": height,
"tall": tall
},
"errorMessage": None
}
@app.post("/api/analyze-2")
async def get_image_size2(image: UploadFile = File(...)):
try:
image = Image.open(image.file) # 이미지 객체 받아오기
except Exception:
return ErrorResponse(400, "잘못된 이미지입니다.")
try:
width, height, tall = box.calculate_box_size(image)
except Exception:
width, height, tall = 0, 0, 0
return {
"status": 200,
"response": {
"width": width,
"height": height,
"tall": tall
},
"errorMessage": None
}
def ErrorResponse(status, message):
return JSONResponse(status_code=status, content=
{
"status": status,
"response": None,
"errorMessage": message
})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)