File size: 3,255 Bytes
2628138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3696e2
9ad57a5
2628138
9ad57a5
 
 
 
 
2628138
9ad57a5
 
 
 
 
2628138
9ad57a5
 
 
 
 
 
 
2628138
9ad57a5
 
 
2628138
9ad57a5
 
2628138
9ad57a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2628138
9ad57a5
 
 
 
2628138
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# import cv2
# import streamlit as st
# from streamlit_webrtc import webrtc_streamer, VideoHTMLAttributes
# import numpy as np
# import av

# st.title("OpenCV Filters on Video Stream")

# filter = "none"


# def transform(frame: av.VideoFrame):
#     img = frame.to_ndarray(format="bgr24")

#     if filter == "blur":
#         img = cv2.GaussianBlur(img, (21, 21), 0)
#     elif filter == "canny":
#         img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
#     elif filter == "grayscale":
#         # We convert the image twice because the first conversion returns a 2D array.
#         # the second conversion turns it back to a 3D array.
#         img = cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
#     elif filter == "sepia":
#         kernel = np.array(
#             [[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
#         )
#         img = cv2.transform(img, kernel)
#     elif filter == "invert":
#         img = cv2.bitwise_not(img)
#     elif filter == "none":
#         pass

#     return av.VideoFrame.from_ndarray(img, format="bgr24")


# col1, col2, col3, col4, col5, col6 = st.columns([1, 1, 1, 1, 1, 1])

# with col1:
#     if st.button("None"):
#         filter = "none"
# with col2:
#     if st.button("Blur"):
#         filter = "blur"
# with col3:
#     if st.button("Grayscale"):
#         filter = "grayscale"
# with col4:
#     if st.button("Sepia"):
#         filter = "sepia"
# with col5:
#     if st.button("Canny"):
#         filter = "canny"
# with col6:
#     if st.button("Invert"):
#         filter = "invert"


# webrtc_streamer(
#     key="streamer",
#     video_frame_callback=transform,
#     sendback_audio=False
#     )

import cv2
import streamlit as st
from streamlit_webrtc import webrtc_streamer
import numpy as np
import av

st.title("OpenCV Filters on Video Stream")

# Define the filter state variable
filter = "none"

def transform(frame: av.VideoFrame):
    img = frame.to_ndarray(format="bgr24")

    # Apply the selected filter
    if filter == "blur":
        img = cv2.GaussianBlur(img, (21, 21), 0)
    elif filter == "canny":
        img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
    elif filter == "grayscale":
        img = cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
    elif filter == "sepia":
        kernel = np.array([[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]])
        img = cv2.transform(img, kernel)
    elif filter == "invert":
        img = cv2.bitwise_not(img)
    
    return av.VideoFrame.from_ndarray(img, format="bgr24")

# Streamlit buttons to choose filters
col1, col2, col3, col4, col5, col6 = st.columns([1, 1, 1, 1, 1, 1])

with col1:
    if st.button("None"):
        filter = "none"
with col2:
    if st.button("Blur"):
        filter = "blur"
with col3:
    if st.button("Grayscale"):
        filter = "grayscale"
with col4:
    if st.button("Sepia"):
        filter = "sepia"
with col5:
    if st.button("Canny"):
        filter = "canny"
with col6:
    if st.button("Invert"):
        filter = "invert"

# Display the video stream
webrtc_streamer(
    key="streamer",
    video_frame_callback=transform,
    sendback_audio=False
)