Spaces:
Sleeping
Sleeping
File size: 6,141 Bytes
6480008 b3a317f 6480008 b3a317f 6480008 562dbfd b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 de2a513 6480008 b3a317f a19d8e1 b3a317f 48127bc 63085e6 b3a317f 2b44bba 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 6480008 b3a317f 8a58a4b 6480008 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 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
@st.cache_resource
def load_model():
return joblib.load("svc_face_classifier (1).pkl")
@st.cache_resource
def load_label_encoder():
return joblib.load("label_encoder.pkl")
@st.cache_data
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()
|