Sudipta Nayak
commited on
Commit
·
c4f2cca
1
Parent(s):
b896387
logging added
Browse files- app/main.py +23 -17
app/main.py
CHANGED
@@ -7,6 +7,8 @@ from pydantic import BaseModel
|
|
7 |
import subprocess
|
8 |
from pathlib import Path
|
9 |
from config import settings
|
|
|
|
|
10 |
|
11 |
app = FastAPI(
|
12 |
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
@@ -27,29 +29,33 @@ async def root(request: Request):
|
|
27 |
|
28 |
@app.post("/detect", response_class=HTMLResponse)
|
29 |
async def detect_objects(request: Request, item: Item):
|
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 |
|
|
|
7 |
import subprocess
|
8 |
from pathlib import Path
|
9 |
from config import settings
|
10 |
+
import logging
|
11 |
+
|
12 |
|
13 |
app = FastAPI(
|
14 |
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
|
|
29 |
|
30 |
@app.post("/detect", response_class=HTMLResponse)
|
31 |
async def detect_objects(request: Request, item: Item):
|
32 |
+
try:
|
33 |
+
print('Item:', item)
|
34 |
|
35 |
+
print('File name:', item.file.filename)
|
36 |
+
|
37 |
+
input_file = f"uploads/{item.file.filename}"
|
38 |
+
output_file = f"static/output/{item.file.filename}"
|
39 |
|
40 |
+
# Save the uploaded file
|
41 |
+
with open(input_file, "wb") as file:
|
42 |
+
file.write(item.file.file.read())
|
43 |
|
44 |
+
print('Detect start')
|
45 |
|
46 |
+
# Run YOLOv7 detection and save output
|
47 |
+
subprocess.run(["python", "detect.py", "--conf", "0.5", "--img-size", "640", "--weights", "app/model/best.pt", "--source", input_file, "--save-txt", "--save-conf", "--exist-ok", "--project", output_file])
|
48 |
|
49 |
+
print('Detect end')
|
50 |
|
51 |
+
# Render HTML using Jinja2Templates
|
52 |
+
return templates.TemplateResponse(
|
53 |
+
"result.html",
|
54 |
+
{"request": request, "original_image": f"/static/{item.file.filename}", "output_image": f"/static/output/{item.file.filename}"},
|
55 |
+
)
|
56 |
+
except Exception as e:
|
57 |
+
logging.error(f"Error in /detect endpoint: {str(e)}")
|
58 |
+
raise e
|
59 |
|
60 |
|
61 |
|