Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import streamlit as st
|
|
8 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
9 |
from sample_utils.turn import get_ice_servers
|
10 |
from cvzone.HandTrackingModule import HandDetector
|
11 |
-
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
12 |
import os
|
13 |
import time
|
14 |
|
@@ -22,20 +21,13 @@ st.subheader('''Turn on the webcam and use hand gestures to interact with the vi
|
|
22 |
Use 'a' and 'd' from the keyboard to change the background.''')
|
23 |
|
24 |
# Initialize modules
|
25 |
-
detector = HandDetector(maxHands=1, detectionCon=0.
|
26 |
-
segmentor = SelfiSegmentation()
|
27 |
|
28 |
# Define virtual keyboard layout
|
29 |
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
30 |
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
|
31 |
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
|
32 |
|
33 |
-
class Button:
|
34 |
-
def __init__(self, pos, text, size=[100, 100]):
|
35 |
-
self.pos = pos
|
36 |
-
self.size = size
|
37 |
-
self.text = text
|
38 |
-
|
39 |
class Detection(NamedTuple):
|
40 |
label: str
|
41 |
score: float
|
@@ -43,7 +35,7 @@ class Detection(NamedTuple):
|
|
43 |
|
44 |
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
45 |
|
46 |
-
#
|
47 |
listImg = os.listdir('model/street') if os.path.exists('model/street') else []
|
48 |
if not listImg:
|
49 |
st.error("Error: 'street' directory is missing or empty. Please add background images.")
|
@@ -62,17 +54,23 @@ if "output_text" not in st.session_state:
|
|
62 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
63 |
global indexImg, output_text
|
64 |
|
|
|
65 |
img = frame.to_ndarray(format="bgr24")
|
|
|
|
|
66 |
hands, img = detector.findHands(img, draw=True)
|
67 |
|
68 |
detections = []
|
69 |
if hands:
|
|
|
70 |
for hand in hands:
|
71 |
bbox = hand['bbox']
|
72 |
label = "Hand"
|
73 |
score = hand['score']
|
74 |
box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
|
75 |
detections.append(Detection(label=label, score=score, box=box))
|
|
|
|
|
76 |
|
77 |
result_queue.put(detections)
|
78 |
st.session_state["output_text"] = output_text
|
|
|
8 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
9 |
from sample_utils.turn import get_ice_servers
|
10 |
from cvzone.HandTrackingModule import HandDetector
|
|
|
11 |
import os
|
12 |
import time
|
13 |
|
|
|
21 |
Use 'a' and 'd' from the keyboard to change the background.''')
|
22 |
|
23 |
# Initialize modules
|
24 |
+
detector = HandDetector(maxHands=1, detectionCon=0.7)
|
|
|
25 |
|
26 |
# Define virtual keyboard layout
|
27 |
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
28 |
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
|
29 |
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
class Detection(NamedTuple):
|
32 |
label: str
|
33 |
score: float
|
|
|
35 |
|
36 |
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
37 |
|
38 |
+
# Background image loading
|
39 |
listImg = os.listdir('model/street') if os.path.exists('model/street') else []
|
40 |
if not listImg:
|
41 |
st.error("Error: 'street' directory is missing or empty. Please add background images.")
|
|
|
54 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
55 |
global indexImg, output_text
|
56 |
|
57 |
+
# Convert the frame to BGR
|
58 |
img = frame.to_ndarray(format="bgr24")
|
59 |
+
|
60 |
+
# Process the frame with Hand Detector
|
61 |
hands, img = detector.findHands(img, draw=True)
|
62 |
|
63 |
detections = []
|
64 |
if hands:
|
65 |
+
logger.info(f"Detected {len(hands)} hand(s).")
|
66 |
for hand in hands:
|
67 |
bbox = hand['bbox']
|
68 |
label = "Hand"
|
69 |
score = hand['score']
|
70 |
box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
|
71 |
detections.append(Detection(label=label, score=score, box=box))
|
72 |
+
else:
|
73 |
+
logger.info("No hands detected.")
|
74 |
|
75 |
result_queue.put(detections)
|
76 |
st.session_state["output_text"] = output_text
|