Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import joblib | |
import cv2 | |
from PIL import Image | |
import plotly.express as px | |
import plotly.graph_objects as go | |
# ------------------------------- | |
# 1. Setup | |
# ------------------------------- | |
st.set_page_config(page_title="Cricket Player Comparision", layout="centered") | |
st.title("π Cricket Player Comparision Tool") | |
# ------------------------------- | |
# 2. Load Data & Models | |
# ------------------------------- | |
def load_dataset(): | |
return pd.read_csv("cric_final.csv") | |
def load_assets(): | |
model = joblib.load("svc_face_classifier.pkl") | |
encoder = joblib.load("label_encoder.pkl") | |
detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
return model, encoder, detector | |
df = load_dataset() | |
model, encoder, detector = load_assets() | |
formats = ["Test", "ODI", "T20", "IPL"] | |
# ------------------------------- | |
# 3. Player Detection from Image | |
# ------------------------------- | |
def detect_player(image_file, model, encoder, detector): | |
try: | |
img = Image.open(image_file).convert("RGB") | |
img_np = np.array(img) | |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY) | |
faces = detector.detectMultiScale(gray, 1.3, 5) | |
if not len(faces): | |
return None, "No face detected" | |
x, y, w, h = faces[0] | |
face = cv2.resize(gray[y:y+h, x:x+w], (64, 64)).flatten().reshape(1, -1) | |
label = model.predict(face)[0] | |
name = encoder.inverse_transform([label])[0] | |
return name, None | |
except Exception as e: | |
return None, str(e) | |
# ------------------------------- | |
# 4. Player Stats Summary | |
# ------------------------------- | |
def get_summary(player_row, formats): | |
return { | |
"Total Runs": sum(player_row.get(f'batting_Runs_{f}', 0) for f in formats), | |
"Total Wickets": sum(player_row.get(f'bowling_{f}_Wickets', 0) for f in formats), | |
"Best Strike Rate": max(player_row.get(f'batting_SR_{f}', 0) for f in formats) | |
} | |
# ------------------------------- | |
# 5. Upload Player Images | |
# ------------------------------- | |
col1, col2 = st.columns(2) | |
with col1: | |
img1 = st.file_uploader("Upload Player 1 Image", type=["jpg", "png", "jpeg"], key="img1") | |
with col2: | |
img2 = st.file_uploader("Upload Player 2 Image", type=["jpg", "png", "jpeg"], key="img2") | |
p1_name, p2_name = None, None | |
if img1: | |
p1_name, err1 = detect_player(img1, model, encoder, detector) | |
if err1: | |
st.error(f"Player 1 Error: {err1}") | |
else: | |
col1.image(img1, caption=f"Player 1: {p1_name}", width=200) | |
if img2: | |
p2_name, err2 = detect_player(img2, model, encoder, detector) | |
if err2: | |
st.error(f"Player 2 Error: {err2}") | |
else: | |
col2.image(img2, caption=f"Player 2: {p2_name}", width=200) | |
if not (p1_name and p2_name): | |
st.warning("Upload images for both players to load the data.") | |
st.stop() | |
if p1_name not in df["Player"].values or p2_name not in df["Player"].values: | |
st.error("One or both players are not in the dataset.") | |
st.stop() | |
# ------------------------------- | |
# 6. Stats Extraction | |
# ------------------------------- | |
player1 = df[df["Player"] == p1_name].iloc[0] | |
player2 = df[df["Player"] == p2_name].iloc[0] | |
stats1 = get_summary(player1, formats) | |
stats2 = get_summary(player2, formats) | |
# ------------------------------- | |
# 7. Display Summary Stats | |
# ------------------------------- | |
st.subheader("π Player Summary") | |
col1, col2 = st.columns(2) | |
for col, stats in zip([col1, col2], [stats1, stats2]): | |
col.metric("Total Runs", stats["Total Runs"]) | |
col.metric("Total Wickets", stats["Total Wickets"]) | |
col.metric("Best SR", round(stats["Best Strike Rate"], 2)) | |
# ------------------------------- | |
# 8. Visual Comparisons | |
# ------------------------------- | |
st.markdown("## π Visual Comparison") | |
# Batting | |
bat_df = pd.DataFrame({ | |
"Format": formats, | |
p1_name: [player1.get(f'batting_Runs_{f}', 0) for f in formats], | |
p2_name: [player2.get(f'batting_Runs_{f}', 0) for f in formats] | |
}) | |
st.plotly_chart(px.bar(bat_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Batting Runs")) | |
# Bowling | |
bowl_df = pd.DataFrame({ | |
"Format": formats, | |
p1_name: [player1.get(f'bowling_{f}_Wickets', 0) for f in formats], | |
p2_name: [player2.get(f'bowling_{f}_Wickets', 0) for f in formats] | |
}) | |
st.plotly_chart(px.bar(bowl_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Bowling Wickets")) | |
# ------------------------------- | |
# Strike Rate Comparison | |
sr_df = pd.DataFrame({ | |
"Format": formats, | |
p1_name: [player1.get(f'batting_SR_{f}', 0) for f in formats], | |
p2_name: [player2.get(f'batting_SR_{f}', 0) for f in formats] | |
}) | |
st.plotly_chart(px.bar(sr_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Strike Rate Comparison")) | |
#--------------------------------- | |
# 9. Match Distribution | |
# ------------------------------- | |
for name, row, col, key_suffix in zip( | |
[p1_name, p2_name], | |
[player1, player2], | |
[col1, col2], | |
["player1", "player2"] | |
): | |
values = [row.get(f"Matches_{f}", 0) for f in formats] | |
fig = px.pie( | |
values=values, | |
names=formats, | |
title=f"{name}'s Match Distribution" | |
) | |
col.plotly_chart(fig, key=f"pie_chart_{key_suffix}") # π Unique key | |
# 10. Milestones | |
# ------------------------------- | |
st.subheader("π Milestones") | |
for fmt in formats: | |
st.markdown(f"### {fmt}") | |
col1, col2 = st.columns(2) | |
for m in ["50s", "100s", "200s"]: | |
col1.metric(f"{p1_name} {m}", player1.get(f"batting_{m}_{fmt}", 0)) | |
col2.metric(f"{p2_name} {m}", player2.get(f"batting_{m}_{fmt}", 0)) | |