-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfast_api.py
More file actions
273 lines (243 loc) · 9.26 KB
/
fast_api.py
File metadata and controls
273 lines (243 loc) · 9.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# import sys
#
# sys.path.insert(0, '/home/long/Desktop/IDCardDetectionandRecognition')
# import os
# import cv2
# import numpy as np
# import uvicorn
# from typing import Optional
# from io import BytesIO
# from PIL import Image
# from PIL.Image import Image
# from fastapi import FastAPI
# from pydantic import BaseModel
# from fastapi import APIRouter, UploadFile, HTTPException
# from main import predict
#
# app = FastAPI()
# # yolo_router = APIRouter()
# DETECTION_URL = '/id-card-yolo/detect'
#
#
# class predictYOLO(BaseModel):
# results: dict
#
#
# # @app.post(DETECTION_URL)
# # async def detect(image: UploadFile, response_model: predictYOLO):
# # if image.filename.split('.')[-1] in ("jpg", "jpeg", "png"):
# # pass
# # else:
# # raise HTTPException(status_code=415, detail="Item not found")
# # response_model = predict(image)
# # return response_model
#
#
# @app.get('/hello-world')
# def hello():
# return {"hello": 'Long'}
#
import uvicorn
import uuid
import cv2
import io
import base64
import imagezmq
from fastapi import FastAPI, HTTPException, UploadFile, APIRouter, File, Form, Body, Query, Depends
from pydantic import BaseModel
import os
from PIL import Image
import argparse
from starlette.responses import StreamingResponse
from fastapi.templating import Jinja2Templates
from fastapi.responses import FileResponse, StreamingResponse, HTMLResponse
from typing import List, Union, Optional
from main import predict_yolov7, predict_yolov5
import numpy as np
from pathlib import Path
import sys
import socket
from starlette.responses import RedirectResponse
app_desc = """<h2>Made by`Pham Minh Long`</h2>"""
app = FastAPI(title="Chúa tể phát hiện cccd/cmnd", description=app_desc)
DETECTION_URL = '/id-card-yolo/detect/'
LABEL_STUDIO = '/id-card-yolo/detect_label_studio/'
hostname = socket.gethostname()
IP_ADDRESS = socket.gethostbyname(hostname)
# ROOT = str(Path.home())
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
def parse_arg():
parser = argparse.ArgumentParser()
parser.add_argument('--local_host', type=str, help='your local host connection', default=IP_ADDRESS)
parser.add_argument('--weights', type=str, help='initial weights path', default='weights/yolov7x/yolov7x.pt')
parser.add_argument('--port', type=int, help='your port connection', default=8000)
parser.add_argument('--folder_save_rotation', type=str,
default=str(ROOT) + '/results/correct',
required=False)
parser.add_argument('--folder_save_detection', type=str,
default=str(ROOT) + '/results/detect',
required=False)
parser.add_argument('--folder_save_polygon', type=str,
default=str(ROOT / 'results/polygon'),
required=False)
return parser.parse_args()
class choice(BaseModel):
option: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: List[str] = []
# class option(BaseModel):
# option: str
def base64str_to_PILImage(predictedImage):
base64_img_bytes = base64.b64encode(predictedImage).decode('utf-8')
base64bytes = base64.b64decode(base64_img_bytes)
bytesObj = io.BytesIO(base64bytes)
img = Image.open(bytesObj)
return img
# @app.get("/", include_in_schema=False)
# async def index():
# return RedirectResponse(url="/docs")
def image_to_base64(image):
"""
It takes an image, encodes it as a jpg, and then encodes it as base64
:param image: The image to be converted to base64
:return: A base64 encoded string of the image.
"""
_, buffer = cv2.imencode('.jpg', image)
img_data = base64.b64encode(buffer)
return img_data
@app.post(DETECTION_URL)
async def detect(image: UploadFile = File(...), option: str = None):
if image.filename.split('.')[-1] in ("jpg", "jpeg", "png"):
pass
else:
raise HTTPException(status_code=415, detail="Item not found")
# with open(fileName, 'rb') as f:
# encoded_image = base64.b64encode(f.read())
# decoded_image = encoded_image.decode('utf-8')
args = parse_arg()
contents = await image.read()
array = np.fromstring(contents, np.uint8)
img = cv2.imdecode(array, cv2.IMREAD_COLOR)
coordinateBoundingBox, coordinatePolygon, predictedImage = predict_yolov7(img, image.filename, args)
# res, im_png = cv2.imencode(".png", predictedImage)
# predictedImage = base64str_to_PILImage(predictedImage)
# buffered = BytesIO()
# Image.fromarray(predictedImage).save(buffered, format="JPEG")
# img_str = base64.b64encode(buffered.getvalue())
encoded_string = image_to_base64(predictedImage)
# img_decode = BytesIO(base64.b64decode(img_str))
# print(predictedImage)
# dataStr = json.dumps(predictedImage)
#
# base64EncodedStr = base64.b64encode(dataStr.encode('utf-8'))
# print(base64EncodedStr)
#
# print('decoded', base64.b64decode(base64EncodedStr))
if option == 'Y' or option == 'y':
return {'image_name': image.filename}, coordinateBoundingBox, {'polygon_coordinates': coordinatePolygon}, {
"encoded_image": encoded_string}
else:
return {'image_name': image.filename}, coordinateBoundingBox, {'polygon_coordinates': coordinatePolygon}
@app.post(LABEL_STUDIO)
async def detect_label_studio(image: UploadFile = File(...), option: str = None):
if image.filename.split('.')[-1] in ("jpg", "jpeg", "png"):
pass
else:
raise HTTPException(status_code=415, detail="Item not found")
# with open(fileName, 'rb') as f:
# encoded_image = base64.b64encode(f.read())
# decoded_image = encoded_image.decode('utf-8')
args = parse_arg()
contents = await image.read()
array = np.fromstring(contents, np.uint8)
img = cv2.imdecode(array, cv2.IMREAD_COLOR)
coordinatePolygon, predictedImage = predict_yolov5(img, image.filename, args)
# res, im_png = cv2.imencode(".png", predictedImage)
# predictedImage = base64str_to_PILImage(predictedImage)
# buffered = BytesIO()
# Image.fromarray(predictedImage).save(buffered, format="JPEG")
# img_str = base64.b64encode(buffered.getvalue())
# encoded_string = image_to_base64(predictedImage)
# img_decode = BytesIO(base64.b64decode(img_str))
# print(predictedImage)
# dataStr = json.dumps(predictedImage)
#
# base64EncodedStr = base64.b64encode(dataStr.encode('utf-8'))
# print(base64EncodedStr)
#
# print('decoded', base64.b64decode(base64EncodedStr))
return {
'data': {'image': '/data/local-files/?d=/home/long/Downloads/datasets/augment_padding/' + image.filename},
"annotations": [
{
"result": [
{
"original_width": img.shape[1],
"original_height": img.shape[0],
"image_rotation": 0,
"value": {
"points": coordinatePolygon,
"polygonlabels": [
"top-cmnd"
]
},
"id": "796e373c3a",
"from_name": "label",
"to_name": "image",
"type": "polygonlabels",
"origin": "manual"
}
]
}
],
"predictions": []}
# @app.post('/yolo-id-card/request/')
# async def detect(data: str):
# image = base64.b64decode(data)
# coordinateBoundingBox, coordinatePolygon, predictedImage = predict_yolov7(image)
# encoded_string = image_to_base64(predictedImage)
# return coordinateBoundingBox, {'polygon_coordinates': coordinatePolygon}, {"encoded_image": encoded_string}
# @app.post("/files/")
# async def create_files(files: List[bytes] = File(description="Multiple files as bytes"),
# ):
# return {"file_sizes": [len(file) for file in files]}
# @app.post("/uploadFiles/")
# async def create_upload_files(
# files: List[UploadFile] = File(description="Multiple files as UploadFile"),
# ):
# return {"filenames": [file.filename for file in files]}
# @app.get("/")
# async def main():
# content = """
# <body>
# <form action="/files/" enctype="multipart/form-data" method="post">
# <input name="files" type="file" multiple>
# <input type="submit">
# </form>
# <form action="/uploadFiles/" enctype="multipart/form-data" method="post">
# <input name="files" type="file" multiple>
# <input type="submit">
# </form>
# </body>
# """
# return HTMLResponse(content=content)
@app.get('/')
async def read():
return {"message": 'chào mừng đến với bình nguyên vô tận'}
# @app.get("/myapp/v1/filter/a")
# async def style_transfer(data: dict):
# image_byte = data.get('image').encode()
# image_shape = tuple(data.get('shape'))
# image_array = np.frombuffer(base64.b64decode(image_byte)).reshape(image_shape)
if __name__ == '__main__':
args = parse_arg()
app_str = 'fast_api:app'
uvicorn.run(app_str, host=args.local_host, port=args.port, reload=True, workers=1)