Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import cv2
|
|
8 |
import numpy as np
|
9 |
import streamlit as st
|
10 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
11 |
-
from sample_utils.webrtc_helpers import process_video_frame
|
12 |
from sample_utils.turn import get_ice_servers
|
13 |
from cvzone.HandTrackingModule import HandDetector
|
14 |
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
@@ -20,6 +19,63 @@ logger = logging.getLogger(__name__)
|
|
20 |
st.title("Interactive Virtual Keyboard with Twilio Integration")
|
21 |
st.info("Use your webcam to interact with the virtual keyboard via hand gestures.")
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
detector = HandDetector(maxHands=1, detectionCon=0.8)
|
24 |
segmentor = SelfiSegmentation()
|
25 |
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
@@ -37,17 +93,12 @@ if "output_text" not in st.session_state:
|
|
37 |
# Create a thread-safe queue for passing results from callback
|
38 |
result_queue = queue.Queue()
|
39 |
|
40 |
-
# Function to process video frame callback
|
41 |
-
# Corrected function for video frame callback
|
42 |
-
#####FromHERE CHANGE
|
43 |
-
# Function to process video frame callback
|
44 |
-
# Function to process video frame callback
|
45 |
def video_frame_callback(frame):
|
46 |
# Process the frame asynchronously
|
47 |
-
|
48 |
-
# Put the processed
|
49 |
-
result_queue.put(
|
50 |
-
return
|
51 |
|
52 |
webrtc_ctx = webrtc_streamer(
|
53 |
key="keyboard-demo",
|
@@ -67,4 +118,4 @@ st.write(
|
|
67 |
1. Turn on your webcam using the checkbox above.
|
68 |
2. Use hand gestures to interact with the virtual keyboard.
|
69 |
"""
|
70 |
-
)
|
|
|
8 |
import numpy as np
|
9 |
import streamlit as st
|
10 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
|
|
11 |
from sample_utils.turn import get_ice_servers
|
12 |
from cvzone.HandTrackingModule import HandDetector
|
13 |
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
|
|
19 |
st.title("Interactive Virtual Keyboard with Twilio Integration")
|
20 |
st.info("Use your webcam to interact with the virtual keyboard via hand gestures.")
|
21 |
|
22 |
+
class Button:
|
23 |
+
def __init__(self, pos, text, size=[100, 100]):
|
24 |
+
self.pos = pos
|
25 |
+
self.size = size
|
26 |
+
self.text = text
|
27 |
+
|
28 |
+
# Function to process the video frame from the webcam
|
29 |
+
def process_video_frame(frame: av.VideoFrame, detector, segmentor, imgList, indexImg, keys, session_state)-> av.VideoFrame:
|
30 |
+
# Convert the frame to a numpy array (BGR format)
|
31 |
+
image = frame.to_ndarray(format="bgr24")
|
32 |
+
|
33 |
+
# Remove background using SelfiSegmentation
|
34 |
+
imgOut = segmentor.removeBG(image, imgList[indexImg])
|
35 |
+
|
36 |
+
# Detect hands on the background-removed image
|
37 |
+
hands, img = detector.findHands(imgOut, flipType=False)
|
38 |
+
|
39 |
+
# Create a blank canvas for the keyboard
|
40 |
+
keyboard_canvas = np.zeros_like(img)
|
41 |
+
buttonList = []
|
42 |
+
|
43 |
+
# Create buttons for the virtual keyboard based on the keys list
|
44 |
+
for key in keys[0]:
|
45 |
+
buttonList.append(Button([30 + keys[0].index(key) * 105, 30], key))
|
46 |
+
for key in keys[1]:
|
47 |
+
buttonList.append(Button([30 + keys[1].index(key) * 105, 150], key))
|
48 |
+
for key in keys[2]:
|
49 |
+
buttonList.append(Button([30 + keys[2].index(key) * 105, 260], key))
|
50 |
+
|
51 |
+
# Draw the buttons on the keyboard canvas
|
52 |
+
for button in buttonList:
|
53 |
+
x, y = button.pos
|
54 |
+
cv2.rectangle(keyboard_canvas, (x, y), (x + button.size[0], y + button.size[1]), (255, 255, 255), -1)
|
55 |
+
cv2.putText(keyboard_canvas, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 0), 3)
|
56 |
+
|
57 |
+
# Handle input and gestures from detected hands
|
58 |
+
if hands:
|
59 |
+
for hand in hands:
|
60 |
+
lmList = hand["lmList"]
|
61 |
+
if lmList:
|
62 |
+
# Get the coordinates of the index finger tip (landmark 8)
|
63 |
+
x8, y8 = lmList[8][0], lmList[8][1]
|
64 |
+
for button in buttonList:
|
65 |
+
bx, by = button.pos
|
66 |
+
bw, bh = button.size
|
67 |
+
# Check if the index finger is over a button
|
68 |
+
if bx < x8 < bx + bw and by < y8 < by + bh:
|
69 |
+
# Highlight the button and update the text
|
70 |
+
cv2.rectangle(img, (bx, by), (bx + bw, by + bh), (0, 255, 0), -1)
|
71 |
+
cv2.putText(img, button.text, (bx + 20, by + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
|
72 |
+
# Update the output text in session_state
|
73 |
+
session_state["output_text"] += button.text
|
74 |
+
|
75 |
+
# Corrected return: Create a video frame from the ndarray image
|
76 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
77 |
+
|
78 |
+
# Initialize components
|
79 |
detector = HandDetector(maxHands=1, detectionCon=0.8)
|
80 |
segmentor = SelfiSegmentation()
|
81 |
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
|
|
93 |
# Create a thread-safe queue for passing results from callback
|
94 |
result_queue = queue.Queue()
|
95 |
|
|
|
|
|
|
|
|
|
|
|
96 |
def video_frame_callback(frame):
|
97 |
# Process the frame asynchronously
|
98 |
+
processed_frame = process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, st.session_state)
|
99 |
+
# Put the processed frame into the queue
|
100 |
+
result_queue.put(processed_frame)
|
101 |
+
return processed_frame
|
102 |
|
103 |
webrtc_ctx = webrtc_streamer(
|
104 |
key="keyboard-demo",
|
|
|
118 |
1. Turn on your webcam using the checkbox above.
|
119 |
2. Use hand gestures to interact with the virtual keyboard.
|
120 |
"""
|
121 |
+
)
|