Update pages/Life_cycle_of_ML.py
Browse files- pages/Life_cycle_of_ML.py +128 -87
pages/Life_cycle_of_ML.py
CHANGED
|
@@ -873,12 +873,13 @@ def video_handling_dashboard():
|
|
| 873 |
"""
|
| 874 |
|
| 875 |
# **Title and Introduction**
|
| 876 |
-
st.title("OpenCV Video Handling
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
|
|
|
| 882 |
|
| 883 |
st.subheader("Basic Points for Video handling")
|
| 884 |
|
|
@@ -903,7 +904,8 @@ if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
| 903 |
cap.release()
|
| 904 |
cv2.destroyAllWindows()
|
| 905 |
""", language='python')
|
| 906 |
-
|
|
|
|
| 907 |
st.code("""import cv2
|
| 908 |
# Extracting a specific frame
|
| 909 |
cap = cv2.VideoCapture('your_video_file.mp4')
|
|
@@ -914,88 +916,127 @@ cap.release()
|
|
| 914 |
""", language='python')
|
| 915 |
|
| 916 |
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
st.
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
""")
|
| 925 |
-
st.
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
while cap.isOpened():
|
| 966 |
-
ret, frame = cap.read()
|
| 967 |
-
if not ret:
|
| 968 |
-
st.info("🎬 Video playback completed.")
|
| 969 |
-
break
|
| 970 |
-
|
| 971 |
-
# Apply filters
|
| 972 |
-
if apply_grayscale:
|
| 973 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 974 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) # Convert back to 3 channels for compatibility
|
| 975 |
-
if apply_edges:
|
| 976 |
-
edges = cv2.Canny(frame, 100, 200)
|
| 977 |
-
frame = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 978 |
-
|
| 979 |
-
# Convert frame to RGB (Streamlit compatible) and display
|
| 980 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 981 |
-
video_placeholder.image(frame, channels="RGB")
|
| 982 |
-
|
| 983 |
-
# Playback speed
|
| 984 |
-
cv2.waitKey(playback_speed)
|
| 985 |
-
|
| 986 |
-
cap.release()
|
| 987 |
-
st.success("✅ Video playback has ended.")
|
| 988 |
-
|
| 989 |
-
# Additional info for the app
|
| 990 |
-
st.markdown("""
|
| 991 |
-
---
|
| 992 |
-
### Features
|
| 993 |
-
- Choose between live webcam or uploaded video files.
|
| 994 |
-
- Apply filters such as grayscale or edge detection in real time.
|
| 995 |
-
- Control the playback speed with the slider.
|
| 996 |
-
---
|
| 997 |
-
Enjoy exploring OpenCV video handling!
|
| 998 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 999 |
|
| 1000 |
|
| 1001 |
|
|
|
|
| 873 |
"""
|
| 874 |
|
| 875 |
# **Title and Introduction**
|
| 876 |
+
st.title("OpenCV Video Handling Explanation and Examples")
|
| 877 |
+
# 1. What is a Video
|
| 878 |
+
st.header("What is a Video?")
|
| 879 |
+
st.write("""
|
| 880 |
+
A video is essentially a sequence of images (called frames) displayed in rapid succession to create the illusion of motion.
|
| 881 |
+
OpenCV allows us to process these frames sequentially for tasks like display, modification, or saving to a file.
|
| 882 |
+
""")
|
| 883 |
|
| 884 |
st.subheader("Basic Points for Video handling")
|
| 885 |
|
|
|
|
| 904 |
cap.release()
|
| 905 |
cv2.destroyAllWindows()
|
| 906 |
""", language='python')
|
| 907 |
+
|
| 908 |
+
st.header("Extracting Frames from Video:")
|
| 909 |
st.code("""import cv2
|
| 910 |
# Extracting a specific frame
|
| 911 |
cap = cv2.VideoCapture('your_video_file.mp4')
|
|
|
|
| 916 |
""", language='python')
|
| 917 |
|
| 918 |
|
| 919 |
+
|
| 920 |
+
|
| 921 |
+
# Example for `cv2.VideoCapture()`
|
| 922 |
+
st.header("1. cv2.VideoCapture()")
|
| 923 |
+
st.write("""
|
| 924 |
+
The `cv2.VideoCapture()` class is used to open video files or capture frames from a webcam feed.
|
| 925 |
+
Examples:
|
| 926 |
""")
|
| 927 |
+
st.code("""
|
| 928 |
+
# Open the default webcam
|
| 929 |
+
vid = cv2.VideoCapture(0)
|
| 930 |
+
|
| 931 |
+
# Open a video file
|
| 932 |
+
vid = cv2.VideoCapture("video.mp4")
|
| 933 |
+
""", language="python")
|
| 934 |
+
|
| 935 |
+
# Example for `vid.read()`
|
| 936 |
+
st.header("2. vid.read()")
|
| 937 |
+
st.write("""
|
| 938 |
+
The `vid.read()` method reads a frame from a video or webcam. It returns two values:
|
| 939 |
+
- **Boolean**: Whether the frame was read successfully.
|
| 940 |
+
- **Image**: The frame itself, which is a NumPy array.
|
| 941 |
+
Examples:
|
| 942 |
+
""")
|
| 943 |
+
st.code("""
|
| 944 |
+
success, frame = vid.read()
|
| 945 |
+
if success:
|
| 946 |
+
print("Frame read successfully")
|
| 947 |
+
""", language="python")
|
| 948 |
+
|
| 949 |
+
# Example for `cv2.waitKey()`
|
| 950 |
+
st.header("3. cv2.waitKey()")
|
| 951 |
+
st.write("""
|
| 952 |
+
The `cv2.waitKey()` method waits for a specific time (in milliseconds) and listens for keyboard input.
|
| 953 |
+
It's useful in displaying frames and responding to user input.
|
| 954 |
+
Examples:
|
| 955 |
+
- `cv2.waitKey(1)`: A short delay, used in video processing loops.
|
| 956 |
+
- `cv2.waitKey(0)`: Waits indefinitely for a key press.
|
| 957 |
+
""")
|
| 958 |
+
st.code("""
|
| 959 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 960 |
+
print("Exit key pressed")
|
| 961 |
+
""", language="python")
|
| 962 |
+
|
| 963 |
+
# Iterating Over Frames
|
| 964 |
+
st.header("4. Iterating Over Frames")
|
| 965 |
+
st.write("""
|
| 966 |
+
A video is a sequence of frames, so we typically process them in a loop:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 967 |
""")
|
| 968 |
+
st.code("""
|
| 969 |
+
while True:
|
| 970 |
+
success, frame = vid.read()
|
| 971 |
+
if not success: # End of the video
|
| 972 |
+
break
|
| 973 |
+
cv2.imshow("Frame", frame) # Display frame
|
| 974 |
+
if cv2.waitKey(1) & 0xFF == ord('q'): # Exit loop on 'q'
|
| 975 |
+
break
|
| 976 |
+
""", language="python")
|
| 977 |
+
|
| 978 |
+
# Writing Videos with `cv2.VideoWriter`
|
| 979 |
+
st.header("5. Writing Videos with cv2.VideoWriter")
|
| 980 |
+
st.write("""
|
| 981 |
+
OpenCV allows you to save processed frames into a video file using `cv2.VideoWriter`.
|
| 982 |
+
The parameters include:
|
| 983 |
+
- `output_path`: The file path to save the video.
|
| 984 |
+
- `fourcc`: Codec for compression (e.g., `'XVID'` for `.avi` files).
|
| 985 |
+
- `fps`: Frames per second.
|
| 986 |
+
- `frame_size`: Size of the frame (width, height).
|
| 987 |
+
Example:
|
| 988 |
+
""")
|
| 989 |
+
st.code("""
|
| 990 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
| 991 |
+
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
|
| 992 |
+
while True:
|
| 993 |
+
success, frame = vid.read()
|
| 994 |
+
if not success:
|
| 995 |
+
break
|
| 996 |
+
out.write(frame) # Write frame to video file
|
| 997 |
+
out.release()
|
| 998 |
+
""", language="python")
|
| 999 |
+
|
| 1000 |
+
# Releasing Resources
|
| 1001 |
+
st.header("6. Releasing Resources")
|
| 1002 |
+
st.write("""
|
| 1003 |
+
It's essential to release system resources like file handles and the webcam when processing is complete.
|
| 1004 |
+
Example:
|
| 1005 |
+
""")
|
| 1006 |
+
st.code("""
|
| 1007 |
+
vid.release() # Release video source
|
| 1008 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 1009 |
+
""", language="python")
|
| 1010 |
+
|
| 1011 |
+
# A Simple Demo using Streamlit
|
| 1012 |
+
st.header("Demo: Process Frames in Real-Time")
|
| 1013 |
+
st.write("""
|
| 1014 |
+
This example captures video from your webcam, processes frames in real-time, and displays them:
|
| 1015 |
+
""")
|
| 1016 |
+
st.code("""
|
| 1017 |
+
import cv2
|
| 1018 |
+
import streamlit as st
|
| 1019 |
+
|
| 1020 |
+
vid = cv2.VideoCapture(0)
|
| 1021 |
+
stframe = st.empty()
|
| 1022 |
+
|
| 1023 |
+
while True:
|
| 1024 |
+
success, frame = vid.read()
|
| 1025 |
+
if not success:
|
| 1026 |
+
st.warning("Webcam not accessible")
|
| 1027 |
+
break
|
| 1028 |
+
|
| 1029 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR to RGB for Streamlit
|
| 1030 |
+
stframe.image(frame, use_column_width=True)
|
| 1031 |
+
|
| 1032 |
+
if cv2.waitKey(1) & 0xFF == ord('q'): # Exit on pressing 'q'
|
| 1033 |
+
break
|
| 1034 |
+
|
| 1035 |
+
vid.release()
|
| 1036 |
+
cv2.destroyAllWindows()
|
| 1037 |
+
""", language="python")
|
| 1038 |
+
|
| 1039 |
+
st.write("Use the code above to see the webcam in real time using Streamlit!")
|
| 1040 |
|
| 1041 |
|
| 1042 |
|