Sudipta Nayak commited on
Commit
c4f2cca
·
1 Parent(s): b896387

logging added

Browse files
Files changed (1) hide show
  1. 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
- print('Item:', item)
 
31
 
32
- print('File name:', item.file.filename)
33
-
34
- input_file = f"uploads/{item.file.filename}"
35
- output_file = f"static/output/{item.file.filename}"
36
 
37
- # Save the uploaded file
38
- with open(input_file, "wb") as file:
39
- file.write(item.file.file.read())
40
 
41
- print('Detect start')
42
 
43
- # Run YOLOv7 detection and save output
44
- 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])
45
 
46
- print('Detect end')
47
 
48
- # Render HTML using Jinja2Templates
49
- return templates.TemplateResponse(
50
- "result.html",
51
- {"request": request, "original_image": f"/static/{item.file.filename}", "output_image": f"/static/output/{item.file.filename}"},
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