Spaces:
Sleeping
Sleeping
Delete pages/Data Collection.py
Browse files- pages/Data Collection.py +0 -227
pages/Data Collection.py
DELETED
|
@@ -1,227 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import numpy as np
|
| 3 |
-
|
| 4 |
-
# Helper function for subheadings
|
| 5 |
-
def subheading(text):
|
| 6 |
-
"""Displays a subheader with consistent styling."""
|
| 7 |
-
st.markdown(f"<h3 style='color:teal;'>{text}</h3>", unsafe_allow_html=True)
|
| 8 |
-
|
| 9 |
-
# Sidebar for navigation
|
| 10 |
-
st.sidebar.title("Navigation")
|
| 11 |
-
page = st.sidebar.radio("Go to", ["Introduction", "Basic Operations","Converting Image to Black&White ,BGR & Gray SCale"])
|
| 12 |
-
|
| 13 |
-
# App Title and Description
|
| 14 |
-
st.title("Image Processing Fundamentals")
|
| 15 |
-
st.write("""
|
| 16 |
-
This app introduces the basics of image processing, helping you understand how images are formed, represented, and handled programmatically.
|
| 17 |
-
It's designed for beginners exploring computer vision concepts.
|
| 18 |
-
""")
|
| 19 |
-
|
| 20 |
-
# Introduction Section
|
| 21 |
-
if page == "Introduction":
|
| 22 |
-
st.header("Introduction")
|
| 23 |
-
st.write("""
|
| 24 |
-
Images play a crucial role in various fields, including art, science, and technology.
|
| 25 |
-
In this app, you will learn:
|
| 26 |
-
- How images are captured and represented.
|
| 27 |
-
- Different color spaces and their applications.
|
| 28 |
-
- Basic operations on images using Python libraries.
|
| 29 |
-
""")
|
| 30 |
-
|
| 31 |
-
st.header("Understanding Images")
|
| 32 |
-
|
| 33 |
-
# Subsections
|
| 34 |
-
subheading("What is an Image?")
|
| 35 |
-
st.write("""
|
| 36 |
-
An image is a **2D representation of light**, created when light reflects off an object and is captured by a camera or our eyes.
|
| 37 |
-
""")
|
| 38 |
-
|
| 39 |
-
subheading("How is an Image Formed?")
|
| 40 |
-
st.write("""
|
| 41 |
-
- **Light Source**: Light from sources like the sun or a bulb hits an object.
|
| 42 |
-
- **Reflection**: Light bounces off the object's surface.
|
| 43 |
-
- **Capture**: The reflected light is recorded by a camera sensor or the human eye.
|
| 44 |
-
""")
|
| 45 |
-
|
| 46 |
-
subheading("Why is an Image Represented as a Grid?")
|
| 47 |
-
st.write("""
|
| 48 |
-
- Images are stored as grids of **pixels**, where each pixel represents a single point of color and brightness.
|
| 49 |
-
- These grids are also called **image matrices**.
|
| 50 |
-
""")
|
| 51 |
-
|
| 52 |
-
st.header("Color Spaces")
|
| 53 |
-
|
| 54 |
-
subheading("1. Black and White")
|
| 55 |
-
st.write("""
|
| 56 |
-
- Represents two colors: **Black (0)** and **White (255)**.
|
| 57 |
-
- Used for simple image processing tasks where color isn't essential.
|
| 58 |
-
""")
|
| 59 |
-
|
| 60 |
-
subheading("2. Grayscale")
|
| 61 |
-
st.write("""
|
| 62 |
-
- Extends black and white to include **256 shades of gray**.
|
| 63 |
-
- Preserves brightness details but loses color information.
|
| 64 |
-
""")
|
| 65 |
-
|
| 66 |
-
subheading("3. RGB (Red, Green, Blue)")
|
| 67 |
-
st.write("""
|
| 68 |
-
- Combines three color channels: **Red**, **Green**, and **Blue**.
|
| 69 |
-
- Most commonly used for colored images, capable of representing over **16 million colors**.
|
| 70 |
-
""")
|
| 71 |
-
|
| 72 |
-
# Basic Operations Section
|
| 73 |
-
elif page == "Basic Operations":
|
| 74 |
-
|
| 75 |
-
st.code("""
|
| 76 |
-
import cv2
|
| 77 |
-
import numpy as np
|
| 78 |
-
""")
|
| 79 |
-
|
| 80 |
-
st.title("OpenCV Basics: Image Handling")
|
| 81 |
-
|
| 82 |
-
# Theory Section
|
| 83 |
-
st.header("1. `cv2.imread` - Reading an Image")
|
| 84 |
-
st.markdown("""
|
| 85 |
-
- **Purpose**: Converts an image file into a numerical array that represents its pixel data.
|
| 86 |
-
- **Default Behavior**: Reads the image in the **BGR color space**.
|
| 87 |
-
- **Parameters**:
|
| 88 |
-
- `path`: Specifies the location of the image file.
|
| 89 |
-
- `flags`:
|
| 90 |
-
- `1`: Reads the image as a color image (default).
|
| 91 |
-
- `0`: Reads the image as a grayscale image (2D array).
|
| 92 |
-
- **Output**: A numerical array where each element corresponds to a pixel's intensity or color.
|
| 93 |
-
""")
|
| 94 |
-
st.code("""
|
| 95 |
-
# Code to read an image
|
| 96 |
-
img = cv2.imread('path/to/your/image.jpg', 1) # Read as color image
|
| 97 |
-
st.image(img, channels="BGR") # Display image in Streamlit
|
| 98 |
-
""")
|
| 99 |
-
|
| 100 |
-
# Theory Section
|
| 101 |
-
st.header("2. `cv2.imshow` - Displaying an Image")
|
| 102 |
-
st.markdown("""
|
| 103 |
-
- **Purpose**: Opens a pop-up window to show an image.
|
| 104 |
-
- **Parameters**:
|
| 105 |
-
- `Window Name`: Title of the display window.
|
| 106 |
-
- `Image`: Array data representing the image.
|
| 107 |
-
""")
|
| 108 |
-
st.code("""
|
| 109 |
-
# Code to display an image
|
| 110 |
-
cv2.imshow("Sample Image", img)
|
| 111 |
-
""")
|
| 112 |
-
|
| 113 |
-
# Theory Section
|
| 114 |
-
st.header("3. `cv2.waitKey` - Waiting for a Key Input")
|
| 115 |
-
st.markdown("""
|
| 116 |
-
- **Purpose**: Waits for the specified time (in milliseconds) for a key press to proceed or close the window.
|
| 117 |
-
- **Parameters**:
|
| 118 |
-
- `Delay`:
|
| 119 |
-
- If `0`: Waits indefinitely until a key is pressed.
|
| 120 |
-
- If a positive value: Waits for that duration in milliseconds.
|
| 121 |
-
- **Usage**: Prevents the program from closing the window immediately.
|
| 122 |
-
""")
|
| 123 |
-
st.code("""
|
| 124 |
-
# Code to wait for a key press
|
| 125 |
-
cv2.waitKey(0) # Wait indefinitely until key press
|
| 126 |
-
""")
|
| 127 |
-
|
| 128 |
-
# Theory Section
|
| 129 |
-
st.header("4. `cv2.destroyAllWindows` - Closing OpenCV Windows")
|
| 130 |
-
st.markdown("""
|
| 131 |
-
- **Purpose**: Closes all OpenCV-created windows.
|
| 132 |
-
- **Usage**: Ensures proper memory cleanup and avoids crashes by freeing up resources after the display is no longer needed.
|
| 133 |
-
- **Best Practice**: Always use this after displaying images to clean up the resources.
|
| 134 |
-
""")
|
| 135 |
-
st.code("""
|
| 136 |
-
# Code to close all windows
|
| 137 |
-
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 138 |
-
""")
|
| 139 |
-
|
| 140 |
-
st.markdown("""
|
| 141 |
-
### Additional Notes
|
| 142 |
-
- **Why Use `cv2.waitKey`?**
|
| 143 |
-
Without this, the image display window will close immediately after the program finishes execution.
|
| 144 |
-
|
| 145 |
-
- **Handling Pop-Up Windows**
|
| 146 |
-
- Use `cv2.destroyAllWindows()` to close all pop-up windows and release system resources properly.
|
| 147 |
-
""")
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
elif page =="Converting Image to Black&White ,BGR & Gray SCale":
|
| 151 |
-
|
| 152 |
-
from PIL import Image
|
| 153 |
-
|
| 154 |
-
# Title of the app
|
| 155 |
-
st.header("Black & White Images")
|
| 156 |
-
|
| 157 |
-
# Display the code
|
| 158 |
-
st.subheader("Code to Create and Display Black & White Images")
|
| 159 |
-
st.code("""
|
| 160 |
-
import numpy as np
|
| 161 |
-
import streamlit as st
|
| 162 |
-
|
| 163 |
-
# Create images
|
| 164 |
-
white_img = np.full((500, 500), 255, dtype=np.uint8) # White image
|
| 165 |
-
black_img = np.zeros((500, 500), dtype=np.uint8) # Black image
|
| 166 |
-
|
| 167 |
-
# Display in OpenCV
|
| 168 |
-
cv2.imshow("White image", white_img)
|
| 169 |
-
cv2.waitKey(0)
|
| 170 |
-
cv2.destroyAllWindows()
|
| 171 |
-
|
| 172 |
-
cv2.imshow("Black image", black_img)
|
| 173 |
-
cv2.waitKey(0)
|
| 174 |
-
cv2.destroyAllWindows()
|
| 175 |
-
""", language="python")
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
# Section 1: Grayscale Image
|
| 179 |
-
st.header("Grayscale Image Creation")
|
| 180 |
-
st.code("""
|
| 181 |
-
# Grayscale image creation
|
| 182 |
-
gray_img = np.full((500, 500), 155, dtype=np.uint8)
|
| 183 |
-
|
| 184 |
-
# Display in OpenCV
|
| 185 |
-
cv2.imshow("Gray Image", gray_img)
|
| 186 |
-
cv2.waitKey(0)
|
| 187 |
-
cv2.destroyAllWindows()
|
| 188 |
-
""", language="python")
|
| 189 |
-
|
| 190 |
-
# Section 2: BGR Channels
|
| 191 |
-
st.header("BGR Color Channels")
|
| 192 |
-
st.code("""
|
| 193 |
-
# Creating BGR channels
|
| 194 |
-
blue_channel = cv2.merge([
|
| 195 |
-
np.full((300, 300), 255, dtype=np.uint8), # Blue
|
| 196 |
-
np.zeros((300, 300), dtype=np.uint8), # Green
|
| 197 |
-
np.zeros((300, 300), dtype=np.uint8) # Red
|
| 198 |
-
])
|
| 199 |
-
green_channel = cv2.merge([
|
| 200 |
-
np.zeros((300, 300), dtype=np.uint8), # Blue
|
| 201 |
-
np.full((300, 300), 255, dtype=np.uint8), # Green
|
| 202 |
-
np.zeros((300, 300), dtype=np.uint8) # Red
|
| 203 |
-
])
|
| 204 |
-
red_channel = cv2.merge([
|
| 205 |
-
np.zeros((300, 300), dtype=np.uint8), # Blue
|
| 206 |
-
np.zeros((300, 300), dtype=np.uint8), # Green
|
| 207 |
-
np.full((300, 300), 255, dtype=np.uint8) # Red
|
| 208 |
-
])
|
| 209 |
-
""", language="python")
|
| 210 |
-
|
| 211 |
-
st.code("""
|
| 212 |
-
# Display in OpenCV
|
| 213 |
-
cv2.imshow("Blue Channel", blue_channel)
|
| 214 |
-
cv2.waitKey(0)
|
| 215 |
-
cv2.destroyAllWindows()
|
| 216 |
-
|
| 217 |
-
cv2.imshow("Green Channel", green_channel)
|
| 218 |
-
cv2.waitKey(0)
|
| 219 |
-
cv2.destroyAllWindows()
|
| 220 |
-
|
| 221 |
-
cv2.imshow("Red Channel", red_channel)
|
| 222 |
-
cv2.waitKey(0)
|
| 223 |
-
cv2.destroyAllWindows()
|
| 224 |
-
""", language="python")
|
| 225 |
-
|
| 226 |
-
if st.button("Go Back"):
|
| 227 |
-
st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