AliNajdawi commited on
Commit
c452bc0
·
verified ·
1 Parent(s): 7be8847

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import cv2
3
+ import mediapipe as mp
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from collections import deque
7
+ import gradio as gr
8
+
9
+ # Constants (fallback defaults)
10
+ SEQUENCE_LENGTH = 30
11
+ FEATURE_LENGTH = 168
12
+
13
+ # Load model & preprocessing
14
+ def load_model_and_preprocessing():
15
+ global scaler, label_encoder, model, labels_dict
16
+ with open('lstm_preprocessing.pickle', 'rb') as f:
17
+ preproc = pickle.load(f)
18
+ scaler = preproc['scaler']
19
+ label_encoder = preproc['label_encoder']
20
+ timesteps = preproc['timesteps']
21
+ n_features = preproc['n_features']
22
+
23
+ global SEQUENCE_LENGTH, FEATURE_LENGTH
24
+ SEQUENCE_LENGTH = timesteps
25
+ FEATURE_LENGTH = n_features
26
+
27
+ model = tf.keras.models.load_model('lstm_model.h5')
28
+
29
+ labels_dict = {0:'salam',1:'good morning',2:'thanks',3:'ana',4:'anta',5:'ante',6:'hua',
30
+ 7:'hea',8:'antm',9:'hm',10:'name',11:'how r u',12:'thanks god',13:'happy',
31
+ 14:'sad',15:'angry',16:'good',17:'bad',18:'tired',19:'sick',20:'see',21:'say',
32
+ 22:'talk',23:'walk',24:'went',25:'came',26:'home',27:'eat',28:'slept',29:'university',
33
+ 30:'today',31:'tmrw',32:'sunday',33:'tuesday',34:'thursday',35:'friday',36:'week',
34
+ 37:'month',38:'year',39:'when',40:'I know',41:'thinking',42:'forgetten',43:'love',
35
+ 44:'I want',45:'helps',46:'not allowed',47:'agree',48:'together',49:'different'}
36
+
37
+ load_model_and_preprocessing()
38
+
39
+ # MediaPipe setup
40
+ mp_hands = mp.solutions.hands
41
+ hands = mp_hands.Hands(static_image_mode=True, max_num_hands=2)
42
+
43
+ def extract_hand_features(image):
44
+ data_aux = np.zeros(FEATURE_LENGTH, dtype=np.float32)
45
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
46
+ results = hands.process(image_rgb)
47
+ if not results.multi_hand_landmarks:
48
+ return None
49
+
50
+ for hand_idx, hand_landmarks in enumerate(results.multi_hand_landmarks):
51
+ if hand_idx >= 2: break
52
+ min_x = min([lm.x for lm in hand_landmarks.landmark])
53
+ min_y = min([lm.y for lm in hand_landmarks.landmark])
54
+ base_idx = hand_idx * 84
55
+ for i, lm in enumerate(hand_landmarks.landmark):
56
+ data_aux[base_idx + i * 2] = lm.x - min_x
57
+ data_aux[base_idx + i * 2 + 1] = lm.y - min_y
58
+
59
+ return data_aux
60
+
61
+ sequence_buffer = deque(maxlen=SEQUENCE_LENGTH)
62
+
63
+ def predict_sign(image):
64
+ image = np.array(image)
65
+ features = extract_hand_features(image)
66
+ if features is None:
67
+ return "No hand detected"
68
+ sequence_buffer.append(features)
69
+
70
+ if len(sequence_buffer) < SEQUENCE_LENGTH:
71
+ return f"Waiting for sequence: {len(sequence_buffer)}/{SEQUENCE_LENGTH}"
72
+
73
+ sequence_data = np.array(list(sequence_buffer)).reshape(1, SEQUENCE_LENGTH, FEATURE_LENGTH)
74
+ scaled = scaler.transform(sequence_data.reshape(-1, FEATURE_LENGTH)).reshape(1, SEQUENCE_LENGTH, FEATURE_LENGTH)
75
+ prediction_scores = model.predict(scaled, verbose=0)[0]
76
+ predicted_idx = np.argmax(prediction_scores)
77
+ confidence = prediction_scores[predicted_idx]
78
+ label = labels_dict.get(predicted_idx, "Unknown")
79
+
80
+ return f"{label} ({confidence:.2f})"
81
+
82
+ # Gradio interface
83
+ interface = gr.Interface(fn=predict_sign,
84
+ inputs=gr.Image(source="webcam", tool="editor", type="numpy"),
85
+ outputs="text",
86
+ title="Sign Language Recognition",
87
+ description="Perform a sign and capture it. Wait for prediction...")
88
+
89
+ # ✅ LAUNCH THE APP
90
+ interface.launch()