Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# backend/app.py
|
2 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
|
3 |
+
from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
+
import shutil
|
6 |
+
import os
|
7 |
+
from utils.video_processing import track_ball, generate_replay
|
8 |
+
from utils.drs_logic import predict_impact, is_drs_out
|
9 |
+
from utils.db import update_leaderboard, get_leaderboard, store_drs_result
|
10 |
+
from utils.huggingface_api import submit_to_huggingface
|
11 |
+
|
12 |
+
app = FastAPI(title="Gully DRS API")
|
13 |
+
templates = Jinja2Templates(directory="templates")
|
14 |
+
|
15 |
+
# Ensure upload and replay directories exist
|
16 |
+
os.makedirs("uploads", exist_ok=True)
|
17 |
+
os.makedirs("replays", exist_ok=True)
|
18 |
+
|
19 |
+
@app.post("/upload")
|
20 |
+
async def upload_video(request: Request, file: UploadFile = File(...), player_name: str = "Player1"):
|
21 |
+
"""
|
22 |
+
Upload a video for DRS analysis, process it, and store results in Salesforce.
|
23 |
+
"""
|
24 |
+
try:
|
25 |
+
# Save uploaded video
|
26 |
+
video_path = f"uploads/{file.filename}"
|
27 |
+
with open(video_path, "wb") as buffer:
|
28 |
+
shutil.copyfileobj(file.file, buffer)
|
29 |
+
|
30 |
+
# Process video to extract ball trajectory
|
31 |
+
ball_positions = track_ball(video_path)
|
32 |
+
|
33 |
+
# Predict impact and determine DRS decision
|
34 |
+
impact_point = predict_impact(ball_positions)
|
35 |
+
decision = "Out" if is_drs_out(impact_point) else "Not Out"
|
36 |
+
|
37 |
+
# Generate replay video
|
38 |
+
replay_path = generate_replay(video_path, ball_positions, decision)
|
39 |
+
|
40 |
+
# Submit to HuggingFace API for additional analysis (e.g., speed)
|
41 |
+
huggingface_response = submit_to_huggingface(video_path)
|
42 |
+
speed = huggingface_response.get("speed", 0.0)
|
43 |
+
replay_url = f"/replay/{os.path.basename(replay_path)}"
|
44 |
+
|
45 |
+
# Store DRS result in Salesforce (DRS_Result__c)
|
46 |
+
match_id = store_drs_result(
|
47 |
+
video_url=video_path,
|
48 |
+
verdict=decision,
|
49 |
+
speed=speed,
|
50 |
+
replay_link=replay_url,
|
51 |
+
player_name=player_name
|
52 |
+
)
|
53 |
+
|
54 |
+
# Update leaderboard in Salesforce (User_Profile__c)
|
55 |
+
update_leaderboard(player_name, drs_out=(decision == "Out"))
|
56 |
+
|
57 |
+
# Render result page
|
58 |
+
return templates.TemplateResponse("result.html", {
|
59 |
+
"request": request,
|
60 |
+
"decision": decision,
|
61 |
+
"replay_url": replay_url,
|
62 |
+
"speed": speed,
|
63 |
+
"match_id": match_id,
|
64 |
+
"player_name": player_name
|
65 |
+
})
|
66 |
+
except Exception as e:
|
67 |
+
raise HTTPException(status_code=500, detail=str(e))
|
68 |
+
|
69 |
+
@app.get("/replay/{replay_name}")
|
70 |
+
async def get_replay(replay_name: str):
|
71 |
+
"""
|
72 |
+
Serve the replay video file.
|
73 |
+
"""
|
74 |
+
replay_path = f"replays/{replay_name}"
|
75 |
+
if not os.path.exists(replay_path):
|
76 |
+
raise HTTPException(status_code=404, detail="Replay not found")
|
77 |
+
return FileResponse(replay_path)
|
78 |
+
|
79 |
+
@app.get("/leaderboard", response_class=HTMLResponse)
|
80 |
+
async def get_leaderboard_data(request: Request):
|
81 |
+
"""
|
82 |
+
Render the leaderboard page with data from Salesforce.
|
83 |
+
"""
|
84 |
+
try:
|
85 |
+
leaderboard = get_leaderboard()
|
86 |
+
return templates.TemplateResponse("leaderboard.html", {
|
87 |
+
"request": request,
|
88 |
+
"leaderboard": [{
|
89 |
+
"id": player["Id"],
|
90 |
+
"name": player["Name__c"],
|
91 |
+
"matches": player["Matches__c"],
|
92 |
+
"drs_dismissals": player["LBW_Dismissals__c"],
|
93 |
+
"score": player["Score__c"]
|
94 |
+
} for player in leaderboard]
|
95 |
+
})
|
96 |
+
except Exception as e:
|
97 |
+
raise HTTPException(status_code=500, detail=str(e))
|
98 |
+
|
99 |
+
@app.get("/", response_class=HTMLResponse)
|
100 |
+
async def home(request: Request):
|
101 |
+
"""
|
102 |
+
Render the home page with upload form.
|
103 |
+
"""
|
104 |
+
return templates.TemplateResponse("index.html", {"request": request})
|