Spaces:
Sleeping
Sleeping
Update pages/7_Unstructured_data.py
Browse files- pages/7_Unstructured_data.py +144 -7
pages/7_Unstructured_data.py
CHANGED
|
@@ -644,16 +644,11 @@ if file_type == "VIDEO":
|
|
| 644 |
- **`release()`**: Releases the video capture object and closes the video stream.
|
| 645 |
- **`cv2.namedWindow()`**:It actually creates a pop-up window and all the features of pop-up window will be created
|
| 646 |
- **`cv2.imshow()`**: As it is not creating pop-up window ,it internally calls the namedWindow() where we can create our own pop-up window
|
| 647 |
-
- **`cv2.waitKey()`**: It internally creates a po-up window and for that window it is going to add delay
|
| 648 |
-
|
| 649 |
-
- Waits for a key event for a specified amount of time (in milliseconds). It returns the ASCII value of the key pressed.
|
| 650 |
-
|
| 651 |
-
-If 0 is passed, it waits indefinitely until a key is pressed.
|
| 652 |
- **`cv2.destroyAllWindows()`**: Closes all OpenCV windows that were opened during the program's execution.
|
| 653 |
- **`cv2.MouseCallBack()`**: It is a technique where we can make lot of things automated
|
| 654 |
- **`cv2.SetMouseCallBack()`**:It will be automatically activated when we hover the mouse inside pop-up window.
|
| 655 |
-
|
| 656 |
-
-It is going to call user-defined function which tracks the events of mouse
|
| 657 |
- It contains 5 parameters
|
| 658 |
- **5 parameters:**
|
| 659 |
- def fun(event,x,yflags,param):
|
|
@@ -665,5 +660,147 @@ if file_type == "VIDEO":
|
|
| 665 |
These methods are useful in forming the foundation of real-time video processing in OpenCV,
|
| 666 |
and also essential for handling the display and closing of images in OpenCV applications.
|
| 667 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
|
| 669 |
|
|
|
|
| 644 |
- **`release()`**: Releases the video capture object and closes the video stream.
|
| 645 |
- **`cv2.namedWindow()`**:It actually creates a pop-up window and all the features of pop-up window will be created
|
| 646 |
- **`cv2.imshow()`**: As it is not creating pop-up window ,it internally calls the namedWindow() where we can create our own pop-up window
|
| 647 |
+
- **`cv2.waitKey()`**: It internally creates a po-up window and for that window it is going to add delay.Waits for a key event for a specified amount of time (in milliseconds). It returns the ASCII value of the key pressed.If 0 is passed, it waits indefinitely until a key is pressed.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
- **`cv2.destroyAllWindows()`**: Closes all OpenCV windows that were opened during the program's execution.
|
| 649 |
- **`cv2.MouseCallBack()`**: It is a technique where we can make lot of things automated
|
| 650 |
- **`cv2.SetMouseCallBack()`**:It will be automatically activated when we hover the mouse inside pop-up window.
|
| 651 |
+
- It is going to call user-defined function which tracks the events of mouse
|
|
|
|
| 652 |
- It contains 5 parameters
|
| 653 |
- **5 parameters:**
|
| 654 |
- def fun(event,x,yflags,param):
|
|
|
|
| 660 |
These methods are useful in forming the foundation of real-time video processing in OpenCV,
|
| 661 |
and also essential for handling the display and closing of images in OpenCV applications.
|
| 662 |
""")
|
| 663 |
+
|
| 664 |
+
st.subheader("**Reading and Displaying a Video**")
|
| 665 |
+
st.code("""
|
| 666 |
+
import cv2
|
| 667 |
+
# Open the video file
|
| 668 |
+
video = cv2.VideoCapture("path_to_video.mp4")
|
| 669 |
+
# Loop to read and display frames
|
| 670 |
+
while True:
|
| 671 |
+
success, frame = video.read() # Read a frame
|
| 672 |
+
if not success:
|
| 673 |
+
print("Video Ended")
|
| 674 |
+
break
|
| 675 |
+
cv2.imshow("Video Playback", frame) # Display the frame
|
| 676 |
+
# Break loop on 'q' key press
|
| 677 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 678 |
+
break
|
| 679 |
+
video.release() # Release the video file
|
| 680 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 681 |
+
""", language="python")
|
| 682 |
+
|
| 683 |
+
st.markdown("---")
|
| 684 |
+
st.subheader("**Understanding `cv2.waitKey()` and Key Input**")
|
| 685 |
+
st.write("""
|
| 686 |
+
The line `if cv2.waitKey(1) & 255 == ord('q'):` is used in OpenCV to handle keyboard input while processing video frames. Here’s a breakdown:
|
| 687 |
+
- **`cv2.waitKey(1)`**:
|
| 688 |
+
- Waits for a key press for `1` millisecond.
|
| 689 |
+
- Returns the ASCII value of the key pressed, or `-1` if no key is pressed.
|
| 690 |
+
- **`& 255`**:
|
| 691 |
+
- Extracts only the last 8 bits (ASCII value).
|
| 692 |
+
- **`ord('q')`**:
|
| 693 |
+
- Provides the ASCII value of the character `'q'`.
|
| 694 |
+
- The condition checks if the user pressed the `'q'` key to quit the program.
|
| 695 |
+
""")
|
| 696 |
+
|
| 697 |
+
st.header("**Capturing and Saving a Specific Frame**")
|
| 698 |
+
st.markdown("""
|
| 699 |
+
- Use OpenCV to capture a specific frame from a video and save it as an image file.
|
| 700 |
+
""")
|
| 701 |
+
|
| 702 |
+
st.subheader("Example: Saving a Frame")
|
| 703 |
+
st.code("""
|
| 704 |
+
import cv2
|
| 705 |
+
video = cv2.VideoCapture("path_to_video.mp4") # Replace with 0 for webcam
|
| 706 |
+
while True:
|
| 707 |
+
success, frame = video.read()
|
| 708 |
+
if not success:
|
| 709 |
+
break
|
| 710 |
+
cv2.imshow("Video", frame)
|
| 711 |
+
# Save frame on 's' key press
|
| 712 |
+
if cv2.waitKey(1) & 255 == ord('s'):
|
| 713 |
+
cv2.imwrite("captured_frame.jpg", frame)
|
| 714 |
+
print("Frame saved as captured_frame.jpg")
|
| 715 |
+
# Break loop on 'q' key press
|
| 716 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 717 |
+
break
|
| 718 |
+
video.release()
|
| 719 |
+
cv2.destroyAllWindows()
|
| 720 |
+
""", language="python")
|
| 721 |
+
|
| 722 |
+
|
| 723 |
+
st.header("**Capturing Video from Webcam**")
|
| 724 |
+
st.markdown("""
|
| 725 |
+
- The `cv2.VideoCapture()` function can also be used to capture live video from your webcam or connected camera devices.
|
| 726 |
+
""")
|
| 727 |
+
|
| 728 |
+
st.subheader("**Example of Capturing Video from Webcam**")
|
| 729 |
+
st.code("""
|
| 730 |
+
import cv2
|
| 731 |
+
# Open video capture (0 for primary webcam)
|
| 732 |
+
video = cv2.VideoCapture(0)
|
| 733 |
+
# Loop to read frames
|
| 734 |
+
while True:
|
| 735 |
+
success, frame = video.read() # Read a frame
|
| 736 |
+
if not success:
|
| 737 |
+
break
|
| 738 |
+
cv2.imshow("Webcam", frame) # Display the frame
|
| 739 |
+
# Break loop on 'q' key press
|
| 740 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 741 |
+
break
|
| 742 |
+
video.release() # Release the video capture object
|
| 743 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 744 |
+
""", language="python")
|
| 745 |
+
|
| 746 |
+
st.header("**Processing Video: Converting to Grayscale**")
|
| 747 |
+
st.markdown("""
|
| 748 |
+
You can process each frame of the video in real-time.By following steps:
|
| 749 |
+
- Convert each frame of a video to grayscale.
|
| 750 |
+
- Display the processed video.
|
| 751 |
+
""")
|
| 752 |
+
|
| 753 |
+
st.subheader("**Example of Converting Video to Grayscale**")
|
| 754 |
+
st.code("""
|
| 755 |
+
import cv2
|
| 756 |
+
# Open the video file
|
| 757 |
+
video = cv2.VideoCapture("path_to_video.mp4")
|
| 758 |
+
# Loop to read and process frames
|
| 759 |
+
while True:
|
| 760 |
+
success, frame = video.read() # Read a frame
|
| 761 |
+
if not suc:
|
| 762 |
+
break
|
| 763 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert frame to grayscale
|
| 764 |
+
cv2.imshow("Grayscale Video", gray_frame) # Display the processed frame
|
| 765 |
+
# Break loop on 'q' key press
|
| 766 |
+
if cv2.waitKey(30) & 255 == ord('q'):
|
| 767 |
+
break
|
| 768 |
+
video.release() # Release the video file
|
| 769 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 770 |
+
""", language="python")
|
| 771 |
+
|
| 772 |
+
st.header("**Splitting Channels in a Video Frame**")
|
| 773 |
+
st.markdown("""
|
| 774 |
+
- You can split the three color channels (Blue, Green, and Red) from a video frame and process them individually.
|
| 775 |
+
""")
|
| 776 |
+
|
| 777 |
+
st.subheader("Example: Splitting Video Frame Channels")
|
| 778 |
+
st.code("""
|
| 779 |
+
import cv2
|
| 780 |
+
# Open video capture
|
| 781 |
+
video = cv2.VideoCapture("path_to_video.mp4") # Replace with 0 for webcam
|
| 782 |
+
while True:
|
| 783 |
+
success, frame = video.read()
|
| 784 |
+
if not success:
|
| 785 |
+
break
|
| 786 |
+
# Split the frame into channels
|
| 787 |
+
b, g, r = cv2.split(frame)
|
| 788 |
+
# Merge and display individual channels
|
| 789 |
+
blue_img = cv2.merge([b, g*0, r*0])
|
| 790 |
+
green_img = cv2.merge([b*0, g, r*0])
|
| 791 |
+
red_img = cv2.merge([b*0, g*0, r])
|
| 792 |
+
cv2.imshow("Original Frame", frame)
|
| 793 |
+
cv2.imshow("Blue Channel", blue_img)
|
| 794 |
+
cv2.imshow("Green Channel", green_img)
|
| 795 |
+
cv2.imshow("Red Channel", red_img)
|
| 796 |
+
# Break loop on 'q' key press
|
| 797 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 798 |
+
break
|
| 799 |
+
video.release()
|
| 800 |
+
cv2.destroyAllWindows()
|
| 801 |
+
""", language="python")
|
| 802 |
+
|
| 803 |
+
|
| 804 |
+
|
| 805 |
|
| 806 |
|