Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,23 +11,28 @@ t1_time = 0
|
|
11 |
t2_time = 0
|
12 |
start_time = None
|
13 |
active_team = None
|
14 |
-
|
15 |
team1_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
16 |
team2_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
17 |
-
|
18 |
t1_name = "Equipo 1"
|
19 |
t2_name = "Equipo 2"
|
20 |
t1_logo = None
|
21 |
t2_logo = None
|
22 |
-
|
23 |
historial = []
|
|
|
24 |
|
25 |
# Cronómetro tipo ajedrez
|
26 |
def start_possession(team):
|
27 |
global start_time, active_team
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
start_time = time.time()
|
29 |
active_team = team
|
30 |
-
|
|
|
31 |
|
32 |
def switch_possession():
|
33 |
global t1_time, t2_time, start_time, active_team
|
@@ -39,6 +44,126 @@ def switch_possession():
|
|
39 |
t2_time += elapsed
|
40 |
start_time = time.time()
|
41 |
active_team = t1_name if active_team == t2_name else t2_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return f"Ahora tiene la posesión: {active_team}"
|
43 |
|
44 |
def show_stats():
|
|
|
11 |
t2_time = 0
|
12 |
start_time = None
|
13 |
active_team = None
|
|
|
14 |
team1_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
15 |
team2_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
|
|
16 |
t1_name = "Equipo 1"
|
17 |
t2_name = "Equipo 2"
|
18 |
t1_logo = None
|
19 |
t2_logo = None
|
|
|
20 |
historial = []
|
21 |
+
cronometro = "00:00"
|
22 |
|
23 |
# Cronómetro tipo ajedrez
|
24 |
def start_possession(team):
|
25 |
global start_time, active_team
|
26 |
+
if active_team and start_time:
|
27 |
+
elapsed = time.time() - start_time
|
28 |
+
if active_team == t1_name:
|
29 |
+
globals()['t1_time'] += elapsed
|
30 |
+
elif active_team == t2_name:
|
31 |
+
globals()['t2_time'] += elapsed
|
32 |
start_time = time.time()
|
33 |
active_team = team
|
34 |
+
historial.append(f"{team} inicia posesión")
|
35 |
+
return update_summary()
|
36 |
|
37 |
def switch_possession():
|
38 |
global t1_time, t2_time, start_time, active_team
|
|
|
44 |
t2_time += elapsed
|
45 |
start_time = time.time()
|
46 |
active_team = t1_name if active_team == t2_name else t2_name
|
47 |
+
historial.append(f"Cambio de posesión: {active_team}")
|
48 |
+
return update_summary()
|
49 |
+
|
50 |
+
def show_stats():
|
51 |
+
if active_team and start_time:
|
52 |
+
elapsed = time.time() - start_time
|
53 |
+
temp_t1 = t1_time + (elapsed if active_team == t1_name else 0)
|
54 |
+
temp_t2 = t2_time + (elapsed if active_team == t2_name else 0)
|
55 |
+
else:
|
56 |
+
temp_t1 = t1_time
|
57 |
+
temp_t2 = t2_time
|
58 |
+
total = temp_t1 + temp_t2
|
59 |
+
p1 = round((temp_t1 / total) * 100, 2) if total > 0 else 0
|
60 |
+
p2 = 100 - p1 if total > 0 else 0
|
61 |
+
mins = int(total) // 60
|
62 |
+
secs = int(total) % 60
|
63 |
+
globals()['cronometro'] = f"{mins:02}:{secs:02}"
|
64 |
+
return f"{t1_name}: {p1}%", f"{t2_name}: {p2}%", generate_possession_bar(p1, p2), cronometro
|
65 |
+
|
66 |
+
def add_stat(team, stat):
|
67 |
+
if team == "1":
|
68 |
+
team1_stats[stat] += 1
|
69 |
+
historial.append(f"{t1_name} - {stat.upper()} +1")
|
70 |
+
else:
|
71 |
+
team2_stats[stat] += 1
|
72 |
+
historial.append(f"{t2_name} - {stat.upper()} +1")
|
73 |
+
return update_summary()
|
74 |
+
|
75 |
+
def update_summary():
|
76 |
+
sum1 = f"{t1_name} - 🟨 {team1_stats['amarillas']} | 🔵 {team1_stats['rojas']} | ⚽ {team1_stats['corners']}"
|
77 |
+
sum2 = f"{t2_name} - 🟨 {team2_stats['amarillas']} | 🔵 {team2_stats['rojas']} | ⚽ {team2_stats['corners']}"
|
78 |
+
return sum1, sum2, "\n".join(historial)
|
79 |
+
|
80 |
+
def set_team_info(name1, name2, logo1, logo2):
|
81 |
+
global t1_name, t2_name, t1_logo, t2_logo
|
82 |
+
t1_name = name1
|
83 |
+
t2_name = name2
|
84 |
+
t1_logo = logo1
|
85 |
+
t2_logo = logo2
|
86 |
+
return update_summary()
|
87 |
+
|
88 |
+
def generate_possession_bar(p1, p2):
|
89 |
+
bar = Image.new('RGB', (400, 40), color='black')
|
90 |
+
draw = ImageDraw.Draw(bar)
|
91 |
+
draw.rectangle([0, 0, int(p1 * 4), 40], fill='blue')
|
92 |
+
draw.rectangle([int(p1 * 4), 0, 400, 40], fill='red')
|
93 |
+
return bar
|
94 |
+
|
95 |
+
def export_image():
|
96 |
+
img = Image.new('RGB', (800, 400), color='#001845')
|
97 |
+
draw = ImageDraw.Draw(img)
|
98 |
+
font = ImageFont.load_default()
|
99 |
+
|
100 |
+
draw.text((20, 20), f"{t1_name} vs {t2_name}", font=font, fill='white')
|
101 |
+
draw.text((20, 50), f"🟨 {team1_stats['amarillas']} 🔵 {team1_stats['rojas']} ⚽ {team1_stats['corners']}", font=font, fill='blue')
|
102 |
+
draw.text((20, 70), f"🟨 {team2_stats['amarillas']} 🔵 {team2_stats['rojas']} ⚽ {team2_stats['corners']}", font=font, fill='red')
|
103 |
+
|
104 |
+
p1_out, p2_out, _, tiempo = show_stats()
|
105 |
+
draw.text((20, 120), f"Posesión: {p1_out} - {p2_out}", font=font, fill='white')
|
106 |
+
draw.text((20, 140), f"Tiempo total: {tiempo}", font=font, fill='white')
|
107 |
+
draw.text((20, 160), "Historial:", font=font, fill='yellow')
|
108 |
+
|
109 |
+
for i, ev in enumerate(historial[-10:]):
|
110 |
+
draw.text((40, 180 + i * 20), ev, font=font, fill='white')
|
111 |
+
|
112 |
+
return img
|
113 |
+
|
114 |
+
with gr.Blocks(css="""
|
115 |
+
body { background-color: #001845; color: white; font-family: sans-serif; }
|
116 |
+
button, .gr-button { background-color: #003566; color: white; border-radius: 8px; font-size: 16px; padding: 10px; }
|
117 |
+
input, textarea, .gr-textbox { border-radius: 8px; font-size: 16px; }
|
118 |
+
.image-preview, .gr-image { border-radius: 8px; border: 2px solid #003566; }
|
119 |
+
.textbox-label, .gr-textbox label { color: white; }
|
120 |
+
""") as demo:
|
121 |
+
gr.Markdown("## 🏆 Medidor de Posesión y Estadísticas Estilo Champions League")
|
122 |
+
|
123 |
+
with gr.Row():
|
124 |
+
name1 = gr.Textbox(value="Equipo 1", label="Nombre Equipo 1")
|
125 |
+
name2 = gr.Textbox(value="Equipo 2", label="Nombre Equipo 2")
|
126 |
+
logo1 = gr.Image(type="pil", label="Escudo 1")
|
127 |
+
logo2 = gr.Image(type="pil", label="Escudo 2")
|
128 |
+
gr.Button("✅ Establecer Equipos").click(set_team_info, inputs=[name1, name2, logo1, logo2], outputs=[])
|
129 |
+
|
130 |
+
resumen1 = gr.Textbox(label="Resumen Equipo 1")
|
131 |
+
resumen2 = gr.Textbox(label="Resumen Equipo 2")
|
132 |
+
log_box = gr.Textbox(label="Historial del partido", lines=10)
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
gr.Button("🕒 Iniciar Posesión Equipo 1").click(lambda: start_possession(t1_name), outputs=[resumen1, resumen2, log_box])
|
136 |
+
gr.Button("🕒 Iniciar Posesión Equipo 2").click(lambda: start_possession(t2_name), outputs=[resumen1, resumen2, log_box])
|
137 |
+
gr.Button("🔁 Cambiar Posesión").click(switch_possession, outputs=[resumen1, resumen2, log_box])
|
138 |
+
|
139 |
+
with gr.Row():
|
140 |
+
btn_stats = gr.Button("📊 Mostrar posesión")
|
141 |
+
p1_out = gr.Textbox(label="Posesión Equipo 1")
|
142 |
+
p2_out = gr.Textbox(label="Posesión Equipo 2")
|
143 |
+
bar = gr.Image(label="Barra 3D de posesión")
|
144 |
+
tiempo_total = gr.Textbox(label="⏱ Tiempo total")
|
145 |
+
btn_stats.click(show_stats, outputs=[p1_out, p2_out, bar, tiempo_total])
|
146 |
+
|
147 |
+
with gr.Row():
|
148 |
+
gr.Button("🟨 Amarilla Equipo 1").click(lambda: add_stat("1", "amarillas"), outputs=[resumen1, resumen2, log_box])
|
149 |
+
gr.Button("🔵 Roja Equipo 1").click(lambda: add_stat("1", "rojas"), outputs=[resumen1, resumen2, log_box])
|
150 |
+
gr.Button("⚽ Córner Equipo 1").click(lambda: add_stat("1", "corners"), outputs=[resumen1, resumen2, log_box])
|
151 |
+
|
152 |
+
with gr.Row():
|
153 |
+
gr.Button("🟨 Amarilla Equipo 2").click(lambda: add_stat("2", "amarillas"), outputs=[resumen1, resumen2, log_box])
|
154 |
+
gr.Button("🔵 Roja Equipo 2").click(lambda: add_stat("2", "rojas"), outputs=[resumen1, resumen2, log_box])
|
155 |
+
gr.Button("⚽ Córner Equipo 2").click(lambda: add_stat("2", "corners"), outputs=[resumen1, resumen2, log_box])
|
156 |
+
|
157 |
+
demo.load(update_summary, outputs=[resumen1, resumen2, log_box])
|
158 |
+
gr.Button("🖼 Exportar Imagen Final").click(export_image, outputs=gr.Image(type="pil"))
|
159 |
+
|
160 |
+
if __name__ == "__main__":
|
161 |
+
demo.launch()
|
162 |
+
t1_time += elapsed
|
163 |
+
elif active_team == t2_name:
|
164 |
+
t2_time += elapsed
|
165 |
+
start_time = time.time()
|
166 |
+
active_team = t1_name if active_team == t2_name else t2_name
|
167 |
return f"Ahora tiene la posesión: {active_team}"
|
168 |
|
169 |
def show_stats():
|