Spaces:
Sleeping
Sleeping
srivatsavdamaraju
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def main():
|
6 |
+
st.set_page_config(page_title="Streamlit Webcam Canny Edge App")
|
7 |
+
st.title("Webcam Canny Edge Detection App")
|
8 |
+
st.caption("Powered by OpenCV, Streamlit")
|
9 |
+
|
10 |
+
# Capture video from the webcam
|
11 |
+
cap = cv2.VideoCapture(0)
|
12 |
+
frame_placeholder = st.empty()
|
13 |
+
stop_button_pressed = st.button("Stop")
|
14 |
+
|
15 |
+
while cap.isOpened() and not stop_button_pressed:
|
16 |
+
ret, frame = cap.read()
|
17 |
+
if not ret:
|
18 |
+
st.write("Video Capture Ended")
|
19 |
+
break
|
20 |
+
|
21 |
+
# Convert the frame to grayscale
|
22 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
23 |
+
|
24 |
+
# Apply Canny edge detection
|
25 |
+
edges = cv2.Canny(gray_frame, 100, 200)
|
26 |
+
|
27 |
+
# Create an overlay of edges on the original frame
|
28 |
+
edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
29 |
+
overlay = cv2.addWeighted(frame, 0.7, edges_colored, 0.3, 0)
|
30 |
+
|
31 |
+
# Resize the frame for better display (smaller size)
|
32 |
+
overlay_resized = cv2.resize(overlay, (320, 240)) # Change size here
|
33 |
+
|
34 |
+
# Display the overlay
|
35 |
+
frame_placeholder.image(overlay_resized, channels="BGR", caption="Webcam Feed with Canny Edges", use_column_width=True)
|
36 |
+
|
37 |
+
# Break loop on 'q' key or stop button press
|
38 |
+
if cv2.waitKey(1) & 0xFF == ord("q") or stop_button_pressed:
|
39 |
+
break
|
40 |
+
|
41 |
+
cap.release()
|
42 |
+
cv2.destroyAllWindows()
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
main()
|