Pratyush101 commited on
Commit
c0c1c4c
·
verified ·
1 Parent(s): 3c1f2cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -12,12 +12,12 @@ import os
12
  import time
13
 
14
  # Logger Setup
15
- logger = logging.getLogger(_name_)
16
 
17
  # Streamlit settings
18
- st.set_page_config(page_title="Virtual Keyboard", page_icon="🏋️")
19
  st.title("Interactive Virtual Keyboard")
20
- st.subheader('''Turn on the webcam and use hand gestures to interact with the virtual keyboard .''')
21
 
22
  # Initialize MediaPipe and Background Segmentor
23
  mp_hands = mp.solutions.hands
@@ -59,20 +59,20 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
59
  # Create the keyboard buttons
60
  buttonList = []
61
  h, w = img.shape[:2]
62
- key_width = int(0.07 * w)
63
- key_height = int(0.09 * h)
64
- font_scale = 0.0045 * w
65
- font_thickness = int(0.009 * h)
66
 
67
  for row, key_row in enumerate(keys):
68
  for col, key in enumerate(key_row):
69
- x = int(0.03 * w + col * (key_width + 10))
70
- y = int(0.03 * h + row * (key_height + 10))
71
  buttonList.append(Button([x, y], key, size=[key_width, key_height]))
72
 
73
  # Add special buttons for Backspace and Space
74
- buttonList.append(Button([int(0.85 * w), int(0.03 * h)], 'BS', size=[int(0.08 * w), key_height]))
75
- buttonList.append(Button([int(0.2 * w), int(0.4 * h)], 'SPACE', size=[int(0.6 * w), key_height]))
76
 
77
  # Draw Keyboard Buttons
78
  for button in buttonList:
@@ -105,7 +105,7 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
105
  x8, y8 = int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * w), int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * h)
106
 
107
  distance = np.sqrt((x8 - x4) * 2 + (y8 - y4) * 2)
108
- click_threshold = 0.2*np.sqrt(bbox[2] * 2 + bbox[3] * 2)
109
 
110
  for button in buttonList:
111
  x, y = button.pos
@@ -113,34 +113,34 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
113
  if x < x8 < x + bw and y < y8 < y + bh:
114
  cv2.rectangle(img, (x, y), (x + bw, y + bh), (0, 255, 160), -1)
115
  cv2.putText(img, button.text, (x + int(0.2 * bw), y + int(0.7 * bh)), cv2.FONT_HERSHEY_PLAIN, font_scale, (255, 255, 255), font_thickness)
116
- # # Handle button press
117
- if (distance)<click_threshold:
118
  if time.time() - prev_key_time[0] > 2:
119
  prev_key_time[0] = time.time()
120
  if button.text != 'BS' and button.text != 'SPACE':
121
  output_text += button.text # Append key to output text
122
  elif button.text == 'BS':
123
- output_text = output_text[:-1] # Remove last character
124
  else:
125
  output_text += ' ' # Add space
126
- # Position and dimensions for the rectangle
 
127
  text_x = int(0.05 * w)
128
- text_y = int(0.70 * h)
129
- text_width = int(0.9 * w) # Adjust width as needed
130
- text_height = int(0.1 * h) # Adjust height as needed
131
- # Draw the rectangle
 
132
  cv2.rectangle(img,
133
- (text_x, text_y - text_height), # Top-left corner
134
- (text_x + text_width, text_y), # Bottom-right corner
135
- (100, 100, 100), # Background color (light gray)
136
- -1) # Filled rectangle
137
- cv2.putText(img, output_text, (int(0.05 * w) , int(0.70 * h)), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 5)
138
-
139
-
140
-
141
 
142
  result_queue.put(detections)
143
-
144
  return av.VideoFrame.from_ndarray(img, format="bgr24")
145
 
146
  # WebRTC Streamer
 
12
  import time
13
 
14
  # Logger Setup
15
+ logger = logging.getLogger(__name__)
16
 
17
  # Streamlit settings
18
+ st.set_page_config(page_title="Virtual Keyboard", page_icon="🏋")
19
  st.title("Interactive Virtual Keyboard")
20
+ st.subheader('''Turn on the webcam and use hand gestures to interact with the virtual keyboard.''')
21
 
22
  # Initialize MediaPipe and Background Segmentor
23
  mp_hands = mp.solutions.hands
 
59
  # Create the keyboard buttons
60
  buttonList = []
61
  h, w = img.shape[:2]
62
+ key_width = int(0.08 * w) # Increased button width
63
+ key_height = int(0.1 * h) # Increased button height
64
+ font_scale = 0.005 * w # Adjusted font size
65
+ font_thickness = int(0.01 * h) # Adjusted font thickness
66
 
67
  for row, key_row in enumerate(keys):
68
  for col, key in enumerate(key_row):
69
+ x = int(0.05 * w + col * (key_width + 10)) # Added extra spacing between keys
70
+ y = int(0.05 * h + row * (key_height + 10)) # Added extra spacing between keys
71
  buttonList.append(Button([x, y], key, size=[key_width, key_height]))
72
 
73
  # Add special buttons for Backspace and Space
74
+ buttonList.append(Button([int(0.85 * w), int(0.05 * h)], 'BS', size=[int(0.1 * w), key_height]))
75
+ buttonList.append(Button([int(0.2 * w), int(0.35 * h)], 'SPACE', size=[int(0.6 * w), key_height]))
76
 
77
  # Draw Keyboard Buttons
78
  for button in buttonList:
 
105
  x8, y8 = int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * w), int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * h)
106
 
107
  distance = np.sqrt((x8 - x4) * 2 + (y8 - y4) * 2)
108
+ click_threshold = 0.2 * np.sqrt(bbox[2] * 2 + bbox[3] * 2)
109
 
110
  for button in buttonList:
111
  x, y = button.pos
 
113
  if x < x8 < x + bw and y < y8 < y + bh:
114
  cv2.rectangle(img, (x, y), (x + bw, y + bh), (0, 255, 160), -1)
115
  cv2.putText(img, button.text, (x + int(0.2 * bw), y + int(0.7 * bh)), cv2.FONT_HERSHEY_PLAIN, font_scale, (255, 255, 255), font_thickness)
116
+ if distance < click_threshold:
 
117
  if time.time() - prev_key_time[0] > 2:
118
  prev_key_time[0] = time.time()
119
  if button.text != 'BS' and button.text != 'SPACE':
120
  output_text += button.text # Append key to output text
121
  elif button.text == 'BS':
122
+ output_text = output_text[:-1] # Remove last character
123
  else:
124
  output_text += ' ' # Add space
125
+
126
+ # Position and dimensions for the output text box
127
  text_x = int(0.05 * w)
128
+ text_y = int(0.75 * h)
129
+ text_width = int(0.9 * w)
130
+ text_height = int(0.1 * h)
131
+
132
+ # Draw the background for output text box
133
  cv2.rectangle(img,
134
+ (text_x, text_y - text_height),
135
+ (text_x + text_width, text_y),
136
+ (50, 50, 50), # Dark background for the text area
137
+ -1)
138
+
139
+ # Display the output text in white with larger font
140
+ cv2.putText(img, output_text, (text_x + 20, text_y - 20), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 5)
 
141
 
142
  result_queue.put(detections)
143
+
144
  return av.VideoFrame.from_ndarray(img, format="bgr24")
145
 
146
  # WebRTC Streamer