Cric_Metrics / pages /2player_comparison.py
Sathwikchowdary's picture
Update pages/2player_comparison.py
659dfad verified
raw
history blame
2.53 kB
import streamlit as st
import pandas as pd
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
# Load model and encoder
@st.cache_resource
def load_model_and_encoder():
with open('best_rf_pipeline.pkl', 'rb') as f:
model = pickle.load(f)
with open('label_encoder.pkl', 'rb') as f:
encoder = pickle.load(f)
return model, encoder
# Load player dataset
@st.cache_data
def load_data():
df = pd.read_csv('Reduced_final_teams.csv')
return df
# Main App
def main():
st.title("Cricket Player Comparison Tool 🏏")
st.write("Upload two player images and compare their performance visually.")
df = load_data()
model, encoder = load_model_and_encoder()
# Upload Images
col1, col2 = st.columns(2)
with col1:
img1 = st.file_uploader("Upload Image for Player 1", type=['png', 'jpg', 'jpeg'], key='img1')
with col2:
img2 = st.file_uploader("Upload Image for Player 2", type=['png', 'jpg', 'jpeg'], key='img2')
if img1 and img2:
# Placeholder: Extract player name from uploaded file name
player1_name = img1.name.split('.')[0]
player2_name = img2.name.split('.')[0]
if player1_name in df['Player'].values and player2_name in df['Player'].values:
player1_data = df[df['Player'] == player1_name].squeeze()
player2_data = df[df['Player'] == player2_name].squeeze()
st.success(f"Comparing **{player1_name}** vs **{player2_name}**")
# Combine both players into one DataFrame for visualization
comparison_df = pd.DataFrame([player1_data, player2_data])
comparison_df.set_index('Player', inplace=True)
# Display Side-by-Side Metrics
st.subheader("πŸ” Key Metrics")
st.dataframe(comparison_df.T)
# Radar chart / Barplot for comparison
st.subheader("πŸ“Š Visual Comparison")
num_cols = comparison_df.select_dtypes(include='number').columns.tolist()
# Plot
plt.figure(figsize=(10, 5))
comparison_df[num_cols].T.plot(kind='bar', figsize=(10, 6))
plt.title(f"Stat Comparison: {player1_name} vs {player2_name}")
plt.ylabel("Metric Value")
plt.xticks(rotation=45)
st.pyplot(plt.gcf())
else:
st.error("Player names extracted from images do not match any in the dataset. Ensure filenames match player names in the CSV.")
if __name__ == "__main__":
main()