Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import time
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
6 |
start_time = None
|
7 |
active_team = None
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
|
|
|
|
|
|
15 |
def start_possession(team):
|
16 |
global start_time, active_team
|
17 |
start_time = time.time()
|
@@ -19,38 +26,111 @@ def start_possession(team):
|
|
19 |
return f"Posesión para: {team}"
|
20 |
|
21 |
def switch_possession():
|
22 |
-
global
|
23 |
if active_team and start_time:
|
24 |
elapsed = time.time() - start_time
|
25 |
-
if active_team ==
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
start_time = time.time()
|
30 |
-
active_team =
|
31 |
return f"Ahora tiene la posesión: {active_team}"
|
32 |
|
33 |
def show_stats():
|
34 |
-
total =
|
35 |
-
p1 = round((
|
36 |
p2 = 100 - p1 if total > 0 else 0
|
37 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
with gr.Blocks() as demo:
|
40 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
btn_stats = gr.Button("Mostrar Estadísticas")
|
46 |
|
47 |
-
|
48 |
-
stats1 = gr.Textbox(label="Equipo 1")
|
49 |
-
stats2 = gr.Textbox(label="Equipo 2")
|
50 |
|
51 |
-
|
52 |
-
btn2.click(start_possession_equipo2, outputs=out)
|
53 |
-
btn_switch.click(switch_possession, outputs=out)
|
54 |
-
btn_stats.click(show_stats, outputs=[stats1, stats2])
|
55 |
|
56 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import time
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
|
5 |
+
# Variables globales
|
6 |
+
t1_time = 0
|
7 |
+
t2_time = 0
|
8 |
start_time = None
|
9 |
active_team = None
|
10 |
|
11 |
+
team1_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
12 |
+
team2_stats = {"amarillas": 0, "rojas": 0, "corners": 0}
|
13 |
|
14 |
+
t1_name = "Equipo 1"
|
15 |
+
t2_name = "Equipo 2"
|
16 |
+
t1_logo = None
|
17 |
+
t2_logo = None
|
18 |
|
19 |
+
historial = []
|
20 |
+
|
21 |
+
# Cronómetro tipo ajedrez
|
22 |
def start_possession(team):
|
23 |
global start_time, active_team
|
24 |
start_time = time.time()
|
|
|
26 |
return f"Posesión para: {team}"
|
27 |
|
28 |
def switch_possession():
|
29 |
+
global t1_time, t2_time, start_time, active_team
|
30 |
if active_team and start_time:
|
31 |
elapsed = time.time() - start_time
|
32 |
+
if active_team == t1_name:
|
33 |
+
t1_time += elapsed
|
34 |
+
elif active_team == t2_name:
|
35 |
+
t2_time += elapsed
|
36 |
start_time = time.time()
|
37 |
+
active_team = t1_name if active_team == t2_name else t2_name
|
38 |
return f"Ahora tiene la posesión: {active_team}"
|
39 |
|
40 |
def show_stats():
|
41 |
+
total = t1_time + t2_time
|
42 |
+
p1 = round((t1_time / total) * 100, 2) if total > 0 else 0
|
43 |
p2 = 100 - p1 if total > 0 else 0
|
44 |
+
return f"{t1_name}: {p1}%", f"{t2_name}: {p2}%", generate_possession_bar(p1, p2)
|
45 |
+
|
46 |
+
def add_stat(team, stat):
|
47 |
+
if team == "1":
|
48 |
+
team1_stats[stat] += 1
|
49 |
+
historial.append(f"{t1_name} - {stat.upper()} +1")
|
50 |
+
else:
|
51 |
+
team2_stats[stat] += 1
|
52 |
+
historial.append(f"{t2_name} - {stat.upper()} +1")
|
53 |
+
return update_summary()
|
54 |
+
|
55 |
+
def update_summary():
|
56 |
+
sum1 = f"{t1_name} - 🟨 {team1_stats['amarillas']} | 🟥 {team1_stats['rojas']} | ⚽ {team1_stats['corners']}"
|
57 |
+
sum2 = f"{t2_name} - 🟨 {team2_stats['amarillas']} | 🟥 {team2_stats['rojas']} | ⚽ {team2_stats['corners']}"
|
58 |
+
return sum1, sum2, "\n".join(historial)
|
59 |
+
|
60 |
+
def set_team_info(name1, name2, logo1, logo2):
|
61 |
+
global t1_name, t2_name, t1_logo, t2_logo
|
62 |
+
t1_name = name1
|
63 |
+
t2_name = name2
|
64 |
+
t1_logo = logo1
|
65 |
+
t2_logo = logo2
|
66 |
+
return update_summary()
|
67 |
+
|
68 |
+
def generate_possession_bar(p1, p2):
|
69 |
+
bar = Image.new('RGB', (400, 40), color='black')
|
70 |
+
draw = ImageDraw.Draw(bar)
|
71 |
+
draw.rectangle([0, 0, int(p1 * 4), 40], fill='blue')
|
72 |
+
draw.rectangle([int(p1 * 4), 0, 400, 40], fill='red')
|
73 |
+
return bar
|
74 |
+
|
75 |
+
def export_image():
|
76 |
+
img = Image.new('RGB', (800, 400), color='#001845')
|
77 |
+
draw = ImageDraw.Draw(img)
|
78 |
+
font = ImageFont.load_default()
|
79 |
+
|
80 |
+
draw.text((20, 20), f"{t1_name} vs {t2_name}", font=font, fill='white')
|
81 |
+
draw.text((20, 50), f"🟨 {team1_stats['amarillas']} 🟥 {team1_stats['rojas']} ⚽ {team1_stats['corners']}", font=font, fill='blue')
|
82 |
+
draw.text((20, 70), f"🟨 {team2_stats['amarillas']} 🟥 {team2_stats['rojas']} ⚽ {team2_stats['corners']}", font=font, fill='red')
|
83 |
+
|
84 |
+
p1 = round((t1_time / (t1_time + t2_time)) * 100, 2) if (t1_time + t2_time) > 0 else 0
|
85 |
+
p2 = 100 - p1
|
86 |
+
|
87 |
+
draw.text((20, 120), f"Posesión: {t1_name} {p1}% - {t2_name} {p2}%", font=font, fill='white')
|
88 |
+
draw.text((20, 160), "Historial:", font=font, fill='yellow')
|
89 |
+
|
90 |
+
for i, ev in enumerate(historial[-10:]):
|
91 |
+
draw.text((40, 180 + i * 20), ev, font=font, fill='white')
|
92 |
+
|
93 |
+
return img
|
94 |
|
95 |
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("## 🏆 Medidor de Posesión y Estadísticas Estilo Champions League")
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
name1 = gr.Textbox(value="Equipo 1", label="Nombre Equipo 1")
|
100 |
+
name2 = gr.Textbox(value="Equipo 2", label="Nombre Equipo 2")
|
101 |
+
logo1 = gr.Image(type="pil", label="Escudo 1")
|
102 |
+
logo2 = gr.Image(type="pil", label="Escudo 2")
|
103 |
+
gr.Button("✅ Establecer Equipos").click(set_team_info, inputs=[name1, name2, logo1, logo2], outputs=[])
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
gr.Button("🕒 Iniciar Posesión Equipo 1").click(lambda: start_possession(t1_name), outputs=[])
|
107 |
+
gr.Button("🕒 Iniciar Posesión Equipo 2").click(lambda: start_possession(t2_name), outputs=[])
|
108 |
+
gr.Button("🔁 Cambiar Posesión").click(switch_possession, outputs=[])
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
btn_stats = gr.Button("📊 Mostrar posesión")
|
112 |
+
p1_out = gr.Textbox(label="Posesión Equipo 1")
|
113 |
+
p2_out = gr.Textbox(label="Posesión Equipo 2")
|
114 |
+
bar = gr.Image(label="Barra 3D de posesión")
|
115 |
+
btn_stats.click(show_stats, outputs=[p1_out, p2_out, bar])
|
116 |
+
|
117 |
+
with gr.Row():
|
118 |
+
gr.Button("🟨 Amarilla Equipo 1").click(lambda: add_stat("1", "amarillas"), outputs=[])
|
119 |
+
gr.Button("🟥 Roja Equipo 1").click(lambda: add_stat("1", "rojas"), outputs=[])
|
120 |
+
gr.Button("⚽ Córner Equipo 1").click(lambda: add_stat("1", "corners"), outputs=[])
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
gr.Button("🟨 Amarilla Equipo 2").click(lambda: add_stat("2", "amarillas"), outputs=[])
|
124 |
+
gr.Button("🟥 Roja Equipo 2").click(lambda: add_stat("2", "rojas"), outputs=[])
|
125 |
+
gr.Button("⚽ Córner Equipo 2").click(lambda: add_stat("2", "corners"), outputs=[])
|
126 |
|
127 |
+
resumen1 = gr.Textbox(label="Resumen Equipo 1")
|
128 |
+
resumen2 = gr.Textbox(label="Resumen Equipo 2")
|
129 |
+
log_box = gr.Textbox(label="Historial del partido", lines=10)
|
|
|
130 |
|
131 |
+
demo.load(update_summary, outputs=[resumen1, resumen2, log_box])
|
|
|
|
|
132 |
|
133 |
+
gr.Button("🖼 Exportar Imagen Final").click(export_image, outputs=gr.Image(type="pil"))
|
|
|
|
|
|
|
134 |
|
135 |
+
if __name__ == "__main__":
|
136 |
+
demo.launch()
|