arshtech commited on
Commit
55bbccc
Β·
verified Β·
1 Parent(s): 963e581

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -31
app.py CHANGED
@@ -1,45 +1,128 @@
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
 
 
 
 
 
4
 
5
- # -------------------------------
6
- # Page Configuration
7
- # -------------------------------
8
- st.set_page_config(
9
- page_title="Hand2Voice",
10
- layout="wide"
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  )
12
 
13
- st.title("🀚 Hand2Voice")
14
- st.subheader("Hand Gestures to Speech Interface")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # -------------------------------
17
- # Layout
18
- # -------------------------------
19
  col1, col2 = st.columns(2)
20
 
21
- # -------------------------------
22
- # Camera Section (Left)
23
- # -------------------------------
24
  with col1:
25
- st.markdown("### πŸ“· Camera Input")
26
- image = st.camera_input("Capture hand gesture")
27
 
28
- # -------------------------------
29
- # Text Section (Right)
30
- # -------------------------------
31
  with col2:
32
- st.markdown("### πŸ“ Recognized Text")
33
- st.markdown(
34
- "<h2>HELLO <span style='background-color:yellow;'>HOW</span> ARE YOU</h2>",
35
- unsafe_allow_html=True
36
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- st.markdown("### πŸ”Š Spoken Output")
39
- st.info("Currently speaking: HOW")
 
 
40
 
41
- # -------------------------------
42
- # Status
43
- # -------------------------------
44
- st.markdown("---")
45
- st.success("Capture an image of your hand to begin")
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
+ import mediapipe as mp
5
+ import requests
6
+ from gtts import gTTS
7
+ import os
8
+ import tempfile
9
 
10
+ # ---------------- CONFIG ----------------
11
+ st.set_page_config(page_title="Hand2Voice", layout="wide")
12
+
13
+ GESTURE_URL = "https://raw.githubusercontent.com/YOUR_USERNAME/Hand2Voice/main/gestures/gesture_rules.json"
14
+
15
+ # ---------------- LOAD GESTURES ----------------
16
+ @st.cache_data
17
+ def load_gestures():
18
+ response = requests.get(GESTURE_URL)
19
+ return response.json()["gestures"]
20
+
21
+ gesture_rules = load_gestures()
22
+
23
+ # ---------------- MEDIAPIPE SETUP ----------------
24
+ mp_hands = mp.solutions.hands
25
+ hands = mp_hands.Hands(
26
+ static_image_mode=True,
27
+ max_num_hands=1,
28
+ min_detection_confidence=0.7
29
  )
30
 
31
+ # ---------------- FINGER LOGIC ----------------
32
+ def get_finger_states(hand_landmarks):
33
+ finger_tips = [4, 8, 12, 16, 20]
34
+ finger_bases = [2, 6, 10, 14, 18]
35
+
36
+ states = []
37
+
38
+ # Thumb
39
+ states.append(
40
+ 1 if hand_landmarks.landmark[4].x >
41
+ hand_landmarks.landmark[3].x else 0
42
+ )
43
+
44
+ # Other fingers
45
+ for tip, base in zip(finger_tips[1:], finger_bases[1:]):
46
+ states.append(
47
+ 1 if hand_landmarks.landmark[tip].y <
48
+ hand_landmarks.landmark[base].y else 0
49
+ )
50
+
51
+ return states
52
+
53
+ # ---------------- GESTURE MATCHING ----------------
54
+ def match_gesture(finger_states):
55
+ for name, info in gesture_rules.items():
56
+ if finger_states == info["pattern"]:
57
+ return name
58
+ return "Unknown Gesture"
59
+
60
+ # ---------------- GESTURE RECOGNIZER ----------------
61
+ def recognize_gesture(frame):
62
+ rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
63
+ results = hands.process(rgb)
64
+
65
+ if results.multi_hand_landmarks:
66
+ for hand_landmarks in results.multi_hand_landmarks:
67
+ states = get_finger_states(hand_landmarks)
68
+ return match_gesture(states)
69
+
70
+ return "No Hand Detected"
71
+
72
+ # ---------------- TEXT TO SPEECH ----------------
73
+ def speak_text(text):
74
+ tts = gTTS(text=text, lang="en")
75
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
76
+ tts.save(temp_file.name)
77
+ return temp_file.name
78
+
79
+ # ---------------- UI ----------------
80
+ st.markdown(
81
+ "<h1 style='text-align:center;'>🀟 Hand2Voice</h1>"
82
+ "<p style='text-align:center;'>Hand Gesture to Voice Conversion</p>",
83
+ unsafe_allow_html=True
84
+ )
85
+
86
+ st.divider()
87
 
 
 
 
88
  col1, col2 = st.columns(2)
89
 
90
+ # -------- CAMERA COLUMN --------
 
 
91
  with col1:
92
+ st.subheader("πŸ“· Camera Input")
93
+ image = st.camera_input("Capture your hand gesture")
94
 
95
+ # -------- OUTPUT COLUMN --------
 
 
96
  with col2:
97
+ st.subheader("πŸ“ Output")
98
+
99
+ if image is not None:
100
+ bytes_data = image.getvalue()
101
+ np_img = np.frombuffer(bytes_data, np.uint8)
102
+ frame = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
103
+
104
+ gesture_text = recognize_gesture(frame)
105
+
106
+ # Highlight text
107
+ st.markdown(
108
+ f"""
109
+ <div style="
110
+ padding:20px;
111
+ background-color:#e0f2fe;
112
+ border-radius:12px;
113
+ font-size:24px;
114
+ font-weight:bold;
115
+ text-align:center;">
116
+ πŸ”Š {gesture_text}
117
+ </div>
118
+ """,
119
+ unsafe_allow_html=True
120
+ )
121
 
122
+ # Speak text
123
+ if gesture_text not in ["No Hand Detected", "Unknown Gesture"]:
124
+ audio_path = speak_text(gesture_text)
125
+ st.audio(audio_path, format="audio/mp3")
126
 
127
+ else:
128
+ st.info("Show your hand and capture an image")