fest / app.py
Ahmad140's picture
Create app.py
61c9c7d verified
Raw History Blame Contribute Delete
5.19 kB
import gradio as gr
import cv2
import numpy as np
import tensorflow as tf
import face_recognition
# =========================================================================
# 1. Load Trained CNN Model & Haar Cascade
# =========================================================================
print("Loading Model...")
try:
smile_model = tf.keras.models.load_model('smile_model.h5')
print("βœ… Model loaded successfully!")
except Exception as e:
print(f"❌ Error loading model: {e}. Please ensure 'smile_model.h5' is uploaded.")
smile_model = None
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# =========================================================================
# 2. Helper Functions (Exactly from your Colab)
# =========================================================================
def mouth_aspect_ratio(landmarks):
left = np.array(landmarks['top_lip'][0])
right = np.array(landmarks['top_lip'][6])
upper = np.array(landmarks['top_lip'][3])
lower = np.array(landmarks['bottom_lip'][3])
# calculate MAR
return np.linalg.norm(right-left) / (np.linalg.norm(lower-upper) + 1e-6)
# =========================================================================
# 3. Main Processing Function
# =========================================================================
def detect_smile(image, method, cnn_threshold, mar_threshold):
if image is None:
return None, "No image provided."
# Gradio provides image as RGB numpy array
frame = image.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
smile_count = 0
total_faces = len(faces)
if method == "CNN Model (from .h5)":
if smile_model is None:
return frame, "Error: CNN Model not loaded."
for (x, y, w, h) in faces:
face_roi = frame[y:y+h, x:x+w]
face_resized = cv2.resize(face_roi, (224, 224))
face_arr = np.expand_dims(face_resized / 255.0, axis=0).astype(np.float32)
prob = smile_model.predict(face_arr, verbose=0)[0][0]
label = 'Smile' if prob > cnn_threshold else 'No Smile'
color = (0, 255, 0) if label == 'Smile' else (255, 0, 0) # Gradio Uses RGB, so Red is (255,0,0)
if label == 'Smile': smile_count += 1
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
cv2.putText(frame, f"{label} ({prob:.2f})", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
elif method == "Mouth Aspect Ratio (MAR)":
# Using face_recognition library as in Cell 13
landmarks_list = face_recognition.face_landmarks(frame)
for (x, y, w, h) in faces:
ratio = 0.0
is_smile = False
for lm in landmarks_list:
# Check if this landmark belongs to the current face bounding box
nose = np.array(lm['nose_tip'][0])
if x <= nose[0] <= x+w and y <= nose[1] <= y+h:
ratio = mouth_aspect_ratio(lm)
is_smile = ratio > mar_threshold
break
label = 'Smile' if is_smile else 'No Smile'
color = (0, 255, 0) if is_smile else (255, 0, 0)
if is_smile: smile_count += 1
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
cv2.putText(frame, f"{label} (r={ratio:.1f})", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
status = f"Total Faces Detected: {total_faces} | Smiles Detected: {smile_count}"
return frame, status
# =========================================================================
# 4. Gradio Interface Layout
# =========================================================================
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# πŸ˜ƒ Smile Detector Pro")
gr.Markdown("Upload an image or use your webcam to detect smiles. Choose between your Custom Trained CNN or the Mouth Aspect Ratio (MAR) method.")
with gr.Row():
with gr.Column():
input_image = gr.Image(sources=["upload", "webcam"], type="numpy", label="Input Image / Live Camera")
method_radio = gr.Radio(["CNN Model (from .h5)", "Mouth Aspect Ratio (MAR)"], value="CNN Model (from .h5)", label="Detection Method")
cnn_thresh = gr.Slider(0.0, 1.0, value=0.85, step=0.01, label="CNN Threshold", info="Default: 0.85 (Cell 11)")
mar_thresh = gr.Slider(1.0, 5.0, value=3.5, step=0.1, label="MAR Threshold", info="Default: 3.5 (Cell 13)")
submit_btn = gr.Button("πŸ” Detect Smile", variant="primary")
with gr.Column():
output_image = gr.Image(type="numpy", label="Result")
output_text = gr.Textbox(label="Status")
submit_btn.click(
fn=detect_smile,
inputs=[input_image, method_radio, cnn_thresh, mar_thresh],
outputs=[output_image, output_text]
)
# Run the app
demo.launch()