Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
# Load gesture classification model
|
| 9 |
+
model = tf.keras.models.load_model('gesture_model.h5')
|
| 10 |
+
|
| 11 |
+
# Mediapipe initialization
|
| 12 |
+
mp_hands = mp.solutions.hands
|
| 13 |
+
hands = mp_hands.Hands()
|
| 14 |
+
mp_draw = mp.solutions.drawing_utils
|
| 15 |
+
|
| 16 |
+
# Function for gesture classification
|
| 17 |
+
def classify_gesture(landmarks):
|
| 18 |
+
landmarks = np.array(landmarks).reshape(1, -1)
|
| 19 |
+
prediction = model.predict(landmarks)
|
| 20 |
+
return np.argmax(prediction)
|
| 21 |
+
|
| 22 |
+
# Streamlit UI
|
| 23 |
+
def main():
|
| 24 |
+
st.set_page_config(page_title="Sign Language Translator", layout="wide")
|
| 25 |
+
st.title("🤟 Sign Language Translator")
|
| 26 |
+
st.write("Translate sign language gestures into text and speech in real time.")
|
| 27 |
+
|
| 28 |
+
# Sidebar
|
| 29 |
+
st.sidebar.header("Settings")
|
| 30 |
+
use_camera = st.sidebar.checkbox("Use Camera")
|
| 31 |
+
|
| 32 |
+
# Display Video Feed
|
| 33 |
+
if use_camera:
|
| 34 |
+
st.write("### 📸 Live Camera Feed")
|
| 35 |
+
frame_placeholder = st.empty()
|
| 36 |
+
|
| 37 |
+
cap = cv2.VideoCapture(0)
|
| 38 |
+
while cap.isOpened():
|
| 39 |
+
ret, frame = cap.read()
|
| 40 |
+
if not ret:
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 44 |
+
results = hands.process(frame)
|
| 45 |
+
|
| 46 |
+
if results.multi_hand_landmarks:
|
| 47 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
| 48 |
+
mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 49 |
+
|
| 50 |
+
# Extract landmarks
|
| 51 |
+
landmarks = [landmark.x for landmark in hand_landmarks.landmark]
|
| 52 |
+
landmarks += [landmark.y for landmark in hand_landmarks.landmark]
|
| 53 |
+
gesture = classify_gesture(landmarks)
|
| 54 |
+
st.write(f"Gesture: {gesture}")
|
| 55 |
+
|
| 56 |
+
frame_placeholder.image(frame, channels="RGB")
|
| 57 |
+
|
| 58 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 59 |
+
break
|
| 60 |
+
|
| 61 |
+
cap.release()
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main()
|