Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,45 @@ import numpy as np
|
|
| 3 |
import cv2
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load the saved Keras model
|
| 7 |
-
model = load_model("
|
| 8 |
|
| 9 |
# Define the labels for ASL classes
|
| 10 |
-
labels = ['A', 'B', 'C', 'D', 'E', 'F',
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def preprocess_frame(frame):
|
| 13 |
-
"""Preprocess the frame for the ASL model."""
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def predict_asl(frame):
|
| 22 |
"""Predict the ASL sign from the webcam frame."""
|
|
|
|
| 3 |
import cv2
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
|
| 6 |
+
IMG_HEIGHT = 96
|
| 7 |
+
IMG_WIDTH = 96
|
| 8 |
+
|
| 9 |
# Load the saved Keras model
|
| 10 |
+
model = load_model("model_01.keras")
|
| 11 |
|
| 12 |
# Define the labels for ASL classes
|
| 13 |
+
labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
| 14 |
+
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
| 15 |
+
'T', 'U', 'V', 'W', 'X', 'Y'] # Replace with your actual label names
|
| 16 |
|
| 17 |
def preprocess_frame(frame):
|
| 18 |
+
"""Preprocess the video frame for the ASL model."""
|
| 19 |
+
# Convert the frame to a TensorFlow tensor
|
| 20 |
+
if isinstance(frame, np.ndarray):
|
| 21 |
+
frame = tf.convert_to_tensor(frame, dtype=tf.float32)
|
| 22 |
+
# Reshape to add channel dimension if grayscale
|
| 23 |
+
if frame.ndim == 2: # If the input is grayscale
|
| 24 |
+
frame = tf.expand_dims(frame, axis=-1)
|
| 25 |
+
frame = tf.image.grayscale_to_rgb(frame)
|
| 26 |
+
|
| 27 |
+
# Ensure the frame has 3 channels (RGB)
|
| 28 |
+
if frame.shape[-1] == 1: # Grayscale image
|
| 29 |
+
frame = tf.image.grayscale_to_rgb(frame)
|
| 30 |
+
|
| 31 |
+
# First scale down to dataset dimensions (if applicable)
|
| 32 |
+
frame = tf.image.resize(frame, [28, 28]) # Resize to smaller dimensions for consistency
|
| 33 |
+
|
| 34 |
+
# Resize to the target model input dimensions
|
| 35 |
+
frame = tf.image.resize(frame, [IMG_HEIGHT, IMG_WIDTH])
|
| 36 |
+
|
| 37 |
+
# Normalize pixel values to [0, 1]
|
| 38 |
+
frame = tf.cast(frame, tf.float32) / 255.0
|
| 39 |
+
|
| 40 |
+
# Add batch dimension for model input
|
| 41 |
+
frame = tf.expand_dims(frame, axis=0)
|
| 42 |
+
|
| 43 |
+
return frame
|
| 44 |
+
|
| 45 |
|
| 46 |
def predict_asl(frame):
|
| 47 |
"""Predict the ASL sign from the webcam frame."""
|