Cricket_Info / app.py
Anshini's picture
Update app.py
d6c7762 verified
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import seaborn as sns
import matplotlib.pyplot as plt
import base64
import plotly.express as px
import joblib
import cv2
from PIL import Image
import tempfile
import numpy as np
# Set page config
st.set_page_config(page_title="Cricket Career Insights", layout="wide")
# Function to set background image
def set_background(image_path):
with open(image_path, "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode()
page_bg_style = f"""
<style>
.stApp {{
background-image: url("data:image/png;base64,{encoded_image}");
background-size: cover;
background-position: center;
background-attachment: fixed;
}}
</style>
"""
st.markdown(page_bg_style, unsafe_allow_html=True)
# Call function with local image path
set_background("cricket_background.jpg")
# Load data
data = pd.read_csv("International_cricket_data.csv")
gr_df = data.groupby("Player")
# Sidebar Navigation
st.sidebar.title("🏏 Navigation")
page = st.sidebar.radio("Go to", ["🏠 Home", "πŸ“ˆ Player Career Info","Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)"])
if page == "🏠 Home":
st.title("🏏 Step into the world of cricket – scores and more await you!!!")
st.subheader("What is Cricket?")
st.markdown("""
Cricket is the heartbeat of millions, a game where bat meets ball, and history is written in every boundary and wicket.
It’s a battlefield of strategy, where captains plot, bowlers scheme, and batsmen rise like warriors to defend their honor.
From the electrifying roar of a six soaring into the stands to the nail-biting tension of the final over,
cricket is more than a sportβ€”it’s a journey of emotions, uniting fans across the globe.
""")
st.subheader("πŸ“Š About the Application")
st.write("""
Welcome to **Cricket Career Insights** – Your Ultimate Cricket Analytics Hub! πŸš€
- πŸ” **Player Stats** – Track career progression.
- πŸ† **Milestones** – First match & last match highlights.
- πŸ“Š **Performance Trends** – Runs, wickets, & strike rates across formats.
- πŸ”₯ **Record-Breaking Moments** – Highest scores & best bowling figures.
- ⚑ **Visual Insights** – Stunning charts & graphs.
""")
st.subheader("πŸ‘©β€πŸ’» About the Author")
st.markdown("""
<p style="text-align: justify;">
Hi, I’m Anshini Kumbhare! πŸ‘‹ I’m an AI πŸ’» enthusiast with a passion for exploring data and uncovering meaningful insights.
I thrive on solving real-world problems using data-driven approaches and have a deep interest in the field of data science.
With a curious mind and an eagerness to learn, I constantly explore advancements in AI and emerging technologies.
My love for cricket 🏏, combined with my technical skills, inspired me to create this applicationβ€”showcasing the perfect blend of passion and innovation.
When I’m not working on data projects, I enjoy staying updated with the latest trends in artificial intelligence,
contributing to AI communities, and collaborating on creative ideas to make technology accessible for everyone. βœ¨πŸ“Š
Let’s connect and explore the world of data together! πŸš€
</p>
""", unsafe_allow_html=True)
# Social Media Links
st.markdown("### πŸ“Œ Connect with Me:")
st.markdown("[![LinkedIn](https://img.shields.io/badge/LinkedIn-Profile-blue)](https://www.linkedin.com/in/anshini-kumbhare)")
st.markdown("[![GitHub](https://img.shields.io/badge/GitHub-Profile-lightgrey)](https://github.com/anshinii)")
st.markdown('</div>', unsafe_allow_html=True)
elif page == "πŸ“ˆ Player Career Info":
st.title("πŸ“ˆ Player Career Information")
player_name = st.selectbox("🏏 Choose a Cricketer", list(gr_df.groups.keys()))
player_data = gr_df.get_group(player_name).iloc[0]
formats = ['Test', 'ODI', 'T20', 'IPL']
# Batting Career Summary
st.subheader("Batting Career Summary")
batting_summary = [
[fmt, player_data.get(f'Matches_{fmt}', 0), player_data.get(f'batting_Innings_{fmt}', 0),
player_data.get(f'batting_Runs_{fmt}', 0), player_data.get(f'batting_Balls_{fmt}', 0),
player_data.get(f'batting_Highest_{fmt}', 0), player_data.get(f'batting_Average_{fmt}', 0),
player_data.get(f'batting_SR_{fmt}', 0), player_data.get(f'batting_Not Out_{fmt}', 0),
player_data.get(f'batting_Fours_{fmt}', 0), player_data.get(f'batting_Sixes_{fmt}', 0),
player_data.get(f'batting_50s_{fmt}', 0), player_data.get(f'batting_100s_{fmt}', 0),
player_data.get(f'batting_200s_{fmt}', 0)] for fmt in formats
]
batting_df = pd.DataFrame(batting_summary, columns=[
'Format', 'Matches', 'Innings', 'Runs', 'Balls Faced', 'Highest', 'Average',
'Strike Rate', 'Not Out', '4s', '6s', '50s', '100s', '200s'
])
st.dataframe(batting_df.set_index("Format"))
# Bowling Career Summary
st.subheader("Bowling Career Summary")
bowling_summary = [
[fmt, player_data.get(f'bowling_{fmt}_Innings', 0), player_data.get(f'bowling_{fmt}_Balls', 0),
player_data.get(f'bowling_{fmt}_Runs', 0), player_data.get(f'bowling_{fmt}_Wickets', 0),
player_data.get(f'bowling_{fmt}_Avg', 0), player_data.get(f'bowling_{fmt}_Eco', 0),
player_data.get(f'bowling_{fmt}_SR', 0), player_data.get(f'bowling_{fmt}_BBI', 0),
player_data.get(f'bowling_{fmt}_5w', 0), player_data.get(f'bowling_{fmt}_10w', 0)] for fmt in formats
]
bowling_df = pd.DataFrame(bowling_summary, columns=[
'Format', 'Innings', 'Balls', 'Runs', 'Wickets', 'Avg', 'Economy',
'Strike Rate', 'BBI', '5w', '10w'
])
st.dataframe(bowling_df.set_index("Format"))
# Matches per Format Pie Chart
st.subheader("Matches Played per Format")
match_counts = [player_data.get(f'Matches_{fmt}', 0) for fmt in formats]
fig1 = px.pie(names=formats, values=match_counts, title="Match Distribution by Format", hole=0.4)
st.plotly_chart(fig1)
# Batting and Strike Rate Bar Charts
st.subheader("Additional Visual Insights")
fig2 = px.bar(batting_df, x='Format', y='Average', title='Batting Average by Format', text_auto=True, color='Format')
st.plotly_chart(fig2)
fig3 = px.bar(batting_df, x='Format', y='Strike Rate', title='Strike Rate by Format', text_auto=True, color='Format')
st.plotly_chart(fig3)
# 100s and 50s Bar Chart
fig4 = go.Figure(data=[
go.Bar(name='100s', x=formats, y=batting_df['100s']),
go.Bar(name='50s', x=formats, y=batting_df['50s'])
])
fig4.update_layout(title='Centuries and Fifties by Format', barmode='group')
st.plotly_chart(fig4)
# Line Chart for Runs Trend
st.subheader("Runs Trend Across Formats")
fig5 = px.line(batting_df, x='Format', y='Runs', title='Runs Scored Across Formats', markers=True)
st.plotly_chart(fig5)
# if page == "Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)":
# st.title("🏏 Player Recognition & Performance Analysis")
# player_stats_df = pd.read_csv("final_cricket_dataset.csv")
# model = joblib.load("svc_face_classifier.pkl")
# label_encoder = joblib.load("label_encoder.pkl")
# face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# # Streamlit Page Setup
# # Image Preprocessing + Face Detection Function
# def detect_and_predict_face(image_file):
# image = Image.open(image_file).convert("RGB")
# img_np = np.array(image)
# gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
# faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# if len(faces) == 0:
# return None, "No face detected!"
# x, y, w, h = faces[0]
# face = gray[y:y+h, x:x+w]
# resized_face = cv2.resize(face, (64, 64))
# flattened = resized_face.flatten().reshape(1, -1)
# pred_label = model.predict(flattened)[0]
# pred_name = label_encoder.inverse_transform([pred_label])[0]
# return pred_name, None
# # Upload images
# col1, col2 = st.columns(2)
# with col1:
# img1 = st.file_uploader("Upload First Player Image", type=["jpg", "png", "jpeg"], key="img1")
# with col2:
# img2 = st.file_uploader("Upload Second Player Image", type=["jpg", "png", "jpeg"], key="img2")
# if img1 and img2:
# p1_name, err1 = detect_and_predict_face(img1)
# p2_name, err2 = detect_and_predict_face(img2)
# if err1:
# st.error(f"Image 1: {err1}")
# if err2:
# st.error(f"Image 2: {err2}")
# if not err1 and not err2:
# st.success(f"βœ… Player 1 Detected: {p1_name}")
# st.success(f"βœ… Player 2 Detected: {p2_name}")
# formats = ['Test', 'ODI', 'T20', 'IPL']
# df = player_stats_df
# players = [p1_name, p2_name]
# if p1_name not in df['Label'].values or p2_name not in df['Label'].values:
# st.error("One or both players not found in dataset.")
# st.stop()
# p1_data = df[df['Label'] == p1_name].iloc[0]
# p2_data = df[df['Label'] == p2_name].iloc[0]
# st.markdown("## πŸ“Š Comparative Stats")
# # Batting Summary
# st.markdown("### 🏏 Batting Career Summary")
# batting_summary = []
# for fmt in formats:
# batting_summary.append({
# "Format": fmt,
# p1_name: p1_data.get(f'batting_Runs_{fmt}', 0),
# p2_name: p2_data.get(f'batting_Runs_{fmt}', 0)
# })
# batting_df = pd.DataFrame(batting_summary)
# fig = px.bar(batting_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Total Runs by Format")
# st.plotly_chart(fig, use_container_width=True)
# # Bowling Summary
# st.markdown("### 🎯 Bowling Career Summary")
# bowling_summary = []
# for fmt in formats:
# bowling_summary.append({
# "Format": fmt,
# p1_name: p1_data.get(f'bowling_{fmt}_Wickets', 0),
# p2_name: p2_data.get(f'bowling_{fmt}_Wickets', 0)
# })
# bowling_df = pd.DataFrame(bowling_summary)
# fig = px.bar(bowling_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Total Wickets by Format")
# st.plotly_chart(fig, use_container_width=True)
# # Strike Rate vs Runs
# st.markdown("### ⚑ Strike Rate vs Runs")
# data = {
# "Format": formats,
# f"{p1_name} Runs": [p1_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats],
# f"{p1_name} SR": [p1_data.get(f'batting_SR_{fmt}', 0) for fmt in formats],
# f"{p2_name} Runs": [p2_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats],
# f"{p2_name} SR": [p2_data.get(f'batting_SR_{fmt}', 0) for fmt in formats],
# }
# fig = px.scatter(x=data[f"{p1_name} Runs"] + data[f"{p2_name} Runs"],
# y=data[f"{p1_name} SR"] + data[f"{p2_name} SR"],
# color=["Player 1"] * 4 + ["Player 2"] * 4,
# text=formats * 2,
# labels={"x": "Runs", "y": "Strike Rate"},
# title="Runs vs Strike Rate Comparison")
# st.plotly_chart(fig, use_container_width=True)
# # Milestones
# st.markdown("### πŸ† Milestone Comparison")
# milestone_df = pd.DataFrame({
# "Format": formats * 2,
# "Player": [p1_name] * 4 + [p2_name] * 4,
# "50s": [p1_data.get(f"batting_50s_{fmt}", 0) for fmt in formats] +
# [p2_data.get(f"batting_50s_{fmt}", 0) for fmt in formats],
# "100s": [p1_data.get(f"batting_100s_{fmt}", 0) for fmt in formats] +
# [p2_data.get(f"batting_100s_{fmt}", 0) for fmt in formats],
# "200s": [p1_data.get(f"batting_200s_{fmt}", 0) for fmt in formats] +
# [p2_data.get(f"batting_200s_{fmt}", 0) for fmt in formats]
# })
# for stat in ['50s', '100s', '200s']:
# fig = px.bar(milestone_df, x="Format", y=stat, color="Player", barmode="group",
# title=f"{stat} Achievements Comparison")
# st.plotly_chart(fig, use_container_width=True)
# # Pie Chart - Matches Played
# st.markdown("### 🧩 Matches Played by Format")
# for i, data_player in enumerate([p1_data, p2_data]):
# match_data = {
# fmt: data_player.get(f"Matches_{fmt}", 0) for fmt in formats
# }
# fig = px.pie(values=list(match_data.values()), names=list(match_data.keys()),
# title=f"{players[i]} Matches Distribution")
# st.plotly_chart(fig, use_container_width=True)
# # Final Trend Overview
# st.markdown("### πŸ“‰ Trend Overview")
# fig, ax = plt.subplots(1, 2, figsize=(14, 5))
# sns.barplot(x=formats, y=[p1_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats], ax=ax[0], label=p1_name)
# sns.barplot(x=formats, y=[p2_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats], ax=ax[0], label=p2_name)
# ax[0].set_title("Batting Runs Trend")
# ax[0].legend()
# sns.barplot(x=formats, y=[p1_data.get(f'bowling_{fmt}_Wickets', 0) for fmt in formats], ax=ax[1], label=p1_name)
# sns.barplot(x=formats, y=[p2_data.get(f'bowling_{fmt}_Wickets', 0) for fmt in formats], ax=ax[1], label=p2_name)
# ax[1].set_title("Bowling Wickets Trend")
# ax[1].legend()
# st.pyplot(fig)
elif page == "Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)":
st.title("🏏 Player Recognition & Performance Analysis")
player_stats_df = pd.read_csv("final_cricket_dataset.csv")
model = joblib.load("svc_face_classifier.pkl")
label_encoder = joblib.load("label_encoder.pkl")
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
def detect_and_predict_face(image_file):
image = Image.open(image_file).convert("RGB")
img_np = np.array(image)
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) == 0:
return None, "No face detected!"
x, y, w, h = faces[0]
face = gray[y:y+h, x:x+w]
resized = cv2.resize(face, (64, 64))
flat = resized.flatten().reshape(1, -1)
pred = model.predict(flat)[0]
name = label_encoder.inverse_transform([pred])[0]
return name, None
col1, col2 = st.columns(2)
img1 = col1.file_uploader("Upload First Player Image", type=["jpg", "jpeg", "png"], key="img1")
img2 = col2.file_uploader("Upload Second Player Image", type=["jpg", "jpeg", "png"], key="img2")
if img1 and img2:
p1_name, err1 = detect_and_predict_face(img1)
p2_name, err2 = detect_and_predict_face(img2)
if err1: st.error(f"Image 1: {err1}")
if err2: st.error(f"Image 2: {err2}")
if not err1 and not err2:
st.success(f"βœ… Player 1 Detected: {p1_name}")
st.success(f"βœ… Player 2 Detected: {p2_name}")
df = player_stats_df.copy()
if p1_name not in df['Label'].values or p2_name not in df['Label'].values:
st.error("One or both players not found in dataset.")
st.stop()
p1_data = df[df['Label'] == p1_name].iloc[0]
p2_data = df[df['Label'] == p2_name].iloc[0]
st.write("### πŸ“‹ Player Data Preview")
st.write(p1_data)
st.write(p2_data)
formats = ['Test', 'ODI', 'T20', 'IPL']
def plot_bar(compare_col, title, y_label):
vals1 = [p1_data.get(f"{compare_col}_{fmt}", 0) for fmt in formats]
vals2 = [p2_data.get(f"{compare_col}_{fmt}", 0) for fmt in formats]
df_plot = pd.DataFrame({
"Format": formats,
p1_name: vals1,
p2_name: vals2
})
if df_plot[[p1_name, p2_name]].sum().sum() == 0:
st.warning(f"No data to plot for {title}.")
return
try:
fig = px.bar(df_plot, x="Format", y=[p1_name, p2_name], barmode="group", title=title)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"Could not render {title}: {e}")
st.markdown("## πŸ“Š Comparative Stats")
st.subheader("πŸ›‘οΈ Batting – Total Runs by Format")
plot_bar("batting_Runs", "Total Runs by Format", "Runs")
st.subheader("🎯 Bowling – Total Wickets by Format")
plot_bar("bowling_Wickets", "Total Wickets by Format", "Wickets")
st.subheader("⚑ Runs vs Strike Rate Scatter")
data_pts = []
for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
runs = [pdata.get(f"batting_Runs_{fmt}", 0) for fmt in formats]
sr = [pdata.get(f"batting_SR_{fmt}", 0) for fmt in formats]
for fmt, r, s in zip(formats, runs, sr):
data_pts.append({
"Player": player,
"Format": fmt,
"Runs": r,
"Strike Rate": s
})
scatter_df = pd.DataFrame(data_pts)
if scatter_df[['Runs', 'Strike Rate']].sum().sum() == 0:
st.warning("Not enough data for scatter plot.")
else:
fig = px.scatter(scatter_df, x="Runs", y="Strike Rate",
color="Player", text="Format",
title="Runs vs Strike Rate Comparison",
labels={"Runs": "Runs", "Strike Rate": "Strike Rate"})
fig.update_traces(textposition='top center')
st.plotly_chart(fig, use_container_width=True)
st.subheader("πŸ† Milestone Comparison")
milestone_data = []
for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
for fmt in formats:
milestone_data.append({
"Player": player,
"Format": fmt,
"50s": pdata.get(f"batting_50s_{fmt}", 0),
"100s": pdata.get(f"batting_100s_{fmt}", 0),
"200s": pdata.get(f"batting_200s_{fmt}", 0)
})
ms_df = pd.DataFrame(milestone_data)
for stat in ["50s", "100s", "200s"]:
fig = px.bar(ms_df, x="Format", y=stat,
color="Player", barmode="group",
title=f"{stat} Achievements Comparison")
st.plotly_chart(fig, use_container_width=True)
st.subheader("🧩 Matches Distribution by Format")
for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
match_counts = [pdata.get(f"Matches_{fmt}", 0) for fmt in formats]
if sum(match_counts) == 0:
st.warning(f"No match data for {player}.")
continue
fig = px.pie(values=match_counts, names=formats,
title=f"{player} – Matches Distribution")
st.plotly_chart(fig, use_container_width=True)
st.subheader("πŸ“‰ Final Trends Overview")
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
sns.set_theme(style="whitegrid")
sns.barplot(x=formats,
y=[p1_data.get(f"batting_Runs_{fmt}", 0) for fmt in formats],
ax=axes[0], label=p1_name, color="b", alpha=0.6)
sns.barplot(x=formats,
y=[p2_data.get(f"batting_Runs_{fmt}", 0) for fmt in formats],
ax=axes[0], label=p2_name, color="r", alpha=0.6)
axes[0].set_title("Batting Runs Trend")
axes[0].legend()
sns.barplot(x=formats,
y=[p1_data.get(f"bowling_Test_Wickets", 0) for fmt in formats],
ax=axes[1], label=p1_name, color="b", alpha=0.6)
sns.barplot(x=formats,
y=[p2_data.get(f"bowling_Test_Wickets", 0) for fmt in formats],
ax=axes[1], label=p2_name, color="r", alpha=0.6)
axes[1].set_title("Bowling Wickets Trend")
axes[1].legend()
st.pyplot(fig)