AndrewLam489 commited on
Commit
a3696e2
·
verified ·
1 Parent(s): d563616

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -61
app.py CHANGED
@@ -1,62 +1,87 @@
1
- import cv2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
- from streamlit_webrtc import webrtc_streamer, VideoHTMLAttributes
4
- import numpy as np
5
- import av
6
-
7
- st.title("OpenCV Filters on Video Stream")
8
-
9
- filter = "none"
10
-
11
-
12
- def transform(frame: av.VideoFrame):
13
- img = frame.to_ndarray(format="bgr24")
14
-
15
- if filter == "blur":
16
- img = cv2.GaussianBlur(img, (21, 21), 0)
17
- elif filter == "canny":
18
- img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
19
- elif filter == "grayscale":
20
- # We convert the image twice because the first conversion returns a 2D array.
21
- # the second conversion turns it back to a 3D array.
22
- img = cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
23
- elif filter == "sepia":
24
- kernel = np.array(
25
- [[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
26
- )
27
- img = cv2.transform(img, kernel)
28
- elif filter == "invert":
29
- img = cv2.bitwise_not(img)
30
- elif filter == "none":
31
- pass
32
-
33
- return av.VideoFrame.from_ndarray(img, format="bgr24")
34
-
35
-
36
- col1, col2, col3, col4, col5, col6 = st.columns([1, 1, 1, 1, 1, 1])
37
-
38
- with col1:
39
- if st.button("None"):
40
- filter = "none"
41
- with col2:
42
- if st.button("Blur"):
43
- filter = "blur"
44
- with col3:
45
- if st.button("Grayscale"):
46
- filter = "grayscale"
47
- with col4:
48
- if st.button("Sepia"):
49
- filter = "sepia"
50
- with col5:
51
- if st.button("Canny"):
52
- filter = "canny"
53
- with col6:
54
- if st.button("Invert"):
55
- filter = "invert"
56
-
57
-
58
- webrtc_streamer(
59
- key="streamer",
60
- video_frame_callback=transform,
61
- sendback_audio=False
62
- )
 
1
+ # import cv2
2
+ # import streamlit as st
3
+ # from streamlit_webrtc import webrtc_streamer, VideoHTMLAttributes
4
+ # import numpy as np
5
+ # import av
6
+
7
+ # st.title("OpenCV Filters on Video Stream")
8
+
9
+ # filter = "none"
10
+
11
+
12
+ # def transform(frame: av.VideoFrame):
13
+ # img = frame.to_ndarray(format="bgr24")
14
+
15
+ # if filter == "blur":
16
+ # img = cv2.GaussianBlur(img, (21, 21), 0)
17
+ # elif filter == "canny":
18
+ # img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
19
+ # elif filter == "grayscale":
20
+ # # We convert the image twice because the first conversion returns a 2D array.
21
+ # # the second conversion turns it back to a 3D array.
22
+ # img = cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
23
+ # elif filter == "sepia":
24
+ # kernel = np.array(
25
+ # [[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
26
+ # )
27
+ # img = cv2.transform(img, kernel)
28
+ # elif filter == "invert":
29
+ # img = cv2.bitwise_not(img)
30
+ # elif filter == "none":
31
+ # pass
32
+
33
+ # return av.VideoFrame.from_ndarray(img, format="bgr24")
34
+
35
+
36
+ # col1, col2, col3, col4, col5, col6 = st.columns([1, 1, 1, 1, 1, 1])
37
+
38
+ # with col1:
39
+ # if st.button("None"):
40
+ # filter = "none"
41
+ # with col2:
42
+ # if st.button("Blur"):
43
+ # filter = "blur"
44
+ # with col3:
45
+ # if st.button("Grayscale"):
46
+ # filter = "grayscale"
47
+ # with col4:
48
+ # if st.button("Sepia"):
49
+ # filter = "sepia"
50
+ # with col5:
51
+ # if st.button("Canny"):
52
+ # filter = "canny"
53
+ # with col6:
54
+ # if st.button("Invert"):
55
+ # filter = "invert"
56
+
57
+
58
+ # webrtc_streamer(
59
+ # key="streamer",
60
+ # video_frame_callback=transform,
61
+ # sendback_audio=False
62
+ # )
63
+
64
  import streamlit as st
65
+ import cv2
66
+
67
+ def main():
68
+ st.set_page_config(page_title="Streamlit WebCam App")
69
+ st.title("Webcam Display Steamlit App")
70
+ st.caption("Powered by OpenCV, Streamlit")
71
+ cap = cv2.VideoCapture(0)
72
+ frame_placeholder = st.empty()
73
+ stop_button_pressed = st.button("Stop")
74
+ while cap.isOpened() and not stop_button_pressed:
75
+ ret, frame = cap.read()
76
+ if not ret:
77
+ st.write("Video Capture Ended")
78
+ break
79
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
80
+ frame_placeholder.image(frame,channels="RGB")
81
+ if cv2.waitKey(1) & 0xFF == ord("q") or stop_button_pressed:
82
+ break
83
+ cap.release()
84
+ cv2.destroyAllWindows()
85
+
86
+ if __name__ == "__main__":
87
+ main()