File size: 1,161 Bytes
8b55fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
import av
import streamlit as st
from streamlit_webrtc import WebRtcMode, webrtc_streamer
import numpy as np

st.set_page_config(page_title='Surveillance')

with st.sidebar:
    st.write('# Settings')
    display_settings = {
        'mirror': st.checkbox('mirror'),
        'echo': st.checkbox('echo'),
    }

def audio_frame_callback(frame: av.AudioFrame) -> av.AudioFrame:
    sound = frame.to_ndarray()

    # sound logic

    result_sound = sound if display_settings['echo'] else np.zeros_like(sound)
    result_frame = av.AudioFrame.from_ndarray(result_sound, layout=frame.layout.name)
    result_frame.sample_rate = frame.sample_rate
    return result_frame

def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
    image = frame.to_ndarray(format='bgr24')

    mirrored = image[:,::-1,:] if display_settings['mirror'] else image

    # image logic

    return av.VideoFrame.from_ndarray(mirrored, format='bgr24')

streamer = webrtc_streamer(
    key='surveillance',
    mode=WebRtcMode.SENDRECV,
    audio_frame_callback=audio_frame_callback,
    video_frame_callback=video_frame_callback,
    async_processing=True,
)