thanhnv2323 commited on
Commit
d454bc0
1 Parent(s): 93f1618

update: exe time

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -1,5 +1,7 @@
1
  ####################################### IMPORT #################################
2
  import json
 
 
3
  import pandas as pd
4
  from PIL import Image
5
  from loguru import logger
@@ -12,7 +14,6 @@ from fastapi.middleware.cors import CORSMiddleware
12
  from fastapi.exceptions import HTTPException
13
  import uvicorn
14
 
15
-
16
  from io import BytesIO
17
 
18
  from utils import get_image_from_bytes, detect_sample_model_origin
@@ -20,6 +21,7 @@ from utils import detect_sample_model
20
  from utils import add_bboxs_on_img
21
  from utils import get_bytes_from_image
22
  import gradio as gr
 
23
  ####################################### logger #################################
24
 
25
  logger.remove()
@@ -59,6 +61,7 @@ app.add_middleware(
59
  allow_headers=["*"],
60
  )
61
 
 
62
  @app.on_event("startup")
63
  def save_openapi_json():
64
  '''This function is used to save the OpenAPI documentation
@@ -73,6 +76,7 @@ def save_openapi_json():
73
  with open("openapi.json", "w") as file:
74
  json.dump(openapi_data, file)
75
 
 
76
  # redirect
77
  @app.get("/", include_in_schema=False)
78
  async def redirect():
@@ -97,7 +101,7 @@ def perform_healthcheck():
97
 
98
  ######################### Support Func #################################
99
 
100
- def crop_image_by_predict(image: Image, predict: pd.DataFrame(), crop_class_name: str,) -> Image:
101
  """Crop an image based on the detection of a certain object in the image.
102
 
103
  Args:
@@ -117,10 +121,10 @@ def crop_image_by_predict(image: Image, predict: pd.DataFrame(), crop_class_name
117
  if len(crop_predicts) > 1:
118
  crop_predicts = crop_predicts.sort_values(by=['confidence'], ascending=False)
119
 
120
- crop_bbox = crop_predicts[['xmin', 'ymin', 'xmax','ymax']].iloc[0].values
121
  # crop
122
  img_crop = image.crop(crop_bbox)
123
- return(img_crop)
124
 
125
 
126
  ######################### MAIN Func #################################
@@ -136,6 +140,7 @@ def img_object_detection_to_json(file: bytes = File(...)):
136
  Returns:
137
  dict: JSON format containing the Objects Detections.
138
  """
 
139
  # Step 1: Initialize the result dictionary with None values
140
  # result={'detect_objects': None}
141
 
@@ -170,9 +175,13 @@ def img_object_detection_to_json(file: bytes = File(...)):
170
  }
171
 
172
  # Step 5: Logs and return
173
- logger.info("results: {}", results_json)
 
 
 
174
  return results_json
175
 
 
176
  @app.post("/img_object_detection_to_img")
177
  def img_object_detection_to_img(file: bytes = File(...)):
178
  """
@@ -190,11 +199,11 @@ def img_object_detection_to_img(file: bytes = File(...)):
190
  predict = detect_sample_model(input_image)
191
 
192
  # add bbox on image
193
- final_image = add_bboxs_on_img(image = input_image, predict = predict)
194
 
195
  # return image in bytes format
196
  return StreamingResponse(content=get_bytes_from_image(final_image), media_type="image/jpeg")
197
 
198
 
199
  if __name__ == "__main__":
200
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  ####################################### IMPORT #################################
2
  import json
3
+ import time
4
+
5
  import pandas as pd
6
  from PIL import Image
7
  from loguru import logger
 
14
  from fastapi.exceptions import HTTPException
15
  import uvicorn
16
 
 
17
  from io import BytesIO
18
 
19
  from utils import get_image_from_bytes, detect_sample_model_origin
 
21
  from utils import add_bboxs_on_img
22
  from utils import get_bytes_from_image
23
  import gradio as gr
24
+
25
  ####################################### logger #################################
26
 
27
  logger.remove()
 
61
  allow_headers=["*"],
62
  )
63
 
64
+
65
  @app.on_event("startup")
66
  def save_openapi_json():
67
  '''This function is used to save the OpenAPI documentation
 
76
  with open("openapi.json", "w") as file:
77
  json.dump(openapi_data, file)
78
 
79
+
80
  # redirect
81
  @app.get("/", include_in_schema=False)
82
  async def redirect():
 
101
 
102
  ######################### Support Func #################################
103
 
104
+ def crop_image_by_predict(image: Image, predict: pd.DataFrame(), crop_class_name: str, ) -> Image:
105
  """Crop an image based on the detection of a certain object in the image.
106
 
107
  Args:
 
121
  if len(crop_predicts) > 1:
122
  crop_predicts = crop_predicts.sort_values(by=['confidence'], ascending=False)
123
 
124
+ crop_bbox = crop_predicts[['xmin', 'ymin', 'xmax', 'ymax']].iloc[0].values
125
  # crop
126
  img_crop = image.crop(crop_bbox)
127
+ return (img_crop)
128
 
129
 
130
  ######################### MAIN Func #################################
 
140
  Returns:
141
  dict: JSON format containing the Objects Detections.
142
  """
143
+ start = time.time()
144
  # Step 1: Initialize the result dictionary with None values
145
  # result={'detect_objects': None}
146
 
 
175
  }
176
 
177
  # Step 5: Logs and return
178
+ # logger.info("results: {}", results_json)
179
+ execute_time = time.time() - start
180
+ logger.info("Execute_time")
181
+ logger.info(execute_time)
182
  return results_json
183
 
184
+
185
  @app.post("/img_object_detection_to_img")
186
  def img_object_detection_to_img(file: bytes = File(...)):
187
  """
 
199
  predict = detect_sample_model(input_image)
200
 
201
  # add bbox on image
202
+ final_image = add_bboxs_on_img(image=input_image, predict=predict)
203
 
204
  # return image in bytes format
205
  return StreamingResponse(content=get_bytes_from_image(final_image), media_type="image/jpeg")
206
 
207
 
208
  if __name__ == "__main__":
209
+ uvicorn.run(app, host="0.0.0.0", port=7860)