Phaneendrabayi commited on
Commit
59e3e01
Β·
verified Β·
1 Parent(s): 3d70522

Update pages/Life_cycle_of_ML.py

Browse files
Files changed (1) hide show
  1. 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
- st.header("Step 1: Open a Video Source")
920
  st.markdown("""
921
- `cv2.VideoCapture()` is used to open video sources:
922
- - `0`: Accesses the default system webcam.
923
- - File path: Opens a video file (e.g., `'video.mp4'`).
924
  """)
 
925
 
926
- video_source = st.radio("Select your video input method:", ["Webcam", "Video File"])
 
 
 
 
 
 
927
 
928
  if video_source == "Webcam":
929
  source = 0 # Default webcam
930
- st.info("Using webcam via `cv2.VideoCapture(0)`.")
931
  else:
932
- source = st.text_input("Enter the path to your video file (e.g., 'video.mp4')", "video.mp4")
933
- if not source:
934
- st.warning("Please provide a valid file path to continue.")
935
- st.stop()
936
-
937
- st.code('''
938
- vid = cv2.VideoCapture(0) # Opens the default webcam
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: Process Video Frames")
944
- apply_filter = st.checkbox("Apply Grayscale Filter to Frames")
945
-
946
- st.markdown("""
947
- `vid.read()` retrieves video frames sequentially. Optionally, filters (e.g., grayscale) can be applied.
948
- """)
949
- st.code('''
950
- success, frame = vid.read()
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
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
975
- frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
976
-
977
- # Convert the frame to RGB (Streamlit compatible)
978
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
979
- stframe.image(frame, channels="RGB", use_column_width=True)
980
-
981
- vid.release()
982
- st.success("Video playback completed.")
 
 
 
 
 
 
 
 
 
 
 
 
983
 
984
- # Step 4: Write Processed Video
985
- st.header("Step 4: Save Processed Video")
986
  st.markdown("""
987
- Use `cv2.VideoWriter` to save processed video frames to a file.
 
 
 
 
 
 
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