LakshmiHarika commited on
Commit
387eaca
·
verified ·
1 Parent(s): ffc025f

Update pages/Data Collection.py

Browse files
Files changed (1) hide show
  1. pages/Data Collection.py +166 -8
pages/Data Collection.py CHANGED
@@ -1020,7 +1020,7 @@ elif st.session_state.current_page == "video_processing":
1020
 
1021
  # Heading for cv2.read()
1022
  st.markdown("""
1023
- <h3 style="color: #e25822;">`vid.read()` Function</h3>
1024
  """, unsafe_allow_html=True)
1025
 
1026
  # Explanation for vid.read()
@@ -1033,15 +1033,10 @@ elif st.session_state.current_page == "video_processing":
1033
 
1034
  2. **`img`**: The actual frame (image) read from the video. This frame is returned as a NumPy array and can be processed just like any image.
1035
  """)
1036
-
1037
- # Heading for cv2.waitKey() Explanation
1038
- st.markdown("""
1039
- <h3 style="color: #e25822;">Understanding `cv2.waitKey()`</h3>
1040
- """, unsafe_allow_html=True)
1041
 
1042
  # Short Heading
1043
  st.markdown("""
1044
- <h3 style="color: #e25822;">Understanding `cv2.waitKey()`</h3>
1045
  """, unsafe_allow_html=True)
1046
 
1047
  # Explanation
@@ -1069,13 +1064,176 @@ elif st.session_state.current_page == "video_processing":
1069
  This stops the video when the 'q' key is pressed.
1070
  """)
1071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1073
 
 
 
 
 
1074
 
 
 
 
 
 
 
 
 
1075
 
 
 
 
 
 
 
 
 
 
1076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1077
 
1078
-
1079
 
1080
  elif st.session_state.current_page == "explore_audio":
1081
  st.markdown("""
 
1020
 
1021
  # Heading for cv2.read()
1022
  st.markdown("""
1023
+ <h3 style="color: #e25822;"> Understanding vid.read()</h3>
1024
  """, unsafe_allow_html=True)
1025
 
1026
  # Explanation for vid.read()
 
1033
 
1034
  2. **`img`**: The actual frame (image) read from the video. This frame is returned as a NumPy array and can be processed just like any image.
1035
  """)
 
 
 
 
 
1036
 
1037
  # Short Heading
1038
  st.markdown("""
1039
+ <h3 style="color: #e25822;">Understanding cv2.waitKey()</h3>
1040
  """, unsafe_allow_html=True)
1041
 
1042
  # Explanation
 
1064
  This stops the video when the 'q' key is pressed.
1065
  """)
1066
 
1067
+
1068
+ # Heading for Converting BGR to Grayscale
1069
+ st.markdown("""
1070
+ <h3 style="color: #9400d3;">Converting BGR Video to Grayscale</h3>
1071
+ """, unsafe_allow_html=True)
1072
+
1073
+ # Brief Explanation
1074
+ st.write("""
1075
+ You can handle video frames one at a time and process them as needed. The following example shows how to:
1076
+
1077
+ - Convert each frame of a video from BGR to grayscale.
1078
+ - Display both the original and grayscale video frames side by side.
1079
+ """)
1080
+
1081
+ # Code Example
1082
+ st.code("""
1083
+ vid = cv2.VideoCapture('path_to_video.mp4')
1084
+
1085
+ while True:
1086
+ succ, img = vid.read()
1087
+
1088
+ if succ == False:
1089
+ break
1090
+
1091
+ # Convert frame from BGR to grayscale
1092
+ img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
1093
+
1094
+ # Display the original (colored) and grayscale frames
1095
+ cv2.imshow("Colored Video", img)
1096
+ cv2.imshow("Grayscale Video", img1)
1097
+
1098
+ # Press 'q' to quit the video
1099
+ if cv2.waitKey(1) & 255 == ord("q"):
1100
+ break
1101
+
1102
+ cv2.destroyAllWindows()
1103
+ """, language="python")
1104
 
1105
+ # Heading for Splitting Video into Color Channels
1106
+ st.markdown("""
1107
+ <h3 style="color: #9400d3;">Splitting Colored Video into Different Channels</h3>
1108
+ """, unsafe_allow_html=True)
1109
+
1110
+ # Brief Explanation
1111
+ st.write("""
1112
+ Each frame of a colored video consists of three channels: Blue, Green, and Red (BGR).
1113
+ The following example demonstrates how to:
1114
+ - Split the video frames into separate Blue, Green, and Red channels.
1115
+ - Display the original video alongside each color channel.
1116
+ """)
1117
+
1118
+ # Code Example
1119
+ st.code("""
1120
+ vid = cv2.VideoCapture('path_to_video.mp4')
1121
+
1122
+ while True:
1123
+ succ, img = vid.read()
1124
+
1125
+ if succ == False:
1126
+ break
1127
+
1128
+ # Convert frame to grayscale
1129
+ img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
1130
+
1131
+ # Split the frame into B, G, R channels
1132
+ b, g, r = cv2.split(img)
1133
+ z = np.zeros(img.shape[:-1], dtype=np.uint8)
1134
+ blue_channel = cv2.merge([b, z, z])
1135
+ green_channel = cv2.merge([z, g, z])
1136
+ red_channel = cv2.merge([z, z, r])
1137
+
1138
+ # Display the frames
1139
+ cv2.imshow("Colored Video", img)
1140
+ cv2.imshow("Grayscale Video", img1)
1141
+ cv2.imshow("Blue Channel", blue_channel)
1142
+ cv2.imshow("Green Channel", green_channel)
1143
+ cv2.imshow("Red Channel", red_channel)
1144
+
1145
+ # Press 'q' to quit
1146
+ if cv2.waitKey(1) & 255 == ord("q"):
1147
+ break
1148
+
1149
+ cv2.destroyAllWindows()
1150
+ """, language="python")
1151
 
1152
+ # Heading for Capturing Frames via Webcam
1153
+ st.markdown("""
1154
+ <h3 style="color: #9400d3;">Capturing Frames While Live Streaming Using Webcam</h3>
1155
+ """, unsafe_allow_html=True)
1156
 
1157
+ # Brief Explanation
1158
+ st.write("""
1159
+ OpenCV allows you to access your webcam for live video streaming. The `cv2.VideoCapture()` function is used to activate the webcam. Here's how it works:
1160
+
1161
+ - **`cv2.VideoCapture(0)`**:
1162
+ - The argument `0` tells OpenCV to access the default webcam on your computer.
1163
+ - If you have multiple cameras, you can pass other IDs (like `1`, `2`) to access them.
1164
+ - It creates a connection with the webcam and starts capturing frames in real time.
1165
 
