Asmitha3 commited on
Commit
8a6bcad
·
verified ·
1 Parent(s): 611063b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -61
app.py CHANGED
@@ -1,61 +1,90 @@
1
- import gradio as gr
2
- import tensorflow as tf
3
- import numpy as np
4
-
5
- # --- 1. Model Loading and Classes Configuration ---
6
- try:
7
- # Load the Model from the /tmp path specified in the Dockerfile
8
- # NOTE: You MUST fix the TensorFlow version in the Dockerfile for this line to work.
9
- model = tf.keras.models.load_model('/tmp/sign_language_model.h5')
10
- except Exception as e:
11
- print(f"Error loading model: {e}")
12
- model = None
13
-
14
- # Your Specific Sign Language Classes (Order MUST match your training labels!)
15
- SIGN_CLASSES = ["HELLO", "GOOD BYE", "THANKYOU", "PLEASE", "YES", "NO", "SEE YOU", "LOOK", "FOOD", "SORRY", "HELP", "LOVE", "FRIEND", "NAME", "ME"]
16
-
17
- # --- 2. The Real-Time Prediction Function ---
18
- def classify_sign(input_image_data):
19
- """Processes a single frame from the live webcam feed."""
20
- if model is None or input_image_data is None:
21
- return "Model Loading or Camera Feed Not Active..."
22
-
23
- # Preprocessing (Adjust these dimensions/normalization to match your model's training)
24
- image_resized = tf.image.resize(input_image_data, (64, 64))
25
- image_normalized = image_resized / 255.0
26
-
27
- # Ensure the image shape is correct (e.g., convert to grayscale if needed)
28
- if image_normalized.shape[-1] == 3:
29
- image_normalized = tf.image.rgb_to_grayscale(image_normalized)
30
-
31
- input_tensor = np.expand_dims(image_normalized, axis=0)
32
-
33
- # Make Prediction
34
- predictions = model.predict(input_tensor)[0]
35
-
36
- # Find the best prediction
37
- predicted_index = np.argmax(predictions)
38
- predicted_sign = SIGN_CLASSES[predicted_index]
39
- confidence = predictions[predicted_index] * 100
40
-
41
- # Return the result string
42
- return f"PREDICTED SIGN: {predicted_sign} | Confidence: {confidence:.2f}%"
43
-
44
-
45
- # --- 3. The Gradio Interface for Continuous Streaming ---
46
- gr.Interface(
47
- fn=classify_sign,
48
- inputs=gr.Image(
49
- sources=['webcam'],
50
- type="numpy",
51
- shape=(300, 300),
52
- label="Live Sign Camera"
53
- ),
54
- outputs=gr.Textbox(label="Real-Time Translation"),
55
- live=True,
56
- title="Real-Time Sign Language Translator",
57
- description="Show your sign in front of the camera, and the prediction will update instantly.",
58
- theme="soft",
59
- # FIX for PermissionError: Disables creation of the 'flagged' folder
60
- allow_flagging=False
61
- ).launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import os
5
+
6
+ # --- 1. Model Loading and Classes Configuration ---
7
+ interpreter = None
8
+ model_loaded = False
9
+ input_details = None
10
+ output_details = None
11
+ MODEL_PATH = '/tmp/sign_language_model_lite.tflite'
12
+
13
+ try:
14
+ if os.path.exists(MODEL_PATH):
15
+ # Load the TFLite model file instead of the heavy H5 file
16
+ interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
17
+ interpreter.allocate_tensors()
18
+
19
+ input_details = interpreter.get_input_details()
20
+ output_details = interpreter.get_output_details()
21
+ model_loaded = True
22
+ print("SUCCESS: TFLite Model loaded successfully.")
23
+ else:
24
+ print(f"ERROR: Model file not found at {MODEL_PATH}")
25
+
26
+ except Exception as e:
27
+ print(f"FATAL ERROR: Failed to initialize TFLite interpreter: {e}")
28
+ model_loaded = False
29
+
30
+ # Your Specific Sign Language Classes (Order MUST match your training labels!)
31
+ SIGN_CLASSES = ["HELLO", "GOOD BYE", "THANKYOU", "PLEASE", "YES", "NO", "SEE YOU", "LOOK", "FOOD", "SORRY", "HELP", "LOVE", "FRIEND", "NAME", "ME"]
32
+
33
+ # --- 2. The Real-Time Prediction Function (Updated for TFLite) ---
34
+ def classify_sign(input_image_data):
35
+ """Processes a single frame from the live webcam feed using the TFLite interpreter."""
36
+
37
+ if not model_loaded or input_image_data is None:
38
+ return "Model Loading Error or Camera Feed Not Active..."
39
+
40
+ # 1. Preprocessing (adjust to model's input: 64x64 grayscale, required for the model)
41
+ image_resized = tf.image.resize(input_image_data, (64, 64))
42
+ image_normalized = image_resized / 255.0
43
+
44
+ # Convert to grayscale if the input is color (shape[3] == 3)
45
+ if image_normalized.shape[-1] == 3:
46
+ image_normalized = tf.image.rgb_to_grayscale(image_normalized)
47
+
48
+ # Add batch dimension (1, 64, 64, 1)
49
+ input_tensor = np.expand_dims(image_normalized, axis=0)
50
+
51
+ # 2. TFLite Prediction Logic
52
+ try:
53
+ # Set the input tensor
54
+ interpreter.set_tensor(input_details[0]['index'], input_tensor.numpy().astype(np.float32))
55
+
56
+ # Invoke the model
57
+ interpreter.invoke()
58
+
59
+ # Get prediction results
60
+ predictions = interpreter.get_tensor(output_details[0]['index'])[0]
61
+
62
+ # 3. Post-processing
63
+ predicted_index = np.argmax(predictions)
64
+ predicted_sign = SIGN_CLASSES[predicted_index]
65
+ confidence = predictions[predicted_index] * 100
66
+
67
+ return f"PREDICTED SIGN: {predicted_sign} | Confidence: {confidence:.2f}%"
68
+
69
+ except Exception as e:
70
+ # This catches runtime errors during invocation
71
+ return f"Prediction Runtime Error: {e}"
72
+
73
+
74
+ # --- 3. The Gradio Interface for Continuous Streaming ---
75
+ gr.Interface(
76
+ fn=classify_sign,
77
+ inputs=gr.Image(
78
+ sources=['webcam'],
79
+ type="numpy",
80
+ shape=(300, 300),
81
+ label="Live Sign Camera"
82
+ ),
83
+ outputs=gr.Textbox(label="Real-Time Translation"),
84
+ live=True,
85
+ title="Real-Time Sign Language Translator",
86
+ description="Show your sign in front of the camera, and the prediction will update instantly.",
87
+ theme="soft",
88
+ # FIX for PermissionError and general stability
89
+ allow_flagging=False
90
+ ).launch(server_name="0.0.0.0", server_port=7860)