Jiwoo77's picture
Update app.py
cab7682 verified
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.saving import load_model
import cv2
MODEL_PATH = "fer_surprise_softmax.keras"
# --------------------------
# 1) ๋ชจ๋ธ ๋กœ๋”ฉ
# --------------------------
print("๐Ÿ“Œ ๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘...")
model = load_model(MODEL_PATH, compile=False)
print("โœ… ๋ชจ๋ธ ๋กœ๋”ฉ ์™„๋ฃŒ!")
# --------------------------
# 2) ์˜ˆ์ธก ํ•จ์ˆ˜ (์‹ค์‹œ๊ฐ„ ์›น์บ )
# --------------------------
emotion_labels = ["angry", "disgust", "fear", "happy", "neutral", "sad", "surprise"]
def predict(frame, hr):
if frame is None:
return None, "์นด๋ฉ”๋ผ ํ”„๋ ˆ์ž„ ์—†์Œ"
# ์–ผ๊ตด ํƒ์ง€ (๋Œ€๋น„ ๊ฐ•ํ™” + grayscale)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
resized = cv2.resize(gray, (96, 96))
img = resized.reshape(1, 96, 96, 1).astype("float32") / 255.0
# ๋ชจ๋ธ ์˜ˆ์ธก
preds = model.predict(img, verbose=0)[0]
top_idx = int(np.argmax(preds))
top_label = emotion_labels[top_idx]
surprise_score = float(preds[6])
# Surprise ์—ฌ๋ถ€ binary
surprise_flag = "Surprised ๐Ÿ˜ฒ" if top_label == "surprise" else "Not Surprise ๐Ÿ™‚"
# ์ถœ๋ ฅ ๊ตฌ์„ฑ
result_text = (
f"๐Ÿง  Top Emotion: {top_label}\n"
f"๐Ÿ˜ฒ Surprise Probability: {surprise_score:.3f}\n"
f"โค๏ธ Heart Rate: {hr} BPM\n"
f"๐Ÿ“Œ Surprise Detection: {surprise_flag}"
)
return frame, result_text
# --------------------------
# 3) Gradio UI
# --------------------------
with gr.Blocks() as demo:
gr.Markdown("# ๐Ÿ˜ฒ Real-Time Surprise Detector + โค๏ธ Heart Rate Monitor")
with gr.Row():
webcam = gr.Image(
sources=["webcam"],
streaming=True,
label="Webcam Input",
height=380
)
hr = gr.Number(label="Heart Rate (from Arduino)", value=0)
out_image = gr.Image(label="Live Feed")
out_text = gr.Textbox(label="Analysis Result")
webcam.stream(predict, inputs=[webcam, hr], outputs=[out_image, out_text])
demo.launch()