Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +87 -0
pages/Data Collection.py
CHANGED
|
@@ -983,6 +983,93 @@ elif st.session_state.current_page == "video_processing":
|
|
| 983 |
Once the video is loaded, OpenCV processes each frame in a loop until the video ends or the user stops it. You can apply image processing techniques to each frame, such as transformations, filtering, or object detection, before displaying or saving the modified video.
|
| 984 |
""")
|
| 985 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 986 |
|
| 987 |
|
| 988 |
|
|
|
|
| 983 |
Once the video is loaded, OpenCV processes each frame in a loop until the video ends or the user stops it. You can apply image processing techniques to each frame, such as transformations, filtering, or object detection, before displaying or saving the modified video.
|
| 984 |
""")
|
| 985 |
|
| 986 |
+
import cv2
|
| 987 |
+
import streamlit as st
|
| 988 |
+
|
| 989 |
+
# Heading for Playing Videos with OpenCV
|
| 990 |
+
st.markdown("""
|
| 991 |
+
<h3 style="color: #9400d3;">Playing Videos with OpenCV</h3>
|
| 992 |
+
""", unsafe_allow_html=True)
|
| 993 |
+
|
| 994 |
+
# Explanation
|
| 995 |
+
st.write("""
|
| 996 |
+
To play a video using OpenCV, we load the video with **`cv2.VideoCapture()`** and display each frame using **`cv2.imshow()`**. You can stop the video by pressing 'q'.
|
| 997 |
+
""")
|
| 998 |
+
|
| 999 |
+
# Code example for playing a video
|
| 1000 |
+
st.code("""
|
| 1001 |
+
|
| 1002 |
+
# Load the video
|
| 1003 |
+
vid = cv2.VideoCapture('path_to_video.mp4')
|
| 1004 |
+
|
| 1005 |
+
# Loop to read frames
|
| 1006 |
+
while True:
|
| 1007 |
+
succ, img = vid.read() # Read each frame
|
| 1008 |
+
|
| 1009 |
+
if not succ: # Exit if no frame is read
|
| 1010 |
+
break
|
| 1011 |
+
|
| 1012 |
+
cv2.imshow("video", img) # Show the frame
|
| 1013 |
+
|
| 1014 |
+
# Press 'q' to quit
|
| 1015 |
+
if cv2.waitKey(1) & 255 == ord("q"):
|
| 1016 |
+
break
|
| 1017 |
+
|
| 1018 |
+
# Release resources and close window
|
| 1019 |
+
cv2.destroyAllWindows()
|
| 1020 |
+
""", language="python")
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
# Heading for cv2.read()
|
| 1024 |
+
st.markdown("""
|
| 1025 |
+
<h3 style="color: #e25822;">`vid.read()` Function</h3>
|
| 1026 |
+
""", unsafe_allow_html=True)
|
| 1027 |
+
|
| 1028 |
+
# Explanation for vid.read()
|
| 1029 |
+
st.write("""
|
| 1030 |
+
The **`vid.read()`** function is used to read one frame at a time from the video file.
|
| 1031 |
+
It returns two values:
|
| 1032 |
+
1. **`succ`**: A boolean that indicates whether the frame was successfully read.
|
| 1033 |
+
- **True** if the frame was read successfully.
|
| 1034 |
+
- **False** if the frame could not be read (usually when the video ends).
|
| 1035 |
+
|
| 1036 |
+
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.
|
| 1037 |
+
""")
|
| 1038 |
+
|
| 1039 |
+
# Heading for cv2.waitKey() Explanation
|
| 1040 |
+
st.markdown("""
|
| 1041 |
+
<h3 style="color: #e25822;">Understanding `cv2.waitKey()`</h3>
|
| 1042 |
+
""", unsafe_allow_html=True)
|
| 1043 |
+
|
| 1044 |
+
# Short Heading
|
| 1045 |
+
st.markdown("""
|
| 1046 |
+
<h3 style="color: #e25822;">Understanding `cv2.waitKey()`</h3>
|
| 1047 |
+
""", unsafe_allow_html=True)
|
| 1048 |
+
|
| 1049 |
+
# Explanation
|
| 1050 |
+
st.write("""
|
| 1051 |
+
The line `if cv2.waitKey(1) & 255 == ord('q'):` is used in OpenCV to check if a specific key is pressed while processing video. Here’s a simple explanation of what it does:
|
| 1052 |
+
|
| 1053 |
+
- **`cv2.waitKey(1)`**:
|
| 1054 |
+
- Waits for a key press for **1 millisecond**.
|
| 1055 |
+
- If a key is pressed, it returns the key’s code. If no key is pressed, it returns `-1`.
|
| 1056 |
+
|
| 1057 |
+
- **`& 255`**:
|
| 1058 |
+
- Ensures the key code is compatible across different systems.
|
| 1059 |
+
- Keeps only the last **8 bits**, which represent the key code.
|
| 1060 |
+
|
| 1061 |
+
- **`ord('q')`**:
|
| 1062 |
+
- Finds the ASCII code for the letter `'q'`.
|
| 1063 |
+
- The ASCII code for `'q'` is **113**.
|
| 1064 |
+
- This is used to check if the user pressed the `'q'` key to stop the program.
|
| 1065 |
+
|
| 1066 |
+
### Full Condition:
|
| 1067 |
+
```python
|
| 1068 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1069 |
+
break
|
| 1070 |
+
|
| 1071 |
+
|
| 1072 |
+
|
| 1073 |
|
| 1074 |
|
| 1075 |
|