Skip to content

Commit

Permalink
Fix parsing, added HTTP exn
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikonooooo committed Dec 4, 2023
1 parent 1cc8862 commit 16fc396
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 83 deletions.
47 changes: 26 additions & 21 deletions src/api/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import time
import io
from fastapi import FastAPI, File, UploadFile
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import StreamingResponse, FileResponse
import pandas as pd
import boto3
Expand All @@ -18,7 +18,6 @@
# from ..format import Format



# Amazon S3 Connection
# load_dotenv()
# access_key = os.getenv("AWS_ACCESS_KEY_ID")
Expand All @@ -28,6 +27,7 @@

app = FastAPI()


# Root
@app.get("/")
async def root():
Expand All @@ -44,7 +44,7 @@ async def upload_file(video_file: UploadFile = File(...)):
s3.upload_fileobj(video_file.file, "hooptracker-uploads", file_name + ".mp4")
return {"message": file_name, "status": "success"}
except Exception as ex:
return {"error": str(ex)}
raise HTTPException(status_code=500, detail=str(ex))


@app.post("/process")
Expand All @@ -55,7 +55,6 @@ async def process_file(file_name: str):
"""

try:

# create a data directory if it does not exist already
os.makedirs("data", exist_ok=True)

Expand All @@ -73,7 +72,7 @@ async def process_file(file_name: str):
s3.upload_file("tmp/processed.mp4", "hooptracker-uploads", court_reenc_path)
return {"message": f"successfully processed {file_name}", "status": "success"}
except Exception as ex:
return {"error": str(ex)}
raise HTTPException(status_code=500, detail=str(ex))


@app.get("/download/{file_name}")
Expand All @@ -87,8 +86,14 @@ async def download_file(file_name: str):
download_path_video = "tmp/court_video_reenc-" + file_name + ".mp4"
download_path_txt = "tmp/results-" + file_name + ".txt"

s3.download_file("hooptracker-uploads", "court_video_reenc-" + file_name + ".mp4", download_path_video)
s3.download_file("hooptracker-uploads", "results-" + file_name + ".txt", download_path_txt)
s3.download_file(
"hooptracker-uploads",
"court_video_reenc-" + file_name + ".mp4",
download_path_video,
)
s3.download_file(
"hooptracker-uploads", "results-" + file_name + ".txt", download_path_txt
)

temp_dir = "temporary"
os.makedirs(temp_dir, exist_ok=True)
Expand All @@ -106,28 +111,28 @@ async def download_file(file_name: str):
# clean up temporary directory
shutil.rmtree(temp_dir)

return FileResponse(zip_path, media_type="application/zip", filename="files.zip")
return FileResponse(
zip_path, media_type="application/zip", filename="files.zip"
)
# return {"message": "successfully downloaded", "status": "success"}
except Exception as ex:
return {"error": str(ex)}

raise HTTPException(status_code=500, detail=str(ex))


@app.get("/results")
async def get_formatted_results():
try:
# Assuming the Format.results() method returns the formatted data as JSON
formatted_data = Format.results()
return formatted_data
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

return "PLACEHOLDER"
except Exception as ex:
raise HTTPException(status_code=500, detail=str(ex))


@app.get("/video")
async def get_videos():
file_path = 'tmp/minimap.mp4'
def iterfile():
with open(file_path, mode="rb") as file_like:
yield from file_like
file_path = "tmp/minimap.mp4"

def iterfile():
with open(file_path, mode="rb") as file_like:
yield from file_like

return StreamingResponse(iterfile(), media_type="video/mp4")
return StreamingResponse(iterfile(), media_type="video/mp4")
6 changes: 3 additions & 3 deletions src/processing/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def parse_sort_output(state: GameState, sort_output) -> None:
frame, obj_type, id, xmin, ymin, xwidth, ywidth = lines[b][:7]
if s >= len(sts): # s-1 frameno < bframe, s = len(states)
sts.append(Frame(s)) # append at index s
sts[s].rim = sts[s - 1].rim # ensure rim set
if s > 0:
sts[s].rim = sts[s - 1].rim # ensure rim set
if frame > s:
s += 1
continue

sF: Frame = sts[s]
assert s == frame
sF: Frame = sts[frame]
box = (xmin, ymin, xmin + xwidth, ymin + ywidth)
if obj_type is ObjectType.BALL.value:
sF.add_ball_frame(id, *box)
Expand Down
Loading

0 comments on commit 16fc396

Please sign in to comment.