Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,61 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# Define gestures based on finger count
|
62 |
-
if finger_count == 1:
|
63 |
-
return "Thumbs Up"
|
64 |
-
elif finger_count == 2:
|
65 |
-
return "Two Fingers"
|
66 |
-
elif finger_count == 5:
|
67 |
-
return "Open Palm"
|
68 |
-
else:
|
69 |
-
return "Not Recognized"
|
70 |
-
|
71 |
-
return "No Hand Detected"
|
72 |
-
|
73 |
-
# Streamlit App
|
74 |
-
st.title("Gesture Recognition and Sign Language Translator")
|
75 |
-
|
76 |
-
# Checkbox to choose the functionality
|
77 |
-
use_gesture = st.sidebar.checkbox("Do you know Sign Language?")
|
78 |
-
|
79 |
-
if use_gesture:
|
80 |
-
st.write("This section detects basic gestures using a webcam.")
|
81 |
-
st.write("Please make sure your webcam is accessible.")
|
82 |
-
|
83 |
-
# Open webcam
|
84 |
-
cam = cv2.VideoCapture(0)
|
85 |
-
|
86 |
-
if not cam.isOpened():
|
87 |
-
st.write("Error: Could not open webcam.")
|
88 |
-
else:
|
89 |
-
frame_placeholder = st.empty()
|
90 |
-
|
91 |
-
while True:
|
92 |
-
ret, frame = cam.read()
|
93 |
-
if not ret:
|
94 |
-
st.write("Error: Could not read frame.")
|
95 |
-
break
|
96 |
-
|
97 |
-
gesture = detect_gesture(frame)
|
98 |
-
cv2.putText(frame, f"Gesture: {gesture}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
|
99 |
-
|
100 |
-
# Convert OpenCV image to format suitable for Streamlit
|
101 |
-
_, buffer = cv2.imencode('.jpg', frame)
|
102 |
-
frame_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
103 |
-
img_str = f"data:image/jpeg;base64,{frame_base64}"
|
104 |
-
|
105 |
-
# Display the frame
|
106 |
-
frame_placeholder.markdown(f'<img src="{img_str}" style="width: 100%;">', unsafe_allow_html=True)
|
107 |
-
|
108 |
-
if st.button("Stop Webcam", key="stop_webcam"):
|
109 |
-
break
|
110 |
-
|
111 |
-
# Release webcam
|
112 |
-
cam.release()
|
113 |
-
else:
|
114 |
-
st.write("Enter the text that you want to show to people who don't know sign language.")
|
115 |
-
text_input = st.text_input("Enter text:")
|
116 |
-
|
117 |
-
if st.button("Convert Text to Image"):
|
118 |
-
generated_image = text_to_image(text_input)
|
119 |
-
st.image(generated_image, caption="Generated Image from Text", use_column_width=True)
|
120 |
-
|
121 |
-
st.write("Thanks for using our Sign Language Assistant!")
|
|
|
1 |
+
# Sign Language Translator with Streamlit
|
2 |
+
|
3 |
import streamlit as st
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
+
import mediapipe as mp
|
7 |
+
|
8 |
+
# Load Mediapipe Hands model
|
9 |
+
mp_hands = mp.solutions.hands
|
10 |
+
hands = mp_hands.Hands()
|
11 |
+
|
12 |
+
# Function to process webcam feed
|
13 |
+
def process_webcam():
|
14 |
+
cap = cv2.VideoCapture(0)
|
15 |
+
while cap.isOpened():
|
16 |
+
ret, frame = cap.read()
|
17 |
+
if not ret:
|
18 |
+
break
|
19 |
+
|
20 |
+
# Convert the frame to RGB
|
21 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
22 |
+
results = hands.process(frame_rgb)
|
23 |
+
|
24 |
+
# Draw hand landmarks
|
25 |
+
if results.multi_hand_landmarks:
|
26 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
27 |
+
mp.solutions.drawing_utils.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
28 |
+
|
29 |
+
# Display the frame
|
30 |
+
cv2.imshow('Webcam', frame)
|
31 |
+
|
32 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
33 |
+
break
|
34 |
+
|
35 |
+
cap.release()
|
36 |
+
cv2.destroyAllWindows()
|
37 |
+
|
38 |
+
# Streamlit UI
|
39 |
+
st.title("Sign Language Translator")
|
40 |
+
st.write("This application translates sign language into Farsi and provides visual aids for understanding.")
|
41 |
+
|
42 |
+
if st.button("Open Webcam"):
|
43 |
+
st.write("Opening webcam...")
|
44 |
+
process_webcam()
|
45 |
+
|
46 |
+
# Placeholder for translation logic
|
47 |
+
def translate_sign_language(hand_gesture):
|
48 |
+
# Placeholder for actual translation logic
|
49 |
+
return "Translated Text in Farsi"
|
50 |
+
|
51 |
+
# Placeholder for visual aid
|
52 |
+
def show_visual_aid(hand_gesture):
|
53 |
+
# Placeholder for actual visual aid logic
|
54 |
+
st.image("path_to_hand_shape_image.png", caption="Hand Shape for Sign Language")
|
55 |
+
|
56 |
+
# User input for non-sign language users
|
57 |
+
user_input = st.text_input("Enter text if you do not know sign language:")
|
58 |
+
if user_input:
|
59 |
+
st.write("You entered:", user_input)
|
60 |
+
# Logic to show visual aid
|
61 |
+
show_visual_aid(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|