Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import cv2 | |
| import joblib | |
| from PIL import Image | |
| import plotly.express as px | |
| # Load models and data | |
| def load_model(): | |
| return joblib.load("svc_face_classifier (1).pkl") | |
| def load_label_encoder(): | |
| return joblib.load("label_encoder.pkl") | |
| def load_data(): | |
| return pd.read_csv("Final_Data.csv") | |
| # Haar Cascade | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
| # Detect + Predict | |
| def detect_and_predict_face(image_file, model, label_encoder): | |
| image = Image.open(image_file).convert("RGB") | |
| gray = cv2.cvtColor(np.array(image), 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)) | |
| flattened = resized.flatten().reshape(1, -1) | |
| pred_label = model.predict(flattened)[0] | |
| pred_name = label_encoder.inverse_transform([pred_label])[0] | |
| return pred_name, None | |
| # Player stat functions | |
| def get_player_details(df, name): | |
| return df[df['Player'] == name].iloc[0] | |
| def plot_matches_pie(player_data): | |
| matches_stats = { | |
| 'Format': ['Test', 'ODI', 'T20', 'IPL'], | |
| 'Matches': [player_data['Matches_Test'], player_data['Matches_ODI'], player_data['Matches_T20'], player_data['Matches_IPL']] | |
| } | |
| fig = px.pie(pd.DataFrame(matches_stats), names='Format', values='Matches', | |
| title=f"π {player_data['Player']} - Matches Played π―") | |
| st.plotly_chart(fig) | |
| def plot_batting_stats(player_data): | |
| stats = { | |
| 'Format': ['Test', 'ODI', 'T20', 'IPL'], | |
| 'Runs': [player_data['batting_Runs_Test'], player_data['batting_Runs_ODI'], player_data['batting_Runs_T20'], player_data['batting_Runs_IPL']], | |
| 'Average': [player_data['batting_Average_Test'], player_data['batting_Average_ODI'], player_data['batting_Average_T20'], player_data['batting_Average_IPL']], | |
| 'Sixes': [player_data['batting_Sixes_Test'], player_data['batting_Sixes_ODI'], player_data['batting_Sixes_T20'], player_data['batting_Sixes_IPL']], | |
| 'Fours': [player_data['batting_Fours_Test'], player_data['batting_Fours_ODI'], player_data['batting_Fours_T20'], player_data['batting_Fours_IPL']], | |
| 'Strike Rate': [player_data['batting_SR_Test'], player_data['batting_SR_ODI'], player_data['batting_SR_T20'], player_data['batting_SR_IPL']] | |
| } | |
| df = pd.DataFrame(stats) | |
| st.plotly_chart(px.bar(df, x='Format', y='Runs', title=f"πββοΈ {player_data['Player']} - Runs Scored π―", text='Runs')) | |
| st.plotly_chart(px.bar(df, x='Format', y='Average', title=f"π {player_data['Player']} - Batting Average β", text='Average')) | |
| st.plotly_chart(px.bar(df, x='Format', y='Sixes', title=f"π₯ {player_data['Player']} - Sixes π", text='Sixes')) | |
| st.plotly_chart(px.bar(df, x='Format', y='Fours', title=f"π― {player_data['Player']} - Fours β‘", text='Fours')) | |
| st.plotly_chart(px.line(df, x='Format', y='Strike Rate', title=f"β‘ {player_data['Player']} - Strike Rate π―", markers=True, text='Strike Rate')) | |
| def plot_bowling_stats(player_data): | |
| stats = { | |
| 'Format': ['Test', 'ODI', 'T20', 'IPL'], | |
| 'Wickets': [player_data['bowling_Test_Wickets'], player_data['bowling_ODI_Wickets'], player_data['bowling_T20_Wickets'], player_data['bowling_IPL_Wickets']], | |
| 'Average': [player_data['bowling_Test_Avg'], player_data['bowling_ODI_Avg'], player_data['bowling_T20_Avg'], player_data['bowling_IPL_Avg']], | |
| 'Economy': [player_data['bowling_Test_Eco'], player_data['bowling_ODI_Eco'], player_data['bowling_T20_Eco'], player_data['bowling_IPL_Eco']], | |
| 'Strike Rate': [player_data['bowling_Test_SR'], player_data['bowling_ODI_SR'], player_data['bowling_T20_SR'], player_data['bowling_IPL_SR']] | |
| } | |
| df = pd.DataFrame(stats) | |
| st.plotly_chart(px.bar(df, x='Format', y='Wickets', title=f"π³ {player_data['Player']} - Wickets Taken π", text='Wickets')) | |
| st.plotly_chart(px.bar(df, x='Format', y='Average', title=f"π {player_data['Player']} - Bowling Average π", text='Average')) | |
| st.plotly_chart(px.line(df, x='Format', y='Economy', title=f"π° {player_data['Player']} - Economy Rate π", markers=True, text='Economy')) | |
| st.plotly_chart(px.line(df, x='Format', y='Strike Rate', title=f"β‘ {player_data['Player']} - Bowling Strike Rate π―", markers=True, text='Strike Rate')) | |
| # Main Streamlit App | |
| def main(): | |
| st.title("πΈ Upload Two Player Images to Compare Stats") | |
| model = load_model() | |
| label_encoder = load_label_encoder() | |
| df = load_data() | |
| col1, col2 = st.columns(2) | |
| img1 = col1.file_uploader("Upload Image for Player 1", type=["jpg", "jpeg", "png"], key="img1") | |
| img2 = col2.file_uploader("Upload Image for Player 2", type=["jpg", "jpeg", "png"], key="img2") | |
| if img1 and img2: | |
| with st.spinner("Detecting and predicting players..."): | |
| pred1, err1 = detect_and_predict_face(img1, model, label_encoder) | |
| pred2, err2 = detect_and_predict_face(img2, model, label_encoder) | |
| if err1: st.error(f"Player 1: {err1}") | |
| if err2: st.error(f"Player 2: {err2}") | |
| if not err1 and not err2: | |
| st.success(f"β Predicted Players: {pred1} vs {pred2}") | |
| try: | |
| p1 = get_player_details(df, pred1) | |
| p2 = get_player_details(df, pred2) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.header(f"π {pred1}") | |
| plot_matches_pie(p1) | |
| plot_batting_stats(p1) | |
| plot_bowling_stats(p1) | |
| with col2: | |
| st.header(f"π {pred2}") | |
| plot_matches_pie(p2) | |
| plot_batting_stats(p2) | |
| plot_bowling_stats(p2) | |
| except Exception as e: | |
| st.error(f"β οΈ Player stats not found: {e}") | |
| if __name__ == "__main__": | |
| main() | |