File size: 2,691 Bytes
49e447c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3875f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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, port=5001)