Skip to content

Commit 4c5909c

Browse files
committed
add handle event func
1 parent 643aec3 commit 4c5909c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

apps/99_streamlit_examples/pages/12_Video_processing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
from enum import Enum
23

34
import cv2
@@ -109,11 +110,17 @@ def process(
109110
conf=self.confidence,
110111
classes=self.classes,
111112
)
113+
previous_num = self.num
112114
self.num = len([x for x in results[0].boxes.cls if x == self.target])
113115
output_img = results[0].plot(
114116
labels=True,
115117
conf=True,
116118
)
119+
120+
# play alert sound if the number of target objects increases
121+
if previous_num < self.num:
122+
self._handle_event()
123+
117124
return cv2.cvtColor(
118125
src=output_img,
119126
code=cv2.COLOR_BGR2RGB,
@@ -124,6 +131,27 @@ def get_states(self):
124131
"num": self.num,
125132
}
126133

134+
def _handle_event(self):
135+
st.toast("Alert", icon="❗")
136+
try:
137+
# https://discuss.streamlit.io/t/how-to-play-an-audio-file-automatically-generated-using-text-to-speech-in-streamlit/33201
138+
# https://www.youtube.com/watch?v=U5Z76XLEwRM
139+
with open("./datasets/alert.mp3a", "rb") as f:
140+
data = f.read()
141+
b64 = base64.b64encode(data).decode()
142+
md = f"""
143+
<audio controls autoplay="true">
144+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
145+
</audio>
146+
"""
147+
st.markdown(
148+
md,
149+
unsafe_allow_html=True,
150+
)
151+
except Exception as _:
152+
# st.toast("Failed to play alert sound", icon="❌")
153+
pass
154+
127155

128156
def get_processor(processor_type: ProcessorType) -> Processor:
129157
if processor_type == ProcessorType.BLUR:

0 commit comments

Comments
 (0)