Update app.py
Browse filesI have included the result queue
app.py
CHANGED
@@ -199,6 +199,9 @@ from cvzone.HandTrackingModule import HandDetector
|
|
199 |
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
200 |
import os
|
201 |
import time
|
|
|
|
|
|
|
202 |
from sample_utils.turn import get_ice_servers
|
203 |
|
204 |
logger = logging.getLogger(__name__)
|
@@ -224,6 +227,13 @@ class Button:
|
|
224 |
self.size = size
|
225 |
self.text = text
|
226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
listImg = os.listdir('model/street') if os.path.exists('model/street') else []
|
228 |
if not listImg:
|
229 |
st.error("Error: 'street' directory is missing or empty. Please add background images.")
|
@@ -238,50 +248,51 @@ output_text = ""
|
|
238 |
if "output_text" not in st.session_state:
|
239 |
st.session_state["output_text"] = ""
|
240 |
|
241 |
-
def video_frame_callback(frame):
|
242 |
global indexImg, output_text
|
243 |
|
244 |
img = frame.to_ndarray(format="bgr24")
|
245 |
imgOut = segmentor.removeBG(img, imgList[indexImg])
|
246 |
-
hands,
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
283 |
st.session_state["output_text"] = output_text
|
284 |
-
return
|
285 |
|
286 |
webrtc_streamer(
|
287 |
key="virtual-keyboard",
|
@@ -295,3 +306,4 @@ webrtc_streamer(
|
|
295 |
st.subheader("Output Text")
|
296 |
st.text_area("Live Input:", value=st.session_state["output_text"], height=200)
|
297 |
|
|
|
|
199 |
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
200 |
import os
|
201 |
import time
|
202 |
+
import av
|
203 |
+
import queue
|
204 |
+
from typing import List, NamedTuple
|
205 |
from sample_utils.turn import get_ice_servers
|
206 |
|
207 |
logger = logging.getLogger(__name__)
|
|
|
227 |
self.size = size
|
228 |
self.text = text
|
229 |
|
230 |
+
class Detection(NamedTuple):
|
231 |
+
label: str
|
232 |
+
score: float
|
233 |
+
box: np.ndarray
|
234 |
+
|
235 |
+
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
236 |
+
|
237 |
listImg = os.listdir('model/street') if os.path.exists('model/street') else []
|
238 |
if not listImg:
|
239 |
st.error("Error: 'street' directory is missing or empty. Please add background images.")
|
|
|
248 |
if "output_text" not in st.session_state:
|
249 |
st.session_state["output_text"] = ""
|
250 |
|
251 |
+
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
252 |
global indexImg, output_text
|
253 |
|
254 |
img = frame.to_ndarray(format="bgr24")
|
255 |
imgOut = segmentor.removeBG(img, imgList[indexImg])
|
256 |
+
hands, imgOut = detector.findHands(imgOut, flipType=False)
|
257 |
+
|
258 |
+
buttonList = [Button([30 + col * 105, 30 + row * 120], key) for row, line in enumerate(keys) for col, key in enumerate(line)]
|
259 |
+
|
260 |
+
detections = []
|
261 |
+
if hands:
|
262 |
+
for i, hand in enumerate(hands):
|
263 |
+
lmList = hand['lmList']
|
264 |
+
bbox = hand['bbox']
|
265 |
+
label = "Hand"
|
266 |
+
score = hand['score']
|
267 |
+
box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
|
268 |
+
detections.append(Detection(label=label, score=score, box=box))
|
269 |
+
|
270 |
+
if lmList:
|
271 |
+
x4, y4 = lmList[4][0], lmList[4][1]
|
272 |
+
x8, y8 = lmList[8][0], lmList[8][1]
|
273 |
+
distance = np.sqrt((x8 - x4) ** 2 + (y8 - y4) ** 2)
|
274 |
+
click_threshold = 10
|
275 |
+
|
276 |
+
for button in buttonList:
|
277 |
+
x, y = button.pos
|
278 |
+
w, h = button.size
|
279 |
+
if x < x8 < x + w and y < y8 < y + h:
|
280 |
+
cv2.rectangle(imgOut, button.pos, (x + w, y + h), (0, 255, 160), -1)
|
281 |
+
cv2.putText(imgOut, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
|
282 |
+
|
283 |
+
if (distance / np.sqrt(bbox[2] ** 2 + bbox[3] ** 2)) * 100 < click_threshold:
|
284 |
+
if time.time() - prev_key_time[i] > 2:
|
285 |
+
prev_key_time[i] = time.time()
|
286 |
+
if button.text != 'BS' and button.text != 'SPACE':
|
287 |
+
output_text += button.text
|
288 |
+
elif button.text == 'BS':
|
289 |
+
output_text = output_text[:-1]
|
290 |
+
else:
|
291 |
+
output_text += ' '
|
292 |
+
|
293 |
+
result_queue.put(detections)
|
294 |
st.session_state["output_text"] = output_text
|
295 |
+
return av.VideoFrame.from_ndarray(imgOut, format="bgr24")
|
296 |
|
297 |
webrtc_streamer(
|
298 |
key="virtual-keyboard",
|
|
|
306 |
st.subheader("Output Text")
|
307 |
st.text_area("Live Input:", value=st.session_state["output_text"], height=200)
|
308 |
|
309 |
+
|