Update pages/Life_cycle_of_ML.py
Browse files- pages/Life_cycle_of_ML.py +150 -31
pages/Life_cycle_of_ML.py
CHANGED
|
@@ -160,39 +160,10 @@ print(df_sheet1.head())
|
|
| 160 |
Unstructured data requires advanced methods like natural language processing (NLP) or computer vision to make sense of the data and extract valuable insights.
|
| 161 |
""")
|
| 162 |
# Video Button Section
|
| 163 |
-
if st.button("Video"):
|
| 164 |
-
st.header("Video")
|
| 165 |
-
st.subheader('Install Dependencies:')
|
| 166 |
-
st.code("""pip install opencv-python moviepy""", language='python')
|
| 167 |
-
st.subheader("Reading a Video File: ")
|
| 168 |
-
st.code("""import cv2
|
| 169 |
-
# Open the video file
|
| 170 |
-
video_path = 'your_video_file.mp4' # Replace with your file path
|
| 171 |
-
cap = cv2.VideoCapture(video_path)
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
ret, frame = cap.read()
|
| 176 |
-
if not ret:
|
| 177 |
-
break
|
| 178 |
-
# Display the frame (you can also process it)
|
| 179 |
-
cv2.imshow('Video Frame', frame)
|
| 180 |
|
| 181 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 182 |
-
break
|
| 183 |
-
cap.release()
|
| 184 |
-
cv2.destroyAllWindows()
|
| 185 |
-
""", language='python')
|
| 186 |
-
st.subheader("Extracting Frames from Video:")
|
| 187 |
-
st.code("""import cv2
|
| 188 |
-
# Extracting a specific frame
|
| 189 |
-
cap = cv2.VideoCapture('your_video_file.mp4')
|
| 190 |
-
ret, frame = cap.read() # Read the first frame
|
| 191 |
-
if ret:
|
| 192 |
-
cv2.imwrite('frame_1.jpg', frame) # Save the frame as an image
|
| 193 |
-
cap.release()
|
| 194 |
-
""", language='python')
|
| 195 |
-
|
| 196 |
|
| 197 |
|
| 198 |
if st.button("Lets Play with Images"):
|
|
@@ -892,7 +863,153 @@ def animations():
|
|
| 892 |
- **8**: The thickness of the text stroke.
|
| 893 |
""")
|
| 894 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 895 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 896 |
|
| 897 |
if 'page' not in st.session_state:
|
| 898 |
st.session_state.page = 'main'
|
|
@@ -910,6 +1027,8 @@ elif st.session_state.page == 'Affine':
|
|
| 910 |
affine_transformation_demo()
|
| 911 |
elif st.session_state.page == 'Animation':
|
| 912 |
animations()
|
|
|
|
|
|
|
| 913 |
|
| 914 |
st.markdown("---")
|
| 915 |
|
|
|
|
| 160 |
Unstructured data requires advanced methods like natural language processing (NLP) or computer vision to make sense of the data and extract valuable insights.
|
| 161 |
""")
|
| 162 |
# Video Button Section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
if st.button("Lets Known about videos"):
|
| 165 |
+
st.session_state.page = "videos"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
if st.button("Lets Play with Images"):
|
|
|
|
| 863 |
- **8**: The thickness of the text stroke.
|
| 864 |
""")
|
| 865 |
|
| 866 |
+
|
| 867 |
+
|
| 868 |
+
def video_handling_dashboard():
|
| 869 |
+
"""
|
| 870 |
+
OpenCV Video Handling Dashboard:
|
| 871 |
+
- Showcases playing, processing, and streaming videos in Streamlit.
|
| 872 |
+
- Explains fundamental concepts and uses with code examples.
|
| 873 |
+
"""
|
| 874 |
+
|
| 875 |
+
# **Title and Introduction**
|
| 876 |
+
st.title("OpenCV Video Handling in Streamlit")
|
| 877 |
+
st.write(
|
| 878 |
+
"### What is a Video?\n"
|
| 879 |
+
"A video is essentially a sequence of images (frames) displayed rapidly to create the illusion of motion. "
|
| 880 |
+
"Each frame is an image, and videos are handled by processing these frames sequentially."
|
| 881 |
+
)
|
| 882 |
+
|
| 883 |
+
st.subheader("Basic Points for Video handling")
|
| 884 |
|
| 885 |
+
st.subheader('Install Dependencies:')
|
| 886 |
+
st.code("""pip install opencv-python moviepy""", language='python')
|
| 887 |
+
st.subheader("Reading a Video File: ")
|
| 888 |
+
st.code("""import cv2
|
| 889 |
+
# Open the video file
|
| 890 |
+
video_path = 'your_video_file.mp4' # Replace with your file path
|
| 891 |
+
cap = cv2.VideoCapture(video_path)
|
| 892 |
+
|
| 893 |
+
# Read the video frame by frame
|
| 894 |
+
while cap.isOpened():
|
| 895 |
+
ret, frame = cap.read()
|
| 896 |
+
if not ret:
|
| 897 |
+
break
|
| 898 |
+
# Display the frame (you can also process it)
|
| 899 |
+
cv2.imshow('Video Frame', frame)
|
| 900 |
+
|
| 901 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 902 |
+
break
|
| 903 |
+
cap.release()
|
| 904 |
+
cv2.destroyAllWindows()
|
| 905 |
+
""", language='python')
|
| 906 |
+
st.subheader("Extracting Frames from Video:")
|
| 907 |
+
st.code("""import cv2
|
| 908 |
+
# Extracting a specific frame
|
| 909 |
+
cap = cv2.VideoCapture('your_video_file.mp4')
|
| 910 |
+
ret, frame = cap.read() # Read the first frame
|
| 911 |
+
if ret:
|
| 912 |
+
cv2.imwrite('frame_1.jpg', frame) # Save the frame as an image
|
| 913 |
+
cap.release()
|
| 914 |
+
""", language='python')
|
| 915 |
+
|
| 916 |
+
|
| 917 |
+
st.subheader("Lets Go Pratical stuff")
|
| 918 |
+
# **Step 1: Choose Video Source**
|
| 919 |
+
st.header("1. Choose Video Source")
|
| 920 |
+
st.write("We use `cv2.VideoCapture()` to open a video file or access a webcam feed.")
|
| 921 |
+
video_source = st.radio(
|
| 922 |
+
"Select the video input method:",
|
| 923 |
+
options=["Webcam", "Video File"],
|
| 924 |
+
)
|
| 925 |
+
|
| 926 |
+
if video_source == "Webcam":
|
| 927 |
+
source = 0 # Default webcam
|
| 928 |
+
st.write("Using `cv2.VideoCapture(0)` to access the default system camera.")
|
| 929 |
+
else:
|
| 930 |
+
source = st.text_input("Enter the path to your video file (e.g., 'video.mp4')", "video.mp4")
|
| 931 |
+
if not source:
|
| 932 |
+
st.warning("Please enter a valid file path to continue.")
|
| 933 |
+
st.stop()
|
| 934 |
+
|
| 935 |
+
# Display the code for Step 1
|
| 936 |
+
st.code('''
|
| 937 |
+
vid = cv2.VideoCapture(0) # Open default webcam
|
| 938 |
+
vid = cv2.VideoCapture("video.mp4") # Open a video file
|
| 939 |
+
''', language='python')
|
| 940 |
+
|
| 941 |
+
# **Step 2: Apply Video Processing**
|
| 942 |
+
st.header("2. Apply Video Processing")
|
| 943 |
+
apply_filter = st.checkbox("Apply Grayscale Filter to the Video")
|
| 944 |
+
st.write(
|
| 945 |
+
"`vid.read()` is used to read frames, and we can apply filters (e.g., grayscale) to process these frames sequentially."
|
| 946 |
+
)
|
| 947 |
+
|
| 948 |
+
st.code('''
|
| 949 |
+
success, img = vid.read()
|
| 950 |
+
if success:
|
| 951 |
+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 952 |
+
''', language='python')
|
| 953 |
+
|
| 954 |
+
# Define VideoTransformer for processing
|
| 955 |
+
class VideoTransformer(VideoTransformerBase):
|
| 956 |
+
def transform(self, frame):
|
| 957 |
+
img = frame.to_ndarray(format="bgr24")
|
| 958 |
+
if apply_filter:
|
| 959 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 960 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 961 |
+
return img
|
| 962 |
+
|
| 963 |
+
# **Step 3: Stream Video**
|
| 964 |
+
st.header("3. View Your Video Stream")
|
| 965 |
+
st.write(
|
| 966 |
+
"`cv2.waitKey(1)` is used to control the delay between frames and handle keypress events.\n"
|
| 967 |
+
"For example, press 'q' to stop streaming."
|
| 968 |
+
)
|
| 969 |
+
if source:
|
| 970 |
+
st.write("Press **Start** below to begin streaming.")
|
| 971 |
+
webrtc_streamer(
|
| 972 |
+
key="video-example",
|
| 973 |
+
video_transformer_factory=VideoTransformer if apply_filter else None,
|
| 974 |
+
)
|
| 975 |
+
else:
|
| 976 |
+
st.error("Please select or enter a valid source.")
|
| 977 |
+
|
| 978 |
+
st.code('''
|
| 979 |
+
while True:
|
| 980 |
+
success, frame = vid.read()
|
| 981 |
+
if not success:
|
| 982 |
+
break
|
| 983 |
+
cv2.imshow("Video Frame", frame)
|
| 984 |
+
if cv2.waitKey(1) & 0xFF == ord('q'): # Quit with 'q'
|
| 985 |
+
break
|
| 986 |
+
''', language='python')
|
| 987 |
+
|
| 988 |
+
# **Step 4: Writing Videos**
|
| 989 |
+
st.header("4. Write Processed Video to File")
|
| 990 |
+
st.write(
|
| 991 |
+
"Use `cv2.VideoWriter` to save the processed frames as a video file. "
|
| 992 |
+
"It requires specifying the output path, codec, FPS, and frame size."
|
| 993 |
+
)
|
| 994 |
+
st.code('''
|
| 995 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
| 996 |
+
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
|
| 997 |
+
''', language='python')
|
| 998 |
+
|
| 999 |
+
# **Step 5: Release Resources**
|
| 1000 |
+
st.header("5. Releasing Resources")
|
| 1001 |
+
st.write(
|
| 1002 |
+
"Properly release system resources (e.g., camera or file handles) using `vid.release()` "
|
| 1003 |
+
"and destroy any OpenCV windows with `cv2.destroyAllWindows()`."
|
| 1004 |
+
)
|
| 1005 |
+
st.code('''
|
| 1006 |
+
vid.release() # Release the video or camera source
|
| 1007 |
+
out.release() # Release the VideoWriter object
|
| 1008 |
+
cv2.destroyAllWindows()
|
| 1009 |
+
''', language='python')
|
| 1010 |
+
|
| 1011 |
+
|
| 1012 |
+
|
| 1013 |
|
| 1014 |
if 'page' not in st.session_state:
|
| 1015 |
st.session_state.page = 'main'
|
|
|
|
| 1027 |
affine_transformation_demo()
|
| 1028 |
elif st.session_state.page == 'Animation':
|
| 1029 |
animations()
|
| 1030 |
+
elif st.session_state.page == 'videos':
|
| 1031 |
+
video_handling_dashboard()
|
| 1032 |
|
| 1033 |
st.markdown("---")
|
| 1034 |
|