1166
+ The following example demonstrates how to:
1167
+ - Activate the webcam.
1168
+ - Display the live stream.
1169
+ - Close the webcam window by pressing the 'p' key.
1170
+ """)
1171
+
1172
+ # Code Example
1173
+ st.code("""
1174
+ vid = cv2.VideoCapture(0) # 0 indicates the default webcam
1175
 
1176
+ while True:
1177
+ succ, img = vid.read()
1178
+
1179
+ if succ == False: # Optional: Check if the webcam is working
1180
+ print("Camera not working")
1181
+ break
1182
+
1183
+ # Display the live stream
1184
+ cv2.imshow("Live Stream", img)
1185
+
1186
+ # Press 'p' to stop the live stream
1187
+ if cv2.waitKey(1) & 255 == ord("p"):
1188
+ break
1189
+
1190
+ vid.release()
1191
+ cv2.destroyAllWindows()
1192
+ """, language="python")
1193
+
1194
+
1195
+ # Heading for Capturing and Saving Frames
1196
+ st.markdown("""
1197
+ <h3 style="color: #9400d3;">Capturing and Saving Frames</h3>
1198
+ """, unsafe_allow_html=True)
1199
+
1200
+ # Brief Explanation
1201
+ st.write("""
1202
+ This code uses OpenCV to access the webcam, display the video feed, and save specific frames as image files:
1203
+ - **Webcam Activation**: The `cv2.VideoCapture(0)` function initializes the default webcam.
1204
+ - **Capturing Frames**: Press **'s'** to capture and save the current frame to a specified directory.
1205
+ - **Stopping the Stream**: Press **'p'** to stop the webcam and close the application.
1206
+ """)
1207
+
1208
+ # Code Example
1209
+ st.code("""
1210
+ vid = cv2.VideoCapture(0) # Open webcam
1211
+ c = 0 # Counter for naming saved images
1212
+
1213
+ while True:
1214
+ succ, img = vid.read()
1215
+
1216
+ if succ == False: # Check if the webcam is working
1217
+ print("Camera not working")
1218
+ break
1219
+
1220
+ cv2.imshow("Live Stream", img) # Display live stream
1221
+
1222
+ # Save frame as an image file when 's' is pressed
1223
+ if cv2.waitKey(1) & 255 == ord("s"):
1224
+ cv2.imwrite('path_to_save_directory/{}.jpg'.format(c), img)
1225
+ print("Image is captured and saved")
1226
+ c += 1 # Increment counter for next image name
1227
+
1228
+ # Quit live stream when 'p' is pressed
1229
+ if cv2.waitKey(1) & 255 == ord("p"):
1230
+ break
1231
+
1232
+ vid.release()
1233
+ cv2.destroyAllWindows()
1234
+ """, language="python")
1235
+
1236
 
 
1237
 
1238
  elif st.session_state.current_page == "explore_audio":
1239
  st.markdown("""