Spaces:
Sleeping
Sleeping
| 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("[](https://www.linkedin.com/in/anshini-kumbhare)") | |
| st.markdown("[](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) | |