model loads on request
Browse files- inference_utils.py +0 -1
- main.py +9 -6
inference_utils.py
CHANGED
|
@@ -90,7 +90,6 @@ def get_predictions(model,img,return_raw = False):
|
|
| 90 |
"confidenceInClass" : float(i[4]),
|
| 91 |
"DetectedClass" : every_monument[int(i[5])]
|
| 92 |
}
|
| 93 |
-
print(temp)
|
| 94 |
results.append(temp)
|
| 95 |
|
| 96 |
# changing the final arrays of dictionaries before returning
|
|
|
|
| 90 |
"confidenceInClass" : float(i[4]),
|
| 91 |
"DetectedClass" : every_monument[int(i[5])]
|
| 92 |
}
|
|
|
|
| 93 |
results.append(temp)
|
| 94 |
|
| 95 |
# changing the final arrays of dictionaries before returning
|
main.py
CHANGED
|
@@ -4,15 +4,18 @@ import cv2
|
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
import aiofiles
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
model = torch.hub.load(yolo_path, 'custom', path = weights_path, source = 'local',device='cpu',force_reload=True)
|
| 12 |
-
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
-
|
| 16 |
CHUNK_SIZE = 1024 * 1024
|
| 17 |
|
| 18 |
@app.get("/")
|
|
@@ -21,7 +24,7 @@ async def root():
|
|
| 21 |
|
| 22 |
@app.post("/detect-monument/")
|
| 23 |
async def upload(file: UploadFile = File(...)):
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
filepath = os.path.join('./', os.path.basename(file.filename))
|
| 27 |
async with aiofiles.open(filepath, 'wb') as f:
|
|
|
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
import aiofiles
|
| 7 |
+
from functools import lru_cache
|
| 8 |
|
| 9 |
+
@lru_cache(maxsize=1)
|
| 10 |
+
def load_model():
|
| 11 |
+
weights_path = os.path.join('best.pt')
|
| 12 |
+
yolo_path = os.path.join('yolov5')
|
| 13 |
+
model = torch.hub.load(yolo_path, 'custom', path = weights_path, source = 'local',device='cpu',force_reload=True)
|
| 14 |
|
| 15 |
+
return model
|
| 16 |
+
|
|
|
|
|
|
|
| 17 |
|
| 18 |
app = FastAPI()
|
|
|
|
| 19 |
CHUNK_SIZE = 1024 * 1024
|
| 20 |
|
| 21 |
@app.get("/")
|
|
|
|
| 24 |
|
| 25 |
@app.post("/detect-monument/")
|
| 26 |
async def upload(file: UploadFile = File(...)):
|
| 27 |
+
model = load_model()
|
| 28 |
try:
|
| 29 |
filepath = os.path.join('./', os.path.basename(file.filename))
|
| 30 |
async with aiofiles.open(filepath, 'wb') as f:
|