Update pages/Life_cycle_of_ML.py
Browse files- pages/Life_cycle_of_ML.py +58 -73
pages/Life_cycle_of_ML.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
|
| 7 |
def main_page():
|
| 8 |
# Title and Introduction
|
|
@@ -916,92 +916,77 @@ cap.release()
|
|
| 916 |
|
| 917 |
st.subheader("Lets Go Pratical stuff")
|
| 918 |
# Step 1: Choose Video Source
|
| 919 |
-
|
| 920 |
st.markdown("""
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
""")
|
|
|
|
| 925 |
|
| 926 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 927 |
|
| 928 |
if video_source == "Webcam":
|
| 929 |
source = 0 # Default webcam
|
| 930 |
-
st.
|
| 931 |
else:
|
| 932 |
-
source = st.
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
vid = cv2.VideoCapture("video.mp4") # Opens a video file
|
| 940 |
-
''', language='python')
|
| 941 |
|
| 942 |
# Step 2: Apply Video Processing
|
| 943 |
-
st.header("Step 2:
|
| 944 |
-
apply_filter = st.checkbox("
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
if success:
|
| 952 |
-
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 953 |
-
''', language='python')
|
| 954 |
-
|
| 955 |
-
# Step 3: Display the Video
|
| 956 |
-
st.header("Step 3: View Video")
|
| 957 |
-
st.markdown("""
|
| 958 |
-
Iterates over video frames using a loop. Each frame is displayed in the Streamlit app:
|
| 959 |
-
- Press **Stop** to terminate the video playback.
|
| 960 |
-
""")
|
| 961 |
-
|
| 962 |
-
if st.button("Start Video Playback"):
|
| 963 |
-
vid = cv2.VideoCapture(source) # Open video source
|
| 964 |
-
stframe = st.empty() # Placeholder for video frames
|
| 965 |
-
|
| 966 |
-
while vid.isOpened():
|
| 967 |
-
success, frame = vid.read()
|
| 968 |
-
if not success:
|
| 969 |
-
st.error("Failed to load video source. Video playback has ended.")
|
| 970 |
-
break
|
| 971 |
-
|
| 972 |
-
# Apply filter if selected
|
| 973 |
if apply_filter:
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 983 |
|
| 984 |
-
#
|
| 985 |
-
st.header("Step 4: Save Processed Video")
|
| 986 |
st.markdown("""
|
| 987 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 988 |
""")
|
| 989 |
-
st.code('''
|
| 990 |
-
fourcc = cv2.VideoWriter_fourcc(*'XVID') # Codec
|
| 991 |
-
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # Output file setup
|
| 992 |
-
''', language='python')
|
| 993 |
|
| 994 |
-
# Step 5: Release Resources
|
| 995 |
-
st.header("Step 5: Release Resources")
|
| 996 |
-
st.markdown("""
|
| 997 |
-
Always release the video source and any OpenCV windows after processing:
|
| 998 |
-
- Use `vid.release()` to release resources.
|
| 999 |
-
- Use `cv2.destroyAllWindows()` to close any OpenCV-created windows.
|
| 1000 |
-
""")
|
| 1001 |
-
st.code('''
|
| 1002 |
-
vid.release() # Release video source
|
| 1003 |
-
cv2.destroyAllWindows() # Close any windows
|
| 1004 |
-
''', language='python')
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
import pandas as pd
|
| 5 |
+
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
| 6 |
|
| 7 |
def main_page():
|
| 8 |
# Title and Introduction
|
|
|
|
| 916 |
|
| 917 |
st.subheader("Lets Go Pratical stuff")
|
| 918 |
# Step 1: Choose Video Source
|
| 919 |
+
# Title and Introduction
|
| 920 |
st.markdown("""
|
| 921 |
+
### What is a Video?
|
| 922 |
+
A video is a sequence of frames displayed rapidly to create the illusion of motion.
|
| 923 |
+
This app demonstrates how to handle video using OpenCV with real-time frame processing.
|
| 924 |
""")
|
| 925 |
+
st.info("π‘ Use the controls below to customize your video experience!")
|
| 926 |
|
| 927 |
+
# Step 1: Choose Video Source
|
| 928 |
+
st.header("π Step 1: Select Your Video Source")
|
| 929 |
+
video_source = st.radio(
|
| 930 |
+
"Choose the source of the video:",
|
| 931 |
+
["Webcam", "Video File"],
|
| 932 |
+
help="Select Webcam for live feed or upload a video file."
|
| 933 |
+
)
|
| 934 |
|
| 935 |
if video_source == "Webcam":
|
| 936 |
source = 0 # Default webcam
|
| 937 |
+
st.success("π’ Default Webcam Selected")
|
| 938 |
else:
|
| 939 |
+
source = st.file_uploader(
|
| 940 |
+
"Upload a video file:", type=["mp4", "avi", "mov"],
|
| 941 |
+
help="Supports common video file formats (e.g., MP4, AVI)."
|
| 942 |
+
)
|
| 943 |
+
if source is None:
|
| 944 |
+
st.warning("β οΈ Please upload a valid video file to proceed.")
|
| 945 |
+
return
|
|
|
|
|
|
|
| 946 |
|
| 947 |
# Step 2: Apply Video Processing
|
| 948 |
+
st.header("π¨ Step 2: Apply Filters to Video")
|
| 949 |
+
apply_filter = st.checkbox("Enable Grayscale Filter", help="Converts the video to grayscale.")
|
| 950 |
+
playback_speed = st.slider("Playback Speed (ms per frame):", 10, 200, 50, help="Adjust the delay between frames.")
|
| 951 |
+
|
| 952 |
+
# Step 3: Define VideoTransformer Class for Processing
|
| 953 |
+
class VideoTransformer(VideoTransformerBase):
|
| 954 |
+
def transform(self, frame):
|
| 955 |
+
img = frame.to_ndarray(format="bgr24")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 956 |
if apply_filter:
|
| 957 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 958 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 959 |
+
return img
|
| 960 |
+
|
| 961 |
+
# Step 4: Real-time Playback and Status Updates
|
| 962 |
+
st.header("π₯ Step 3: View Video Stream")
|
| 963 |
+
start_video = st.button("π Start Video Stream")
|
| 964 |
+
stop_video = st.button("β Stop Video Stream")
|
| 965 |
+
|
| 966 |
+
# Real-time video interaction
|
| 967 |
+
if start_video:
|
| 968 |
+
st.info("Loading video, please wait...")
|
| 969 |
+
webrtc_streamer(
|
| 970 |
+
key="example",
|
| 971 |
+
video_transformer_factory=VideoTransformer if apply_filter else None,
|
| 972 |
+
async_processing=False,
|
| 973 |
+
)
|
| 974 |
+
st.success("π¬ Streaming started! Adjust filters and enjoy!")
|
| 975 |
+
elif stop_video:
|
| 976 |
+
st.warning("π Video stream stopped. Click Start to reload.")
|
| 977 |
+
st.stop()
|
| 978 |
|
| 979 |
+
# Summary Footer
|
|
|
|
| 980 |
st.markdown("""
|
| 981 |
+
---
|
| 982 |
+
**App Features:**
|
| 983 |
+
- Supports live webcam feed or video files.
|
| 984 |
+
- Real-time filtering (e.g., grayscale).
|
| 985 |
+
- Interactive controls for playback speed and effects.
|
| 986 |
+
---
|
| 987 |
+
**Enjoy exploring video handling with OpenCV in Streamlit!**
|
| 988 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 989 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
|
| 991 |
|
| 992 |
|