Download app.py from Ahmad140/face: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/Ahmad140/face/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ahmad140/face/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ahmad140/face/resolve/main/app.py
2.96 kB
| import os | |
| # !!! IMPORTANT !!! – hot‑reload crash ko rokne ke liye import se pehle set karein | |
| os.environ['GRADIO_HOT_RELOAD'] = 'False' | |
| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| import tensorflow as tf | |
| import gdown | |
| # ---------- 1. Model Download (agar drive link se lena hai) ---------- | |
| MODEL_FILE = "smile_model.h5" | |
| DRIVE_FILE_ID = "1_t26AY1cZPVsJNaf4f7UYj3Oy7yrA-BZ" | |
| if not os.path.exists(MODEL_FILE): | |
| print("⬇️ Downloading model from Google Drive...") | |
| url = f"https://drive.google.com/uc?id={DRIVE_FILE_ID}" | |
| gdown.download(url, MODEL_FILE, quiet=False) | |
| if not os.path.exists(MODEL_FILE): | |
| raise FileNotFoundError("❌ Model download fail. Drive link check karein.") | |
| print("✅ Download complete!") | |
| # ---------- 2. Model Load ---------- | |
| smile_model = tf.keras.models.load_model(MODEL_FILE) | |
| print("✅ Model loaded") | |
| # ---------- 3. Haar Cascade ---------- | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') | |
| # ---------- 4. Detection Function ---------- | |
| def detect_smile(frame, threshold): | |
| """ | |
| frame : RGB numpy array | |
| threshold : 0.0 – 1.0 (jitna zyada, utna strict smile detection) | |
| """ | |
| img = np.array(frame) | |
| bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY) | |
| faces = face_cascade.detectMultiScale(gray, 1.1, 5) | |
| for (x, y, w, h) in faces: | |
| face_roi = img[y:y+h, x:x+w] | |
| face_resized = cv2.resize(face_roi, (224, 224)) | |
| face_array = np.expand_dims(face_resized / 255.0, axis=0) | |
| prob = smile_model.predict(face_array, verbose=0)[0][0] | |
| label = 'Smile' if prob > threshold else 'No Smile' | |
| color = (0, 255, 0) if label == 'Smile' else (0, 0, 255) | |
| cv2.rectangle(bgr, (x, y), (x+w, y+h), color, 2) | |
| cv2.putText(bgr, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2) | |
| return cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB) | |
| # ---------- 5. Gradio Blocks (Live Stream + Slider) ---------- | |
| with gr.Blocks(title="Live Smile Detector") as demo: | |
| gr.Markdown("# 😊 Real‑Time Smile Detection") | |
| gr.Markdown("Allow camera. Green box = Smile, Red box = No Smile.") | |
| with gr.Row(): | |
| webcam = gr.Image(sources=["webcam"], streaming=True, label="Live Webcam") | |
| result = gr.Image(label="Detection Result", streaming=True) | |
| threshold = gr.Slider( | |
| minimum=0.1, maximum=0.99, value=0.7, step=0.05, | |
| label="Smile Confidence Threshold (jyada = strict)" | |
| ) | |
| # Har frame ke liye detection function chalega | |
| webcam.stream( | |
| fn=detect_smile, | |
| inputs=[webcam, threshold], | |
| outputs=result, | |
| time_limit=0, | |
| concurrency_limit=4 | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| debug=False, | |
| ssr_mode=False # Server‑side rendering band – flicker nahi hoga | |
| ) |