Spaces:
Sleeping
Sleeping
Update pages/hari.py
Browse files- pages/hari.py +230 -4
pages/hari.py
CHANGED
|
@@ -450,10 +450,236 @@ def image_details_page():
|
|
| 450 |
- Formats include JPEG, PNG, BMP, etc.
|
| 451 |
- Libraries like OpenCV and PIL are used for image processing.
|
| 452 |
""")
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
# Unstructured Data - Video Page
|
| 459 |
def video_details_page():
|
|
|
|
| 450 |
- Formats include JPEG, PNG, BMP, etc.
|
| 451 |
- Libraries like OpenCV and PIL are used for image processing.
|
| 452 |
""")
|
| 453 |
+
|
| 454 |
+
import numpy as np
|
| 455 |
+
|
| 456 |
+
# Helper function for subheadings
|
| 457 |
+
def subheading(text):
|
| 458 |
+
"""Displays a subheader with consistent styling."""
|
| 459 |
+
st.markdown(f"<h3 style='color:teal;'>{text}</h3>", unsafe_allow_html=True)
|
| 460 |
+
|
| 461 |
+
# Sidebar for navigation
|
| 462 |
+
st.sidebar.title("Navigation")
|
| 463 |
+
page = st.sidebar.radio("Go to", ["Introduction", "Basic Operations","Converting Image to Black&White ,BGR & Gray SCale"])
|
| 464 |
+
|
| 465 |
+
# App Title and Description
|
| 466 |
+
st.title("Image Processing Fundamentals")
|
| 467 |
+
st.write("""
|
| 468 |
+
This app introduces the basics of image processing, helping you understand how images are formed, represented, and handled programmatically.
|
| 469 |
+
It's designed for beginners exploring computer vision concepts.
|
| 470 |
+
""")
|
| 471 |
+
|
| 472 |
+
# Introduction Section
|
| 473 |
+
if page == "Introduction":
|
| 474 |
+
st.header("Introduction")
|
| 475 |
+
st.write("""
|
| 476 |
+
Images play a crucial role in various fields, including art, science, and technology.
|
| 477 |
+
In this app, you will learn:
|
| 478 |
+
- How images are captured and represented.
|
| 479 |
+
- Different color spaces and their applications.
|
| 480 |
+
- Basic operations on images using Python libraries.
|
| 481 |
+
""")
|
| 482 |
+
|
| 483 |
+
st.header("Understanding Images")
|
| 484 |
+
|
| 485 |
+
# Subsections
|
| 486 |
+
subheading("What is an Image?")
|
| 487 |
+
st.write("""
|
| 488 |
+
An image is a **2D representation of light**, created when light reflects off an object and is captured by a camera or our eyes.
|
| 489 |
+
""")
|
| 490 |
+
|
| 491 |
+
subheading("How is an Image Formed?")
|
| 492 |
+
st.write("""
|
| 493 |
+
- **Light Source**: Light from sources like the sun or a bulb hits an object.
|
| 494 |
+
- **Reflection**: Light bounces off the object's surface.
|
| 495 |
+
- **Capture**: The reflected light is recorded by a camera sensor or the human eye.
|
| 496 |
+
""")
|
| 497 |
+
|
| 498 |
+
subheading("Why is an Image Represented as a Grid?")
|
| 499 |
+
st.write("""
|
| 500 |
+
- Images are stored as grids of **pixels**, where each pixel represents a single point of color and brightness.
|
| 501 |
+
- These grids are also called **image matrices**.
|
| 502 |
+
""")
|
| 503 |
+
|
| 504 |
+
st.header("Color Spaces")
|
| 505 |
+
|
| 506 |
+
subheading("1. Black and White")
|
| 507 |
+
st.write("""
|
| 508 |
+
- Represents two colors: **Black (0)** and **White (255)**.
|
| 509 |
+
- Used for simple image processing tasks where color isn't essential.
|
| 510 |
+
""")
|
| 511 |
+
|
| 512 |
+
subheading("2. Grayscale")
|
| 513 |
+
st.write("""
|
| 514 |
+
- Extends black and white to include **256 shades of gray**.
|
| 515 |
+
- Preserves brightness details but loses color information.
|
| 516 |
+
""")
|
| 517 |
+
|
| 518 |
+
subheading("3. RGB (Red, Green, Blue)")
|
| 519 |
+
st.write("""
|
| 520 |
+
- Combines three color channels: **Red**, **Green**, and **Blue**.
|
| 521 |
+
- Most commonly used for colored images, capable of representing over **16 million colors**.
|
| 522 |
+
""")
|
| 523 |
+
|
| 524 |
+
# Basic Operations Section
|
| 525 |
+
elif page == "Basic Operations":
|
| 526 |
+
|
| 527 |
+
st.code("""
|
| 528 |
+
import cv2
|
| 529 |
+
import numpy as np
|
| 530 |
+
""")
|
| 531 |
+
|
| 532 |
+
st.title("OpenCV Basics: Image Handling")
|
| 533 |
+
|
| 534 |
+
# Theory Section
|
| 535 |
+
st.header("1. `cv2.imread` - Reading an Image")
|
| 536 |
+
st.markdown("""
|
| 537 |
+
- **Purpose**: Converts an image file into a numerical array that represents its pixel data.
|
| 538 |
+
- **Default Behavior**: Reads the image in the **BGR color space**.
|
| 539 |
+
- **Parameters**:
|
| 540 |
+
- `path`: Specifies the location of the image file.
|
| 541 |
+
- `flags`:
|
| 542 |
+
- `1`: Reads the image as a color image (default).
|
| 543 |
+
- `0`: Reads the image as a grayscale image (2D array).
|
| 544 |
+
- **Output**: A numerical array where each element corresponds to a pixel's intensity or color.
|
| 545 |
+
""")
|
| 546 |
+
st.code("""
|
| 547 |
+
# Code to read an image
|
| 548 |
+
img = cv2.imread('path/to/your/image.jpg', 1) # Read as color image
|
| 549 |
+
st.image(img, channels="BGR") # Display image in Streamlit
|
| 550 |
+
""")
|
| 551 |
+
|
| 552 |
+
# Theory Section
|
| 553 |
+
st.header("2. `cv2.imshow` - Displaying an Image")
|
| 554 |
+
st.markdown("""
|
| 555 |
+
- **Purpose**: Opens a pop-up window to show an image.
|
| 556 |
+
- **Parameters**:
|
| 557 |
+
- `Window Name`: Title of the display window.
|
| 558 |
+
- `Image`: Array data representing the image.
|
| 559 |
+
""")
|
| 560 |
+
st.code("""
|
| 561 |
+
# Code to display an image
|
| 562 |
+
cv2.imshow("Sample Image", img)
|
| 563 |
+
""")
|
| 564 |
+
|
| 565 |
+
# Theory Section
|
| 566 |
+
st.header("3. `cv2.waitKey` - Waiting for a Key Input")
|
| 567 |
+
st.markdown("""
|
| 568 |
+
- **Purpose**: Waits for the specified time (in milliseconds) for a key press to proceed or close the window.
|
| 569 |
+
- **Parameters**:
|
| 570 |
+
- `Delay`:
|
| 571 |
+
- If `0`: Waits indefinitely until a key is pressed.
|
| 572 |
+
- If a positive value: Waits for that duration in milliseconds.
|
| 573 |
+
- **Usage**: Prevents the program from closing the window immediately.
|
| 574 |
+
""")
|
| 575 |
+
st.code("""
|
| 576 |
+
# Code to wait for a key press
|
| 577 |
+
cv2.waitKey(0) # Wait indefinitely until key press
|
| 578 |
+
""")
|
| 579 |
+
|
| 580 |
+
# Theory Section
|
| 581 |
+
st.header("4. `cv2.destroyAllWindows` - Closing OpenCV Windows")
|
| 582 |
+
st.markdown("""
|
| 583 |
+
- **Purpose**: Closes all OpenCV-created windows.
|
| 584 |
+
- **Usage**: Ensures proper memory cleanup and avoids crashes by freeing up resources after the display is no longer needed.
|
| 585 |
+
- **Best Practice**: Always use this after displaying images to clean up the resources.
|
| 586 |
+
""")
|
| 587 |
+
st.code("""
|
| 588 |
+
# Code to close all windows
|
| 589 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 590 |
+
""")
|
| 591 |
+
|
| 592 |
+
st.markdown("""
|
| 593 |
+
### Additional Notes
|
| 594 |
+
- **Why Use `cv2.waitKey`?**
|
| 595 |
+
Without this, the image display window will close immediately after the program finishes execution.
|
| 596 |
+
|
| 597 |
+
- **Handling Pop-Up Windows**
|
| 598 |
+
- Use `cv2.destroyAllWindows()` to close all pop-up windows and release system resources properly.
|
| 599 |
+
""")
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
elif page =="Converting Image to Black&White ,BGR & Gray SCale":
|
| 603 |
+
|
| 604 |
+
from PIL import Image
|
| 605 |
+
|
| 606 |
+
# Title of the app
|
| 607 |
+
st.header("Black & White Images")
|
| 608 |
+
|
| 609 |
+
# Display the code
|
| 610 |
+
st.subheader("Code to Create and Display Black & White Images")
|
| 611 |
+
st.code("""
|
| 612 |
+
import numpy as np
|
| 613 |
+
import streamlit as st
|
| 614 |
+
|
| 615 |
+
# Create images
|
| 616 |
+
white_img = np.full((500, 500), 255, dtype=np.uint8) # White image
|
| 617 |
+
black_img = np.zeros((500, 500), dtype=np.uint8) # Black image
|
| 618 |
+
|
| 619 |
+
# Display in OpenCV
|
| 620 |
+
cv2.imshow("White image", white_img)
|
| 621 |
+
cv2.waitKey(0)
|
| 622 |
+
cv2.destroyAllWindows()
|
| 623 |
+
|
| 624 |
+
cv2.imshow("Black image", black_img)
|
| 625 |
+
cv2.waitKey(0)
|
| 626 |
+
cv2.destroyAllWindows()
|
| 627 |
+
""", language="python")
|
| 628 |
+
|
| 629 |
+
|
| 630 |
+
# Section 1: Grayscale Image
|
| 631 |
+
st.header("Grayscale Image Creation")
|
| 632 |
+
st.code("""
|
| 633 |
+
# Grayscale image creation
|
| 634 |
+
gray_img = np.full((500, 500), 155, dtype=np.uint8)
|
| 635 |
+
|
| 636 |
+
# Display in OpenCV
|
| 637 |
+
cv2.imshow("Gray Image", gray_img)
|
| 638 |
+
cv2.waitKey(0)
|
| 639 |
+
cv2.destroyAllWindows()
|
| 640 |
+
""", language="python")
|
| 641 |
+
|
| 642 |
+
# Section 2: BGR Channels
|
| 643 |
+
st.header("BGR Color Channels")
|
| 644 |
+
st.code("""
|
| 645 |
+
# Creating BGR channels
|
| 646 |
+
blue_channel = cv2.merge([
|
| 647 |
+
np.full((300, 300), 255, dtype=np.uint8), # Blue
|
| 648 |
+
np.zeros((300, 300), dtype=np.uint8), # Green
|
| 649 |
+
np.zeros((300, 300), dtype=np.uint8) # Red
|
| 650 |
+
])
|
| 651 |
+
green_channel = cv2.merge([
|
| 652 |
+
np.zeros((300, 300), dtype=np.uint8), # Blue
|
| 653 |
+
np.full((300, 300), 255, dtype=np.uint8), # Green
|
| 654 |
+
np.zeros((300, 300), dtype=np.uint8) # Red
|
| 655 |
+
])
|
| 656 |
+
red_channel = cv2.merge([
|
| 657 |
+
np.zeros((300, 300), dtype=np.uint8), # Blue
|
| 658 |
+
np.zeros((300, 300), dtype=np.uint8), # Green
|
| 659 |
+
np.full((300, 300), 255, dtype=np.uint8) # Red
|
| 660 |
+
])
|
| 661 |
+
""", language="python")
|
| 662 |
+
|
| 663 |
+
st.code("""
|
| 664 |
+
# Display in OpenCV
|
| 665 |
+
cv2.imshow("Blue Channel", blue_channel)
|
| 666 |
+
cv2.waitKey(0)
|
| 667 |
+
cv2.destroyAllWindows()
|
| 668 |
+
|
| 669 |
+
cv2.imshow("Green Channel", green_channel)
|
| 670 |
+
cv2.waitKey(0)
|
| 671 |
+
cv2.destroyAllWindows()
|
| 672 |
+
|
| 673 |
+
cv2.imshow("Red Channel", red_channel)
|
| 674 |
+
cv2.waitKey(0)
|
| 675 |
+
cv2.destroyAllWindows()
|
| 676 |
+
""", language="python")
|
| 677 |
+
|
| 678 |
+
if st.button("Go Back"):
|
| 679 |
+
st.experimental_set_query_params(page="Introduction")
|
| 680 |
+
|
| 681 |
+
if st.button("Back to Home"):
|
| 682 |
+
st.session_state['page'] = "home"
|
| 683 |
|
| 684 |
# Unstructured Data - Video Page
|
| 685 |
def video_details_page():
|