LBW / app.py
dschandra's picture
Create app.py
49e447c verified
raw
history blame
2.68 kB
from flask import Flask, render_template, request, jsonify
import numpy as np
from sklearn.linear_model import LogisticRegression
import json
import os
app = Flask(__name__)
# Simulated ML model for LBW decision (replace with real model later)
# Features: [pitching_x, pitching_y, impact_x, impact_y, speed, spin]
# Label: 0 (Not Out), 1 (Out)
def train_dummy_model():
X = np.array([
[0.5, 0.0, 0.4, 0.5, 30, 0], # Not Out (pitched outside leg)
[0.5, 0.5, 0.5, 0.5, 35, 2], # Out (inline, hits stumps)
[0.6, 0.2, 0.5, 0.6, 32, 1], # Not Out (impact outside off)
[0.5, 0.4, 0.5, 0.4, 34, 0], # Out (inline, hits stumps)
])
y = np.array([0, 1, 0, 1])
model = LogisticRegression()
model.fit(X, y)
return model
model = train_dummy_model()
# Simulate ball trajectory (red arc) and projection (blue line)
def calculate_trajectory(pitching_x, pitching_y, impact_x, impact_y, speed, spin):
# Dummy trajectory: Linear path from bowler to impact
actual_path = [
{"x": 0, "y": 0}, # Bowler position
{"x": pitching_x, "y": pitching_y}, # Pitching point
{"x": impact_x, "y": impact_y} # Impact point
]
# Projected path: Linear from impact to stumps (adjusted for spin)
projection = [
{"x": impact_x, "y": impact_y},
{"x": impact_x + spin * 0.1, "y": 1.0} # Stumps at y=1.0
]
return actual_path, projection
@app.route('/')
def index():
return render_template('index.html')
@app.route('/analyze', methods=['POST'])
def analyze():
# Get input data from form
data = request.form
pitching_x = float(data.get('pitching_x', 0.5))
pitching_y = float(data.get('pitching_y', 0.0))
impact_x = float(data.get('impact_x', 0.5))
impact_y = float(data.get('impact_y', 0.5))
speed = float(data.get('speed', 30))
spin = float(data.get('spin', 0))
# Calculate trajectories
actual_path, projected_path = calculate_trajectory(pitching_x, pitching_y, impact_x, impact_y, speed, spin)
# Predict LBW decision
features = np.array([[pitching_x, pitching_y, impact_x, impact_y, speed, spin]])
prediction = model.predict(features)[0]
confidence = model.predict_proba(features)[0][prediction]
decision = "Out" if prediction == 1 else "Not Out"
# Return data for visualization
return jsonify({
'actual_path': actual_path,
'projected_path': projected_path,
'decision': decision,
'confidence': round(confidence, 2),
'pitching': {'x': pitching_x, 'y': pitching_y},
'impact': {'x': impact_x, 'y': impact_y}
})
if __name__ == '__main__':
app.run(debug=True)