Update pages/LIFE_CYCLE_OF_MACHINE_LEARNING.py
Browse files
pages/LIFE_CYCLE_OF_MACHINE_LEARNING.py
CHANGED
@@ -486,116 +486,127 @@ def Conversion_of_Images_page():
|
|
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 |
-
|
503 |
-
cap.read(): Captures a frame-by-frame video from the source.
|
504 |
|
505 |
-
|
506 |
-
|
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 |
-
|
511 |
-
|
|
|
|
|
512 |
|
513 |
-
|
514 |
-
|
515 |
-
import cv2
|
516 |
|
517 |
-
|
518 |
-
|
519 |
|
520 |
-
|
521 |
-
|
522 |
-
if not ret:
|
523 |
-
break # Exit if frame not read correctly
|
524 |
|
525 |
-
|
|
|
|
|
|
|
|
|
526 |
|
527 |
-
|
528 |
-
|
529 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
cv2.waitKey(delay)
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
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 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
cv2.waitKey(
|
596 |
-
|
597 |
-
|
598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
599 |
|
600 |
###------KEY POINTS -----###
|
601 |
|
|
|
486 |
|
487 |
#---------VIDEO CAPTURE AND EXPLANATION OF CV2.WAITKEY-----------
|
488 |
|
489 |
+
def Video_capture_and_explanation_page():
|
490 |
+
st.header("🎥 Video Capture with `cv2.VideoCapture()`")
|
491 |
|
|
|
|
|
|
|
492 |
st.markdown("""
|
493 |
**Purpose**: Captures live video from a webcam or reads a video file using OpenCV.
|
494 |
|
495 |
+
### Syntax
|
496 |
+
```python
|
497 |
+
cap = cv2.VideoCapture(source)
|
498 |
+
source:
|
499 |
+
0: Refers to the default webcam (if you have one connected).
|
500 |
+
'video.mp4': The path to a video file (can be any supported video format like .mp4, .avi).
|
501 |
+
```
|
|
|
502 |
|
503 |
+
Key Methods:
|
504 |
+
- cap.read(): Captures a frame-by-frame video from the source.
|
|
|
|
|
505 |
|
506 |
+
Returns:
|
507 |
+
- ret: A Boolean indicating whether the frame was read correctly (True if successful).
|
508 |
+
- frame: The captured frame, represented as a NumPy array (this can be processed or displayed).
|
509 |
+
- 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.
|
510 |
|
511 |
+
Example:
|
512 |
+
Here’s an example that captures video from the default webcam and displays it:
|
|
|
513 |
|
514 |
+
```python
|
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 |
+
cv2.imshow('Live Video', frame) # Display the frame
|
525 |
|
526 |
+
# Wait for 1 ms and exit if 'q' is pressed
|
527 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
528 |
+
break
|
529 |
+
|
530 |
+
cap.release() # Release the webcam
|
531 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
532 |
+
```
|
533 |
+
|
534 |
+
How it Works:
|
535 |
+
- cv2.VideoCapture(0): Opens the default webcam (if available).
|
536 |
+
- cap.read(): Reads each frame from the video source.
|
537 |
+
- cv2.imshow('Live Video', frame): Displays each captured frame in a window.
|
538 |
+
- cap.release(): Releases the video capture object when done capturing frames.
|
539 |
+
- cv2.destroyAllWindows(): Closes all OpenCV windows to free up resources.
|
540 |
+
""")
|
541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
542 |
##----------##
|
543 |
|
544 |
+
st.header("⏱️ cv2.waitKey() for Key Event Handling")
|
545 |
+
st.markdown("""
|
546 |
+
Purpose:
|
547 |
+
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.
|
548 |
+
|
549 |
+
Syntax:
|
550 |
+
```python
|
551 |
+
cv2.waitKey(delay)
|
552 |
+
```
|
553 |
+
|
554 |
+
delay:
|
555 |
+
- 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.
|
556 |
+
- 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.
|
557 |
+
|
558 |
+
How it Works:
|
559 |
+
- 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.
|
560 |
+
- Key Event: The function returns an integer value representing the ASCII code of the key pressed. For example, pressing the 'q' key returns 113 (the ASCII value for 'q').
|
561 |
+
|
562 |
+
Example:
|
563 |
+
Here’s an example using cv2.waitKey() to exit the video capture loop when the 'q' key is pressed:
|
564 |
+
|
565 |
+
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
566 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
567 |
break
|
568 |
+
```
|
569 |
|
570 |
+
Explanation:
|
571 |
+
- ord('q'): Converts the 'q' character to its ASCII value (113).
|
572 |
+
- & 0xFF: Masks the higher bits of the returned value to only check for the lower 8 bits, ensuring correct handling of the key press.
|
573 |
+
|
574 |
+
Why is cv2.waitKey() Important?
|
575 |
+
- It helps manage user input while displaying images or videos.
|
576 |
+
- Without cv2.waitKey(), the OpenCV window would immediately close after displaying the image/video, and you would not be able to interact with it.
|
577 |
+
- It enables frame-by-frame processing in real-time video processing (such as live video capture or webcam feeds).
|
578 |
+
|
579 |
+
Example in Context:
|
580 |
+
|
581 |
+
```python
|
582 |
+
import cv2
|
583 |
+
|
584 |
+
# Open the default webcam (0)
|
585 |
+
cap = cv2.VideoCapture(0)
|
586 |
+
|
587 |
+
while cap.isOpened():
|
588 |
+
ret, frame = cap.read() # Capture frame-by-frame
|
589 |
+
if not ret:
|
590 |
+
break # Exit if frame not read correctly
|
591 |
+
|
592 |
+
cv2.imshow('Webcam Feed', frame) # Display the frame
|
593 |
+
|
594 |
+
# Wait for 1 ms and exit if 'q' is pressed
|
595 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
596 |
+
break
|
597 |
+
|
598 |
+
cap.release() # Release the webcam
|
599 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
600 |
+
```
|
601 |
+
|
602 |
+
Explanation:
|
603 |
+
- cv2.VideoCapture(0): Initializes the webcam.
|
604 |
+
- cap.read(): Captures each frame from the webcam.
|
605 |
+
- cv2.imshow('Webcam Feed', frame): Displays the captured frame.
|
606 |
+
- cv2.waitKey(1): Checks for key press every 1 millisecond. If the 'q' key is pressed, the loop breaks, and the webcam feed stops.
|
607 |
+
- cap.release(): Releases the webcam when done.
|
608 |
+
- cv2.destroyAllWindows(): Closes the OpenCV windows and cleans up resources.
|
609 |
+
""")
|
610 |
|
611 |
###------KEY POINTS -----###
|
612 |
|