Update pages/LIFE_CYCLE_OF_MACHINE_LEARNING.py
Browse files
pages/LIFE_CYCLE_OF_MACHINE_LEARNING.py
CHANGED
@@ -477,11 +477,153 @@ def Conversion_of_Images_page():
|
|
477 |
|
478 |
# Navigation Button
|
479 |
if st.button("Video capture and explanation"):
|
480 |
-
st.session_state.page = "
|
481 |
|
482 |
# Navigation Button
|
483 |
if st.button("Back to Data Collection"):
|
484 |
st.session_state.page = "data_collection"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
485 |
# ----------------- Semi-Structured Data Page -----------------
|
486 |
def semi_structured_data_page():
|
487 |
st.title(":orange[Semi-Structured Data]")
|
@@ -570,8 +712,8 @@ def main():
|
|
570 |
operations_using_opencv_page()
|
571 |
elif page == "Conversion_of_Images":
|
572 |
Conversion_of_Images_page()
|
573 |
-
elif page == "
|
574 |
-
|
575 |
|
576 |
|
577 |
if __name__ == "__main__":
|
|
|
477 |
|
478 |
# Navigation Button
|
479 |
if st.button("Video capture and explanation"):
|
480 |
+
st.session_state.page = "Video_capture_and_explanation"
|
481 |
|
482 |
# Navigation Button
|
483 |
if st.button("Back to Data Collection"):
|
484 |
st.session_state.page = "data_collection"
|
485 |
+
|
486 |
+
|
487 |
+
#---------VIDEO CAPTURE AND EXPLANATION OF CV2.WAITKEY-----------
|
488 |
+
|
489 |
+
|
490 |
+
|
491 |
+
def Video_capture_and_explanation_page():
|
492 |
+
st.header("🎥 Video Capture with `cv2.VideoCapture()")
|
493 |
+
st.markdown("""
|
494 |
+
**Purpose**: Captures live video from a webcam or reads a video file using OpenCV.
|
495 |
+
|
496 |
+
### Syntax
|
497 |
+
```python
|
498 |
+
cap = cv2.VideoCapture(source)
|
499 |
+
source:
|
500 |
+
0: Refers to the default webcam (if you have one connected).
|
501 |
+
'video.mp4': The path to a video file (can be any supported video format like .mp4, .avi).
|
502 |
+
Key Methods
|
503 |
+
cap.read(): Captures a frame-by-frame video from the source.
|
504 |
+
|
505 |
+
Returns:
|
506 |
+
ret: A Boolean indicating whether the frame was read correctly (True if successful).
|
507 |
+
frame: The captured frame, represented as a NumPy array (this can be processed or displayed).
|
508 |
+
cap.release(): Releases the video source when you are done capturing. It frees up system resources and allows you to safely close the video capture device or file.
|
509 |
+
|
510 |
+
Example
|
511 |
+
Here’s an example that captures video from the default webcam and displays it:
|
512 |
+
|
513 |
+
python
|
514 |
+
Copy code
|
515 |
+
import cv2
|
516 |
+
|
517 |
+
# Open the default webcam (0)
|
518 |
+
cap = cv2.VideoCapture(0)
|
519 |
+
|
520 |
+
while cap.isOpened():
|
521 |
+
ret, frame = cap.read() # Capture frame-by-frame
|
522 |
+
if not ret:
|
523 |
+
break # Exit if frame not read correctly
|
524 |
+
|
525 |
+
cv2.imshow('Live Video', frame) # Display the frame
|
526 |
+
|
527 |
+
# Wait for 1 ms and exit if 'q' is pressed
|
528 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
529 |
+
break
|
530 |
+
|
531 |
+
cap.release() # Release the webcam
|
532 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
533 |
+
How it Works:
|
534 |
+
cv2.VideoCapture(0): Opens the default webcam (if available).
|
535 |
+
cap.read(): Reads each frame from the video source.
|
536 |
+
cv2.imshow('Live Video', frame): Displays each captured frame in a window.
|
537 |
+
cap.release(): Releases the video capture object when done capturing frames.
|
538 |
+
cv2.destroyAllWindows(): Closes all OpenCV windows to free up resources.
|
539 |
+
""")
|
540 |
+
##----------##
|
541 |
+
|
542 |
+
st.header("⏱️ cv2.waitKey() for Key Event Handling")
|
543 |
+
st.markdown(""" Purpose:
|
544 |
+
cv2.waitKey() is a key function used to handle keyboard events in OpenCV. It is commonly used to display images or video frames and wait for a user input.
|
545 |
+
|
546 |
+
Syntax
|
547 |
+
python
|
548 |
+
Copy code
|
549 |
+
cv2.waitKey(delay)
|
550 |
+
delay:
|
551 |
+
0: Waits indefinitely until a key is pressed. This is useful when displaying images or video and you want to hold the display open until a key is pressed.
|
552 |
+
1: Waits for 1 millisecond. This is commonly used in real-time video streaming where the program keeps checking for user input every 1 millisecond.
|
553 |
+
How it Works:
|
554 |
+
cv2.waitKey(1): This line waits for a key press for 1 millisecond before checking if the user has pressed any key. If no key is pressed within that time, it proceeds to the next frame.
|
555 |
+
Key Event: The function returns an integer value representing the ASCII code of the key pressed.
|
556 |
+
For example, pressing the 'q' key returns 113 (the ASCII value for 'q').
|
557 |
+
Example
|
558 |
+
Here’s an example using cv2.waitKey() to exit the video capture loop when the 'q' key is pressed:
|
559 |
+
|
560 |
+
python
|
561 |
+
Copy code
|
562 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
563 |
+
break
|
564 |
+
ord('q'): Converts the 'q' character to its ASCII value (113).
|
565 |
+
& 0xFF: Masks the higher bits of the returned value to only check for the lower 8 bits, ensuring correct handling of the key press.
|
566 |
+
Why is cv2.waitKey() Important?
|
567 |
+
It helps manage user input while displaying images or videos.
|
568 |
+
Without cv2.waitKey(), the OpenCV window would immediately close after displaying the image/video, and you would not be able to interact with it.
|
569 |
+
It enables frame-by-frame processing in real-time video processing (such as live video capture or webcam feeds).
|
570 |
+
Example in Context:
|
571 |
+
python
|
572 |
+
Copy code
|
573 |
+
import cv2
|
574 |
+
|
575 |
+
# Open the default webcam (0)
|
576 |
+
cap = cv2.VideoCapture(0)
|
577 |
+
|
578 |
+
while cap.isOpened():
|
579 |
+
ret, frame = cap.read() # Capture frame-by-frame
|
580 |
+
if not ret:
|
581 |
+
break # Exit if frame not read correctly
|
582 |
+
|
583 |
+
cv2.imshow('Webcam Feed', frame) # Display the frame
|
584 |
+
|
585 |
+
# Wait for 1 ms and exit if 'q' is pressed
|
586 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
587 |
+
break
|
588 |
+
|
589 |
+
cap.release() # Release the webcam
|
590 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
591 |
+
Explanation:
|
592 |
+
cv2.VideoCapture(0): Initializes the webcam.
|
593 |
+
cap.read(): Captures each frame from the webcam.
|
594 |
+
cv2.imshow('Webcam Feed', frame): Displays the captured frame.
|
595 |
+
cv2.waitKey(1): Checks for key press every 1 millisecond. If the 'q' key is pressed, the loop breaks, and the webcam feed stops.
|
596 |
+
cap.release(): Releases the webcam when done.
|
597 |
+
cv2.destroyAllWindows(): Closes the OpenCV windows and cleans up resources.
|
598 |
+
""")
|
599 |
+
|
600 |
+
###------KEY POINTS -----###
|
601 |
+
|
602 |
+
st.markdown("""
|
603 |
+
|
604 |
+
1. **Video Capture (`cv2.VideoCapture`)**: Opens and reads video either from the webcam or from a video file.
|
605 |
+
- **Method `cap.read()`**: Captures individual frames from the video source.
|
606 |
+
- **Releasing the capture (`cap.release()`)**: Ensures that the resources are freed once done.
|
607 |
+
|
608 |
+
2. **Key Handling (`cv2.waitKey`)**: Waits for user key input and processes it:
|
609 |
+
- **`cv2.waitKey(1)`**: Checks for key presses every 1 millisecond.
|
610 |
+
- **Exiting the loop**: Pressing the `'q'` key exits the video capture loop.
|
611 |
+
|
612 |
+
This explanation provides both the purpose and practical use cases of `cv2.VideoCapture()` and `cv2.waitKey()` in video capture scenarios, including how the two work together to display video and handle key events effectively.
|
613 |
+
""")
|
614 |
+
|
615 |
+
# Navigation Button
|
616 |
+
if st.button("Affine Transformation Matrix"):
|
617 |
+
st.session_state.page = "Affine_Transformation_Matrix"
|
618 |
+
|
619 |
+
# Navigation Button
|
620 |
+
if st.button("Back to Data Collection"):
|
621 |
+
st.session_state.page = "data_collection"
|
622 |
+
|
623 |
+
|
624 |
+
|
625 |
+
|
626 |
+
|
627 |
# ----------------- Semi-Structured Data Page -----------------
|
628 |
def semi_structured_data_page():
|
629 |
st.title(":orange[Semi-Structured Data]")
|
|
|
712 |
operations_using_opencv_page()
|
713 |
elif page == "Conversion_of_Images":
|
714 |
Conversion_of_Images_page()
|
715 |
+
elif page == "Video_capture_and_explanation":
|
716 |
+
Video_capture_and_explanation_page()
|
717 |
|
718 |
|
719 |
if __name__ == "__main__":
|