Spaces:
Build error
Build error
| import gradio as gr | |
| import cv2 | |
| import mediapipe as mp | |
| import numpy as np | |
| import pandas as pd | |
| import tensorflow as tf | |
| import joblib | |
| from sklearn.preprocessing import LabelEncoder | |
| # ------------------ Load Model ------------------ | |
| model = tf.keras.models.load_model("model.h5") | |
| scaler = joblib.load("scaler.pkl") | |
| # ------------------ Load Labels ------------------ | |
| df = pd.read_csv( | |
| "keypoints_dataset+space.csv", | |
| low_memory=False | |
| ) | |
| encoder = LabelEncoder() | |
| encoder.fit(df["label"].astype(str)) | |
| # ------------------ Arabic Mapping ------------------ | |
| label_to_arabic = { | |
| "aleff": "ا", | |
| "bb": "ب", | |
| "ta": "ت", | |
| "thaa": "ث", | |
| "jeem": "ج", | |
| "haa": "ح", | |
| "khaa": "خ", | |
| "dal": "د", | |
| "thal": "ذ", | |
| "ra": "ر", | |
| "zay": "ز", | |
| "seen": "س", | |
| "sheen": "ش", | |
| "saad": "ص", | |
| "dhad": "ض", | |
| "taa": "ط", | |
| "dha": "ظ", | |
| "ain": "ع", | |
| "ghain": "غ", | |
| "fa": "ف", | |
| "gaaf": "ق", | |
| "kaaf": "ك", | |
| "laam": "ل", | |
| "meem": "م", | |
| "nun": "ن", | |
| "ha": "ه", | |
| "waw": "و", | |
| "yaa": "ي", | |
| "0": "0", | |
| "1": "1", | |
| "2": "2", | |
| "3": "3", | |
| "4": "4", | |
| "5": "5", | |
| "6": "6", | |
| "7": "7", | |
| "8": "8", | |
| "9": "9", | |
| "10": "10", | |
| "space": " " | |
| } | |
| # ------------------ MediaPipe ------------------ | |
| mp_hands = mp.solutions.hands | |
| hands = mp_hands.Hands( | |
| static_image_mode=True, | |
| max_num_hands=1, | |
| min_detection_confidence=0.5 | |
| ) | |
| # ------------------ Prediction Function ------------------ | |
| def predict(image): | |
| image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
| results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) | |
| if not results.multi_hand_landmarks: | |
| return "No hand detected" | |
| hand_landmarks = results.multi_hand_landmarks[0] | |
| landmarks = np.array([ | |
| [lm.x, lm.y, lm.z] | |
| for lm in hand_landmarks.landmark | |
| ]) | |
| landmarks = landmarks.flatten().reshape(1, -1) | |
| landmarks = scaler.transform(landmarks) | |
| prediction = model.predict(landmarks, verbose=0) | |
| predicted_class = np.argmax(prediction) | |
| predicted_label = encoder.inverse_transform( | |
| [predicted_class] | |
| )[0] | |
| arabic_output = label_to_arabic.get( | |
| predicted_label, | |
| predicted_label | |
| ) | |
| return f"Prediction: {arabic_output}" | |
| # ------------------ Gradio App ------------------ | |
| app = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="numpy"), | |
| outputs="text", | |
| title="Arabic Sign Language Recognition" | |
| ) | |
| app.launch() |