Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.linear_model import LogisticRegression
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Simulated ML model for LBW decision (replace with real model later)
|
10 |
+
# Features: [pitching_x, pitching_y, impact_x, impact_y, speed, spin]
|
11 |
+
# Label: 0 (Not Out), 1 (Out)
|
12 |
+
def train_dummy_model():
|
13 |
+
X = np.array([
|
14 |
+
[0.5, 0.0, 0.4, 0.5, 30, 0], # Not Out (pitched outside leg)
|
15 |
+
[0.5, 0.5, 0.5, 0.5, 35, 2], # Out (inline, hits stumps)
|
16 |
+
[0.6, 0.2, 0.5, 0.6, 32, 1], # Not Out (impact outside off)
|
17 |
+
[0.5, 0.4, 0.5, 0.4, 34, 0], # Out (inline, hits stumps)
|
18 |
+
])
|
19 |
+
y = np.array([0, 1, 0, 1])
|
20 |
+
model = LogisticRegression()
|
21 |
+
model.fit(X, y)
|
22 |
+
return model
|
23 |
+
|
24 |
+
model = train_dummy_model()
|
25 |
+
|
26 |
+
# Simulate ball trajectory (red arc) and projection (blue line)
|
27 |
+
def calculate_trajectory(pitching_x, pitching_y, impact_x, impact_y, speed, spin):
|
28 |
+
# Dummy trajectory: Linear path from bowler to impact
|
29 |
+
actual_path = [
|
30 |
+
{"x": 0, "y": 0}, # Bowler position
|
31 |
+
{"x": pitching_x, "y": pitching_y}, # Pitching point
|
32 |
+
{"x": impact_x, "y": impact_y} # Impact point
|
33 |
+
]
|
34 |
+
# Projected path: Linear from impact to stumps (adjusted for spin)
|
35 |
+
projection = [
|
36 |
+
{"x": impact_x, "y": impact_y},
|
37 |
+
{"x": impact_x + spin * 0.1, "y": 1.0} # Stumps at y=1.0
|
38 |
+
]
|
39 |
+
return actual_path, projection
|
40 |
+
|
41 |
+
@app.route('/')
|
42 |
+
def index():
|
43 |
+
return render_template('index.html')
|
44 |
+
|
45 |
+
@app.route('/analyze', methods=['POST'])
|
46 |
+
def analyze():
|
47 |
+
# Get input data from form
|
48 |
+
data = request.form
|
49 |
+
pitching_x = float(data.get('pitching_x', 0.5))
|
50 |
+
pitching_y = float(data.get('pitching_y', 0.0))
|
51 |
+
impact_x = float(data.get('impact_x', 0.5))
|
52 |
+
impact_y = float(data.get('impact_y', 0.5))
|
53 |
+
speed = float(data.get('speed', 30))
|
54 |
+
spin = float(data.get('spin', 0))
|
55 |
+
|
56 |
+
# Calculate trajectories
|
57 |
+
actual_path, projected_path = calculate_trajectory(pitching_x, pitching_y, impact_x, impact_y, speed, spin)
|
58 |
+
|
59 |
+
# Predict LBW decision
|
60 |
+
features = np.array([[pitching_x, pitching_y, impact_x, impact_y, speed, spin]])
|
61 |
+
prediction = model.predict(features)[0]
|
62 |
+
confidence = model.predict_proba(features)[0][prediction]
|
63 |
+
decision = "Out" if prediction == 1 else "Not Out"
|
64 |
+
|
65 |
+
# Return data for visualization
|
66 |
+
return jsonify({
|
67 |
+
'actual_path': actual_path,
|
68 |
+
'projected_path': projected_path,
|
69 |
+
'decision': decision,
|
70 |
+
'confidence': round(confidence, 2),
|
71 |
+
'pitching': {'x': pitching_x, 'y': pitching_y},
|
72 |
+
'impact': {'x': impact_x, 'y': impact_y}
|
73 |
+
})
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
app.run(debug=True)
|