CoolFace
Apppublic

juarismar/RPS-Tensorflow-Sequeros

sourceHugging Faceapache-2.0updated 6mo agoView on Hugging Face
1likes
app.py85 linesDownload Raw Back to root
1import gradio as gr
2import numpy as np
3import tensorflow as tf
4from RockPaperScissors2Player import(
5    Hand,
6    RockPaperScissors2Player as rps2p
7)
8from keras.models import Sequential, load_model
9
10print(f"Versión de Gradio: {gr.__version__}")
11
12
13# Entradas.
14input_image = gr.Image(
15    label = "Sube una imagen con tu jugada para vencer a la IA.",
16    sources = ["upload", "webcam", "clipboard"],
17    type = "numpy"
18)
19
20inputs = [input_image]
21
22
23# Salidas.
24output_textbox_user = gr.Textbox(
25    placeholder = "Piedra, papel o tijeras.",
26    label = "Tu jugada",
27)
28
29output_textbox_ai = gr.Textbox(
30    placeholder = "Piedra, papel o tijeras.",
31    label = "Jugada de la IA",
32)
33
34output_result = gr.Textbox(
35    placeholder = "Victoria, derrota o empate.",
36    label = "Resultado",
37)
38
39outputs = [
40    output_textbox_user,
41    output_textbox_ai,
42    output_result
43]
44
45
46# Inferencia.
47model: Sequential = load_model("rps_efficient_net_b0_tf.keras")
48
49def rpc(img: np.ndarray) -> tuple[str, str, str]:
50    # 1. Preprocesamiento
51    # EfficientNet espera (224, 224, 3) en el rango [0, 255] (float o int).
52    img_resized = tf.image.resize(img, (224, 224))
53    img_batch = np.expand_dims(img_resized, axis=0)
54
55    # 2. Predicción
56    y_pred = model.predict(img_batch)
57    user_hand = np.argmax(y_pred)
58    rps = rps2p(Hand(user_hand), None)
59    rps.play()
60        
61    return rps.hand1.display_name, rps.hand2.display_name, rps.get_result()
62
63
64# Interfaz.
65examples = [
66    ["sample_imgs/paper.png"],
67    ["sample_imgs/rock.png"],
68    ["sample_imgs/scissors.png"]
69]
70
71app = gr.Interface(
72    fn = rpc,
73    inputs = inputs,
74    outputs = outputs,
75    examples = examples,
76    title = "Piedra, papel, tijeras - JERM",
77    description = "Cargue una imagen de una mano mostrando piedra, papel o tijeras. O use la cámara web para jugar en tiempo real contra la IA.",
78    submit_btn = "Enviar",
79    clear_btn = "Borrar"
80)
81
82
83# Ejecución.
84if __name__ == "__main__":
85    app.launch()