-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
123 lines (98 loc) · 3.99 KB
/
main.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
####################################### IMPORT #################################
import json
import pandas as pd
from PIL import Image
from loguru import logger
import sys
from fastapi import FastAPI, status
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import HTTPException
from router import detection_post
# from router import tracking_post
####################################### logger #################################
logger.remove()
logger.add(
sys.stderr,
colorize=True,
format="<green>{time:HH:mm:ss}</green> | <level>{message}</level>",
level=10,
)
logger.add("log.log", rotation="1 MB", level="DEBUG", compression="zip")
###################### FastAPI Setup #############################
# title
app = FastAPI(
title="Object Detection using YOLOv8 and FastAPI Template",
description="""Obtain object value out of image
and return image and json result""",
version="2023.6.1",
)
# This function is needed if you want to allow client requests
# from specific domains (specified in the origins argument)
# to access resources from the FastAPI server,
# and the client and server are hosted on different domains.
origins = [
"http://localhost",
"http://localhost:8008",
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(detection_post.router)
# app.include_router(tracking_post.router)
@app.on_event("startup")
def save_openapi_json():
'''This function is used to save the OpenAPI documentation
data of the FastAPI application to a JSON file.
The purpose of saving the OpenAPI documentation data is to have
a permanent and offline record of the API specification,
which can be used for documentation purposes or
to generate client libraries. It is not necessarily needed,
but can be helpful in certain scenarios.'''
openapi_data = app.openapi()
# Change "openapi.json" to desired filename
with open("openapi.json", "w") as file:
json.dump(openapi_data, file)
# redirect
@app.get("/", include_in_schema=False, tags=['docs'])
async def redirect():
return RedirectResponse("/docs")
@app.get('/healthcheck', status_code=status.HTTP_200_OK, tags=['Health Check'])
def perform_healthcheck():
'''
It basically sends a GET request to the route & hopes to get a "200"
response code. Failing to return a 200 response code just enables
the GitHub Actions to rollback to the last version the project was
found in a "working condition". It acts as a last line of defense in
case something goes south.
Additionally, it also returns a JSON response in the form of:
{
'healtcheck': 'Everything OK!'
}
'''
return {'healthcheck': 'Everything OK!'}
######################### Support Func #################################
def crop_image_by_predict(image: Image, predict: pd.DataFrame(), crop_class_name: str,) -> Image:
"""Crop an image based on the detection of a certain object in the image.
Args:
image: Image to be cropped.
predict (pd.DataFrame): Dataframe containing the prediction results of object detection model.
crop_class_name (str, optional): The name of the object class to crop the image by. if not provided, function returns the first object found in the image.
Returns:
Image: Cropped image or None
"""
crop_predicts = predict[(predict['name'] == crop_class_name)]
if crop_predicts.empty:
raise HTTPException(status_code=400, detail=f"{crop_class_name} not found in photo")
# if there are several detections, choose the one with more confidence
if len(crop_predicts) > 1:
crop_predicts = crop_predicts.sort_values(by=['confidence'], ascending=False)
crop_bbox = crop_predicts[['xmin', 'ymin', 'xmax','ymax']].iloc[0].values
# crop
img_crop = image.crop(crop_bbox)
return(img_crop)