File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
apps/99_streamlit_examples/pages Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ import base64
1
2
from enum import Enum
2
3
3
4
import cv2
@@ -109,11 +110,17 @@ def process(
109
110
conf = self .confidence ,
110
111
classes = self .classes ,
111
112
)
113
+ previous_num = self .num
112
114
self .num = len ([x for x in results [0 ].boxes .cls if x == self .target ])
113
115
output_img = results [0 ].plot (
114
116
labels = True ,
115
117
conf = True ,
116
118
)
119
+
120
+ # play alert sound if the number of target objects increases
121
+ if previous_num < self .num :
122
+ self ._handle_event ()
123
+
117
124
return cv2 .cvtColor (
118
125
src = output_img ,
119
126
code = cv2 .COLOR_BGR2RGB ,
@@ -124,6 +131,27 @@ def get_states(self):
124
131
"num" : self .num ,
125
132
}
126
133
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.mp3" , "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
+
127
155
128
156
def get_processor (processor_type : ProcessorType ) -> Processor :
129
157
if processor_type == ProcessorType .BLUR :
You can’t perform that action at this time.
0 commit comments