Spaces:
Build error
Build error
Update pages/Machine_Learning_LifeCycle.py
Browse files- pages/Machine_Learning_LifeCycle.py +395 -197
pages/Machine_Learning_LifeCycle.py
CHANGED
|
@@ -1,90 +1,79 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import cv2
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
ml_lifecycle_html = """
|
| 10 |
-
<div class="circle-container">
|
| 11 |
-
<a href="#" class="button" id="problem-statement">Problem Statement</a>
|
| 12 |
-
<a href="?page=data_collection" class="button" id="data-collection">Data Collection</a>
|
| 13 |
-
<a href="#" class="button">Simple EDA</a>
|
| 14 |
-
<a href="#" class="button">Pre Processing</a>
|
| 15 |
-
<a href="#" class="button">EDA</a>
|
| 16 |
-
<a href="#" class="button">Feature Engineering</a>
|
| 17 |
-
<a href="#" class="button">Model Training</a>
|
| 18 |
-
<a href="#" class="button">Testing</a>
|
| 19 |
-
<a href="#" class="button">Deployment</a>
|
| 20 |
-
<a href="#" class="button">Monitoring</a>
|
| 21 |
-
<div class="center"><strong>ML Lifecycle</strong></div>
|
| 22 |
-
</div>
|
| 23 |
-
"""
|
| 24 |
-
|
| 25 |
-
# Ensure the custom CSS is loaded
|
| 26 |
custom_css = """
|
| 27 |
<style>
|
| 28 |
-
html, body, [data-testid="stAppViewContainer"] {
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
.button:nth-child(
|
| 77 |
-
.button:nth-child(
|
| 78 |
-
.button:nth-child(
|
| 79 |
-
.
|
| 80 |
-
|
| 81 |
-
top: 50%;
|
| 82 |
-
left:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
</style>
|
| 89 |
"""
|
| 90 |
|
|
@@ -117,7 +106,7 @@ def data_collection_page():
|
|
| 117 |
st.write("""
|
| 118 |
### What is data?
|
| 119 |
Data refers to raw facts, figures, or information that can be processed, analyzed, and used to generate knowledge or insights. Data can come in various forms and can be categorized based on its structure, meaning, and usage.
|
| 120 |
-
*Types of Data:* """)
|
| 121 |
|
| 122 |
# Show buttons for different types of data
|
| 123 |
structured_data = st.button('Structured Data')
|
|
@@ -145,16 +134,16 @@ def data_collection_page():
|
|
| 145 |
### What is Excel?
|
| 146 |
Excel is a spreadsheet program from Microsoft that is used for organizing, analyzing, and storing data in tabular form.
|
| 147 |
#### How to Read Excel Files?
|
| 148 |
-
You can read Excel files using libraries like pandas:
|
| 149 |
-
python
|
| 150 |
import pandas as pd
|
| 151 |
df = pd.read_excel('file.xlsx')
|
| 152 |
-
|
| 153 |
#### Issues with Excel:
|
| 154 |
- Large file sizes leading to memory errors.
|
| 155 |
- Formatting issues that can interfere with data analysis.
|
| 156 |
#### Overcoming Issues:
|
| 157 |
-
- Use openpyxl for large files.
|
| 158 |
- Ensure proper formatting before importing into tools.
|
| 159 |
""")
|
| 160 |
|
|
@@ -199,19 +188,19 @@ def data_collection_page():
|
|
| 199 |
CSV stands for Comma-Separated Values. It is a simple file format used to store tabular data such as a spreadsheet or database.
|
| 200 |
|
| 201 |
#### How to Read CSV Files?
|
| 202 |
-
You can read CSV files using pandas:
|
| 203 |
-
python
|
| 204 |
import pandas as pd
|
| 205 |
df = pd.read_csv('file.csv')
|
| 206 |
-
|
| 207 |
|
| 208 |
#### Issues with CSV:
|
| 209 |
- Inconsistent delimiters.
|
| 210 |
- Missing values.
|
| 211 |
|
| 212 |
#### Overcoming Issues:
|
| 213 |
-
- Use the delimiter parameter in pandas.read_csv().
|
| 214 |
-
- Handle missing values using dropna() or fillna().
|
| 215 |
""")
|
| 216 |
|
| 217 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
@@ -233,26 +222,26 @@ def data_collection_page():
|
|
| 233 |
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
|
| 234 |
|
| 235 |
#### How to Read JSON Files?
|
| 236 |
-
You can read JSON files using pandas or the json library:
|
| 237 |
-
python
|
| 238 |
import pandas as pd
|
| 239 |
df = pd.read_json('file.json')
|
| 240 |
-
|
| 241 |
|
| 242 |
-
Or using the json library:
|
| 243 |
-
python
|
| 244 |
import json
|
| 245 |
with open('file.json', 'r') as file:
|
| 246 |
data = json.load(file)
|
| 247 |
-
|
| 248 |
|
| 249 |
#### Issues with JSON:
|
| 250 |
- Nested structures can be difficult to parse.
|
| 251 |
- Missing keys in objects.
|
| 252 |
|
| 253 |
#### Overcoming Issues:
|
| 254 |
-
- Use normalize parameter in pandas.json_normalize() to flatten nested data.
|
| 255 |
-
- Handle missing keys by providing default values using .get().
|
| 256 |
""")
|
| 257 |
|
| 258 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
@@ -276,26 +265,26 @@ def data_collection_page():
|
|
| 276 |
XML (eXtensible Markup Language) is a markup language that defines rules for encoding documents in a format that is both human-readable and machine-readable.
|
| 277 |
|
| 278 |
#### How to Read XML Files?
|
| 279 |
-
You can read XML files using libraries like xml.etree.ElementTree or pandas:
|
| 280 |
-
python
|
| 281 |
import pandas as pd
|
| 282 |
df = pd.read_xml('file.xml')
|
| 283 |
-
|
| 284 |
|
| 285 |
-
Or using xml.etree.ElementTree:
|
| 286 |
-
python
|
| 287 |
import xml.etree.ElementTree as ET
|
| 288 |
tree = ET.parse('file.xml')
|
| 289 |
root = tree.getroot()
|
| 290 |
-
|
| 291 |
|
| 292 |
#### Issues with XML:
|
| 293 |
- Handling namespaces can be tricky.
|
| 294 |
- Large files can lead to memory issues.
|
| 295 |
|
| 296 |
#### Overcoming Issues:
|
| 297 |
-
- Use libraries like lxml for better namespace support.
|
| 298 |
-
- Parse large files incrementally using iterparse.
|
| 299 |
""")
|
| 300 |
|
| 301 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
@@ -318,27 +307,27 @@ def data_collection_page():
|
|
| 318 |
HTML (HyperText Markup Language) is the standard markup language for documents designed to be displayed in a web browser.
|
| 319 |
|
| 320 |
#### How to Read HTML Files?
|
| 321 |
-
You can read HTML files using pandas or BeautifulSoup:
|
| 322 |
-
python
|
| 323 |
import pandas as pd
|
| 324 |
df = pd.read_html('file.html')[0] # Read the first table
|
| 325 |
-
|
| 326 |
|
| 327 |
-
Or using BeautifulSoup:
|
| 328 |
-
python
|
| 329 |
from bs4 import BeautifulSoup
|
| 330 |
with open('file.html', 'r') as file:
|
| 331 |
soup = BeautifulSoup(file, 'html.parser')
|
| 332 |
print(soup.prettify())
|
| 333 |
-
|
| 334 |
|
| 335 |
#### Issues with HTML:
|
| 336 |
- Parsing tables can be complex when the structure is inconsistent.
|
| 337 |
- Missing or malformed tags.
|
| 338 |
|
| 339 |
#### Overcoming Issues:
|
| 340 |
-
- Use pandas.read_html() for well-structured tables.
|
| 341 |
-
- Handle malformed tags using BeautifulSoup with lxml parser.
|
| 342 |
""")
|
| 343 |
|
| 344 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
@@ -368,34 +357,34 @@ def data_collection_page():
|
|
| 368 |
The features in a pixel include information like shape, color, and pattern.
|
| 369 |
The clarity of an image directly depends on the number of pixels it has.
|
| 370 |
|
| 371 |
-
*Every array cannot be an image. An array can be an image only when:*
|
| 372 |
1. It should be in 2D or 3D representation.
|
| 373 |
2. The datatype should only be an integer.
|
| 374 |
|
| 375 |
### What are Color Spaces?
|
| 376 |
Color Space is a technique by which we can represent the colors of an image.
|
| 377 |
There are 3 types of color spaces namely:
|
| 378 |
-
1. *Black & White Color Space*
|
| 379 |
-
2. *Grayscale Color Space*
|
| 380 |
-
3. *RGB Color Space*
|
| 381 |
|
| 382 |
#### Black & White Color Space
|
| 383 |
In this Color Space, there are only 2 colors to represent the image, which are black & white.
|
| 384 |
-
Here, 0 represents black and 1 represents white.
|
| 385 |
|
| 386 |
#### Grayscale Color Space
|
| 387 |
In this Color Space, we have black, white, and multiple shades of gray to represent the image.
|
| 388 |
-
Here, 0 represents black, 255 represents white, and 1 to 254 represent various shades of gray.
|
| 389 |
|
| 390 |
#### RGB Color Space
|
| 391 |
In this Color Space, we create a 3D structure with three 2D channels, namely blue, green, and red channels, where:
|
| 392 |
-
- 0 represents the absence of color.
|
| 393 |
-
- 255 represents the presence of color.
|
| 394 |
|
| 395 |
These 3 channels are stacked one after another like a layered structure:
|
| 396 |
-
- *Blue channel*: 0 represents black, 255 represents blue, and 1 to 254 represent multiple shades of blue.
|
| 397 |
-
- *Green channel*: 0 represents black, 255 represents green, and 1 to 254 represent multiple shades of green.
|
| 398 |
-
- *Red channel*: 0 represents black, 255 represents red, and 1 to 254 represent multiple shades of red.
|
| 399 |
""")
|
| 400 |
|
| 401 |
basic_operations_button = st.markdown("""
|
|
@@ -415,12 +404,118 @@ def data_collection_page():
|
|
| 415 |
|
| 416 |
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
-
|
| 423 |
-
image_video_projects_page()
|
| 424 |
st.button("Text Files")
|
| 425 |
st.button("Audio")
|
| 426 |
|
|
@@ -435,15 +530,15 @@ def basic_operations_page():
|
|
| 435 |
st.write("""
|
| 436 |
### Operations on image data
|
| 437 |
There are 3 major operations which can be performed on an image, namely:
|
| 438 |
-
1. *Reading an Image*
|
| 439 |
-
2. *Writing an Image*
|
| 440 |
-
3. *Showing an Image*
|
| 441 |
#### Reading an Image
|
| 442 |
-
Import the cv2 module and use the method imread() to convert an image file into a NumPy array.
|
| 443 |
#### Writing an Image
|
| 444 |
-
Import the cv2 module and use the method imwrite() to convert a NumPy array back into an image file.
|
| 445 |
#### Showing an Image
|
| 446 |
-
Import the cv2 module and use the method imshow() to display an array in the form of an image via a popup window.
|
| 447 |
""")
|
| 448 |
|
| 449 |
if st.button("Open Jupyter Notebook"):
|
|
@@ -477,27 +572,27 @@ def working_on_image_page():
|
|
| 477 |
st.markdown(custom_css, unsafe_allow_html=True)
|
| 478 |
st.markdown("<h1>Working on the Image</h1>", unsafe_allow_html=True)
|
| 479 |
st.write("""
|
| 480 |
-
### Understanding split() Method
|
| 481 |
-
For this operation, we have to import the cv2 module and use the split() method.
|
| 482 |
|
| 483 |
-
The split() method is used to separate a multi-channel image into its individual single-channel components.
|
| 484 |
|
| 485 |
#### Example Code:
|
| 486 |
-
python
|
| 487 |
import cv2
|
| 488 |
img = cv2.imread('image.jpg') # Read an image
|
| 489 |
b, g, r = cv2.split(img) # Splits into Blue, Green, and Red channels
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
-
|
| 492 |
-
For this operation, we have to import the cv2 module and use the merge() method.
|
| 493 |
-
|
| 494 |
-
The merge() method is used to combine multiple single-channel images into a single multi-channel image.
|
| 495 |
|
| 496 |
#### Example Code:
|
| 497 |
-
python
|
| 498 |
import cv2
|
| 499 |
merged_img = cv2.merge([b, g, r]) # Combines Blue, Green, and Red channels into one image
|
| 500 |
-
|
| 501 |
""")
|
| 502 |
|
| 503 |
if st.button("Open Jupyter Notebook"):
|
|
@@ -532,29 +627,29 @@ def conversion_between_color_spaces_page():
|
|
| 532 |
st.markdown("<h1>Conversion between Color Spaces</h1>", unsafe_allow_html=True)
|
| 533 |
st.write("""
|
| 534 |
### How to Convert One Color Space to Another?
|
| 535 |
-
In the cv2 module, we have an in-built method called cvtColor() along with specific parameters for each conversion.
|
| 536 |
#### Common Conversions:
|
| 537 |
-
1. *BGR to Grayscale*:
|
| 538 |
-
- Use the parameter COLOR_BGR2GRAY inside the cvtColor() method.
|
| 539 |
-
python
|
| 540 |
import cv2
|
| 541 |
img = cv2.imread('image.jpg')
|
| 542 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 543 |
-
|
| 544 |
-
2. *Grayscale to BGR*:
|
| 545 |
-
- Use the parameter COLOR_GRAY2BGR inside the cvtColor() method.
|
| 546 |
-
python
|
| 547 |
import cv2
|
| 548 |
img = cv2.imread('gray_image.jpg', 0) # Load a grayscale image
|
| 549 |
bgr_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 550 |
-
|
| 551 |
-
3. *BGR to RGB*:
|
| 552 |
-
- Use the parameter COLOR_BGR2RGB inside the cvtColor() method.
|
| 553 |
-
python
|
| 554 |
import cv2
|
| 555 |
img = cv2.imread('image.jpg')
|
| 556 |
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 557 |
-
|
| 558 |
""")
|
| 559 |
|
| 560 |
if st.button("Open Jupyter Notebook"):
|
|
@@ -592,60 +687,60 @@ def affine_transformations_page():
|
|
| 592 |
### How to Apply Affine Transformations on Images?
|
| 593 |
Affine transformations are used in image augmentation to manipulate and alter images by applying operations such as translation, rotation, scaling, and shearing. These transformations help to generate new images from the existing ones, thereby increasing the dataset diversity.
|
| 594 |
#### Common Affine Transformations:
|
| 595 |
-
1. *Translation*:
|
| 596 |
- Shift the image by a specified number of pixels on the X and Y axes.
|
| 597 |
-
python
|
| 598 |
import cv2
|
| 599 |
import numpy as np
|
| 600 |
img = cv2.imread('image.jpg')
|
| 601 |
rows, cols, _ = img.shape
|
| 602 |
M = np.float32([[1, 0, 50], [0, 1, 100]]) # Translation matrix
|
| 603 |
translated_img = cv2.warpAffine(img, M, (cols, rows))
|
| 604 |
-
|
| 605 |
-
2. *Rotation*:
|
| 606 |
- Rotate the image by an angle at a specified center.
|
| 607 |
-
python
|
| 608 |
import cv2
|
| 609 |
import numpy as np
|
| 610 |
img = cv2.imread('image.jpg')
|
| 611 |
rows, cols, _ = img.shape
|
| 612 |
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1) # Rotation matrix
|
| 613 |
rotated_img = cv2.warpAffine(img, M, (cols, rows))
|
| 614 |
-
|
| 615 |
-
3. *Scaling*:
|
| 616 |
- Increase or decrease the image size by a particular factor.
|
| 617 |
-
python
|
| 618 |
import cv2
|
| 619 |
img = cv2.imread('image.jpg')
|
| 620 |
scaled_img = cv2.resize(img, None, fx=1.5, fy=1.5) # Scaling by factor of 1.5
|
| 621 |
-
|
| 622 |
-
4. *Shearing*:
|
| 623 |
- Apply shearing transformation to stretch the image from its edges.
|
| 624 |
-
python
|
| 625 |
import cv2
|
| 626 |
import numpy as np
|
| 627 |
img = cv2.imread('image.jpg')
|
| 628 |
rows, cols, _ = img.shape
|
| 629 |
M = np.float32([[1, 0.5, 0], [0.5, 1, 0]]) # Shearing matrix
|
| 630 |
sheared_img = cv2.warpAffine(img, M, (cols, rows))
|
| 631 |
-
|
| 632 |
-
5. *Cropping*:
|
| 633 |
- Extract or cut a specific part of the image.
|
| 634 |
-
python
|
| 635 |
import cv2
|
| 636 |
img = cv2.imread('image.jpg')
|
| 637 |
cropped_img = img[50:200, 100:300] # Manually cropping using slicing
|
| 638 |
-
|
| 639 |
#### How to Combine Transformations?
|
| 640 |
You can combine translation, rotation, scaling, and shearing into a single transformation matrix:
|
| 641 |
-
python
|
| 642 |
import cv2
|
| 643 |
import numpy as np
|
| 644 |
img = cv2.imread('image.jpg')
|
| 645 |
rows, cols, _ = img.shape
|
| 646 |
M = np.float32([[1.2, 0.5, 100], [0.5, 1.2, 50]]) # Combined scaling, shearing, and translation
|
| 647 |
transformed_img = cv2.warpAffine(img, M, (cols, rows))
|
| 648 |
-
|
| 649 |
""")
|
| 650 |
|
| 651 |
if st.button("Open Jupyter Notebook"):
|
|
@@ -667,9 +762,10 @@ def affine_transformations_page():
|
|
| 667 |
notebook_html = f.read()
|
| 668 |
st.components.v1.html(notebook_html, height=500, scrolling=True)
|
| 669 |
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
| 673 |
</a>
|
| 674 |
|
| 675 |
""", unsafe_allow_html=True)
|
|
@@ -683,10 +779,10 @@ def handling_video_data_page():
|
|
| 683 |
### What is a Video?
|
| 684 |
A video is a sequence of images, called frames, displayed rapidly one after another to create the illusion of motion.
|
| 685 |
### Working with Video Data in OpenCV
|
| 686 |
-
In OpenCV, we can handle video data using the cv2.VideoCapture() method, which allows us to capture video frames from a video file or a live camera feed.
|
| 687 |
#### Converting a Video into Frames
|
| 688 |
-
The VideoCapture() method is used to load a video, and you can read each frame individually using the read() method. Below is an example of how to extract frames from a video:
|
| 689 |
-
python
|
| 690 |
import cv2
|
| 691 |
# Open the video file
|
| 692 |
video_path = 'your_video.mp4'
|
|
@@ -702,11 +798,11 @@ def handling_video_data_page():
|
|
| 702 |
break
|
| 703 |
cap.release()
|
| 704 |
cv2.destroyAllWindows()
|
| 705 |
-
|
| 706 |
#### Playing a Video
|
| 707 |
-
To play a video, simply load it with cv2.VideoCapture() and display the frames in a loop:
|
| 708 |
|
| 709 |
-
python
|
| 710 |
import cv2
|
| 711 |
# Open the video file
|
| 712 |
video_path = 'your_video.mp4'
|
|
@@ -721,10 +817,10 @@ def handling_video_data_page():
|
|
| 721 |
break
|
| 722 |
cap.release()
|
| 723 |
cv2.destroyAllWindows()
|
| 724 |
-
|
| 725 |
#### Live Video Capturing
|
| 726 |
-
You can use the webcam for live video capturing by passing 0 to cv2.VideoCapture(), which opens the default camera:
|
| 727 |
-
python
|
| 728 |
import cv2
|
| 729 |
# Open the webcam (0 refers to the default camera)
|
| 730 |
cap = cv2.VideoCapture(0)
|
|
@@ -738,10 +834,10 @@ def handling_video_data_page():
|
|
| 738 |
break
|
| 739 |
cap.release()
|
| 740 |
cv2.destroyAllWindows()
|
| 741 |
-
|
| 742 |
In this example:
|
| 743 |
-
- VideoCapture(0) captures live video from the webcam.
|
| 744 |
-
- cv2.imshow() displays the video frames.
|
| 745 |
- The loop will run until the video ends or you press 'q' to quit.
|
| 746 |
""")
|
| 747 |
|
|
@@ -778,31 +874,31 @@ def image_video_projects_page():
|
|
| 778 |
st.write("""
|
| 779 |
### 1. Converting an Image into Tabular Data
|
| 780 |
This amazing project explains how we can convert an image into tabular data. It involves extracting pixel values from an image and representing them as tabular data for analysis or machine learning tasks.
|
| 781 |
-
[Check out the project on GitHub π](https://github.com/
|
| 782 |
""")
|
| 783 |
|
| 784 |
st.write("""
|
| 785 |
### 2. Converting a Video into Tabular Data
|
| 786 |
In this project, we explore how we can convert a video into tabular data by extracting frames and storing the pixel data of each frame as rows in a table. This allows us to analyze videos in a more structured form.
|
| 787 |
-
[Check out the project on GitHub π](https://github.com/
|
| 788 |
""")
|
| 789 |
|
| 790 |
st.write("""
|
| 791 |
### 3. Animation Project
|
| 792 |
This project demonstrates how to create interesting animation videos using the OpenCV package. Youβll learn how to manipulate frames and add effects to create smooth animations.
|
| 793 |
-
[Check out the project on GitHub π](https://github.com/
|
| 794 |
""")
|
| 795 |
|
| 796 |
st.write("""
|
| 797 |
### 4. GIF Project
|
| 798 |
This project teaches how to create an engaging GIF using OpenCV. We will learn how to generate frame sequences and combine them into a GIF format that can be used for sharing or websites.
|
| 799 |
-
[Check out the project on GitHub π](https://github.com/
|
| 800 |
""")
|
| 801 |
|
| 802 |
st.write("""
|
| 803 |
### 5. Cropping Tool
|
| 804 |
This project walks through how to build a cropping tool using OpenCV, allowing users to select and extract specific regions of an image interactively.
|
| 805 |
-
[Check out the project on GitHub π](https://github.com/
|
| 806 |
""")
|
| 807 |
|
| 808 |
main_button = st.markdown("""
|
|
@@ -812,9 +908,109 @@ def image_video_projects_page():
|
|
| 812 |
|
| 813 |
""", unsafe_allow_html=True)
|
| 814 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
|
| 816 |
params = st.query_params # Access query parameters
|
| 817 |
-
page = params.get("page", ["main","data_collection", "basic_operations", "working_on_image","converting_color_spaces","affine_transformations","video_data","projects"]) # Default to 'main' if 'page' doesn't exist
|
| 818 |
|
| 819 |
# Render the page based on the 'page' query parameter
|
| 820 |
if page == "data_collection":
|
|
@@ -831,5 +1027,7 @@ elif page == "video_data":
|
|
| 831 |
handling_video_data_page()
|
| 832 |
elif page == "projects":
|
| 833 |
image_video_projects_page()
|
|
|
|
|
|
|
| 834 |
else:
|
| 835 |
main_page()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# CSS for styling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
custom_css = """
|
| 9 |
<style>
|
| 10 |
+
html, body, [data-testid="stAppViewContainer"] {
|
| 11 |
+
background: linear-gradient(
|
| 12 |
+
rgba(0, 0, 0, 0.6), /* Semi-transparent black overlay */
|
| 13 |
+
rgba(0, 0, 0, 0.6)
|
| 14 |
+
),
|
| 15 |
+
url('https://lh5.googleusercontent.com/uMsECCeKutDPVHLEgNxGD069b96jVsgQ9fohrZUXie2f5Il5sk0NurHcrbQi_74OB2issA24btF0jL1Va17r2zTF2jOTu1iUiZIoCvO_r3BVodh3bMD2VzQbLecUa5N7kZHdhIASEBxBntmy4rkqv8U') no-repeat center center fixed;
|
| 16 |
+
background-size: cover; /* Cover the entire screen */
|
| 17 |
+
font-family: Arial, sans-serif;
|
| 18 |
+
color: #ffffff;
|
| 19 |
+
}
|
| 20 |
+
h1 {
|
| 21 |
+
margin: 20px 0;
|
| 22 |
+
font-size: 2.5rem;
|
| 23 |
+
}
|
| 24 |
+
.circle-container {
|
| 25 |
+
display: flex;
|
| 26 |
+
justify-content: center;
|
| 27 |
+
align-items: center;
|
| 28 |
+
position: relative;
|
| 29 |
+
width: 500px;
|
| 30 |
+
height: 500px;
|
| 31 |
+
margin: 50px;
|
| 32 |
+
background: transparent;
|
| 33 |
+
transform: translate(-50px, -50px); /* Move the container 50px to the left */
|
| 34 |
+
}
|
| 35 |
+
.circle-container .button {
|
| 36 |
+
position: absolute;
|
| 37 |
+
width: 100px;
|
| 38 |
+
height: 100px;
|
| 39 |
+
background: #1e1e2f;
|
| 40 |
+
color: white;
|
| 41 |
+
border: 2px solid #555;
|
| 42 |
+
border-radius: 50%;
|
| 43 |
+
display: flex;
|
| 44 |
+
justify-content: center;
|
| 45 |
+
align-items: center;
|
| 46 |
+
font-size: 0.9rem;
|
| 47 |
+
text-align: center;
|
| 48 |
+
text-decoration: none;
|
| 49 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
|
| 50 |
+
cursor: pointer;
|
| 51 |
+
transition: background 0.2s, box-shadow 0.2s;
|
| 52 |
+
}
|
| 53 |
+
.circle-container .button:hover {
|
| 54 |
+
background: #333; /* Change background color */
|
| 55 |
+
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.3); /* Add shadow on hover */
|
| 56 |
+
}
|
| 57 |
+
/* Positioning buttons in a circular layout */
|
| 58 |
+
.button:nth-child(1) { transform: rotate(270deg) translate(200px) rotate(-270deg); top: 50%; left: 50%; }
|
| 59 |
+
.button:nth-child(2) { transform: rotate(306deg) translate(200px) rotate(-306deg); top: 50%; left: 50%; }
|
| 60 |
+
.button:nth-child(3) { transform: rotate(342deg) translate(200px) rotate(-342deg); top: 50%; left: 50%; }
|
| 61 |
+
.button:nth-child(4) { transform: rotate(378deg) translate(200px) rotate(-378deg); top: 50%; left: 50%; }
|
| 62 |
+
.button:nth-child(5) { transform: rotate(414deg) translate(200px) rotate(-414deg); top: 50%; left: 50%; }
|
| 63 |
+
.button:nth-child(6) { transform: rotate(450deg) translate(200px) rotate(-450deg); top: 50%; left: 50%; }
|
| 64 |
+
.button:nth-child(7) { transform: rotate(486deg) translate(200px) rotate(-486deg); top: 50%; left: 50%; }
|
| 65 |
+
.button:nth-child(8) { transform: rotate(522deg) translate(200px) rotate(-522deg); top: 50%; left: 50%; }
|
| 66 |
+
.button:nth-child(9) { transform: rotate(558deg) translate(200px) rotate(-558deg); top: 50%; left: 50%; }
|
| 67 |
+
.button:nth-child(10) { transform: rotate(594deg) translate(200px) rotate(-594deg); top: 50%; left: 50%; }
|
| 68 |
+
.center {
|
| 69 |
+
position: absolute;
|
| 70 |
+
top: 60%;
|
| 71 |
+
left: 50%;
|
| 72 |
+
font-size: 1.2rem;
|
| 73 |
+
color: white;
|
| 74 |
+
text-align: center;
|
| 75 |
+
transform: translate(-0%, -50%);
|
| 76 |
+
}
|
| 77 |
</style>
|
| 78 |
"""
|
| 79 |
|
|
|
|
| 106 |
st.write("""
|
| 107 |
### What is data?
|
| 108 |
Data refers to raw facts, figures, or information that can be processed, analyzed, and used to generate knowledge or insights. Data can come in various forms and can be categorized based on its structure, meaning, and usage.
|
| 109 |
+
**Types of Data:** """)
|
| 110 |
|
| 111 |
# Show buttons for different types of data
|
| 112 |
structured_data = st.button('Structured Data')
|
|
|
|
| 134 |
### What is Excel?
|
| 135 |
Excel is a spreadsheet program from Microsoft that is used for organizing, analyzing, and storing data in tabular form.
|
| 136 |
#### How to Read Excel Files?
|
| 137 |
+
You can read Excel files using libraries like `pandas`:
|
| 138 |
+
```python
|
| 139 |
import pandas as pd
|
| 140 |
df = pd.read_excel('file.xlsx')
|
| 141 |
+
```
|
| 142 |
#### Issues with Excel:
|
| 143 |
- Large file sizes leading to memory errors.
|
| 144 |
- Formatting issues that can interfere with data analysis.
|
| 145 |
#### Overcoming Issues:
|
| 146 |
+
- Use `openpyxl` for large files.
|
| 147 |
- Ensure proper formatting before importing into tools.
|
| 148 |
""")
|
| 149 |
|
|
|
|
| 188 |
CSV stands for Comma-Separated Values. It is a simple file format used to store tabular data such as a spreadsheet or database.
|
| 189 |
|
| 190 |
#### How to Read CSV Files?
|
| 191 |
+
You can read CSV files using `pandas`:
|
| 192 |
+
```python
|
| 193 |
import pandas as pd
|
| 194 |
df = pd.read_csv('file.csv')
|
| 195 |
+
```
|
| 196 |
|
| 197 |
#### Issues with CSV:
|
| 198 |
- Inconsistent delimiters.
|
| 199 |
- Missing values.
|
| 200 |
|
| 201 |
#### Overcoming Issues:
|
| 202 |
+
- Use the `delimiter` parameter in `pandas.read_csv()`.
|
| 203 |
+
- Handle missing values using `dropna()` or `fillna()`.
|
| 204 |
""")
|
| 205 |
|
| 206 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
|
|
| 222 |
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
|
| 223 |
|
| 224 |
#### How to Read JSON Files?
|
| 225 |
+
You can read JSON files using `pandas` or the `json` library:
|
| 226 |
+
```python
|
| 227 |
import pandas as pd
|
| 228 |
df = pd.read_json('file.json')
|
| 229 |
+
```
|
| 230 |
|
| 231 |
+
Or using the `json` library:
|
| 232 |
+
```python
|
| 233 |
import json
|
| 234 |
with open('file.json', 'r') as file:
|
| 235 |
data = json.load(file)
|
| 236 |
+
```
|
| 237 |
|
| 238 |
#### Issues with JSON:
|
| 239 |
- Nested structures can be difficult to parse.
|
| 240 |
- Missing keys in objects.
|
| 241 |
|
| 242 |
#### Overcoming Issues:
|
| 243 |
+
- Use `normalize` parameter in `pandas.json_normalize()` to flatten nested data.
|
| 244 |
+
- Handle missing keys by providing default values using `.get()`.
|
| 245 |
""")
|
| 246 |
|
| 247 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
|
|
| 265 |
XML (eXtensible Markup Language) is a markup language that defines rules for encoding documents in a format that is both human-readable and machine-readable.
|
| 266 |
|
| 267 |
#### How to Read XML Files?
|
| 268 |
+
You can read XML files using libraries like `xml.etree.ElementTree` or `pandas`:
|
| 269 |
+
```python
|
| 270 |
import pandas as pd
|
| 271 |
df = pd.read_xml('file.xml')
|
| 272 |
+
```
|
| 273 |
|
| 274 |
+
Or using `xml.etree.ElementTree`:
|
| 275 |
+
```python
|
| 276 |
import xml.etree.ElementTree as ET
|
| 277 |
tree = ET.parse('file.xml')
|
| 278 |
root = tree.getroot()
|
| 279 |
+
```
|
| 280 |
|
| 281 |
#### Issues with XML:
|
| 282 |
- Handling namespaces can be tricky.
|
| 283 |
- Large files can lead to memory issues.
|
| 284 |
|
| 285 |
#### Overcoming Issues:
|
| 286 |
+
- Use libraries like `lxml` for better namespace support.
|
| 287 |
+
- Parse large files incrementally using `iterparse`.
|
| 288 |
""")
|
| 289 |
|
| 290 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
|
|
| 307 |
HTML (HyperText Markup Language) is the standard markup language for documents designed to be displayed in a web browser.
|
| 308 |
|
| 309 |
#### How to Read HTML Files?
|
| 310 |
+
You can read HTML files using `pandas` or `BeautifulSoup`:
|
| 311 |
+
```python
|
| 312 |
import pandas as pd
|
| 313 |
df = pd.read_html('file.html')[0] # Read the first table
|
| 314 |
+
```
|
| 315 |
|
| 316 |
+
Or using `BeautifulSoup`:
|
| 317 |
+
```python
|
| 318 |
from bs4 import BeautifulSoup
|
| 319 |
with open('file.html', 'r') as file:
|
| 320 |
soup = BeautifulSoup(file, 'html.parser')
|
| 321 |
print(soup.prettify())
|
| 322 |
+
```
|
| 323 |
|
| 324 |
#### Issues with HTML:
|
| 325 |
- Parsing tables can be complex when the structure is inconsistent.
|
| 326 |
- Missing or malformed tags.
|
| 327 |
|
| 328 |
#### Overcoming Issues:
|
| 329 |
+
- Use `pandas.read_html()` for well-structured tables.
|
| 330 |
+
- Handle malformed tags using `BeautifulSoup` with `lxml` parser.
|
| 331 |
""")
|
| 332 |
|
| 333 |
# After presenting information, add the button for downloading the notebook or PDF
|
|
|
|
| 357 |
The features in a pixel include information like shape, color, and pattern.
|
| 358 |
The clarity of an image directly depends on the number of pixels it has.
|
| 359 |
|
| 360 |
+
**Every array cannot be an image. An array can be an image only when:**
|
| 361 |
1. It should be in 2D or 3D representation.
|
| 362 |
2. The datatype should only be an integer.
|
| 363 |
|
| 364 |
### What are Color Spaces?
|
| 365 |
Color Space is a technique by which we can represent the colors of an image.
|
| 366 |
There are 3 types of color spaces namely:
|
| 367 |
+
1. **Black & White Color Space**
|
| 368 |
+
2. **Grayscale Color Space**
|
| 369 |
+
3. **RGB Color Space**
|
| 370 |
|
| 371 |
#### Black & White Color Space
|
| 372 |
In this Color Space, there are only 2 colors to represent the image, which are black & white.
|
| 373 |
+
Here, `0` represents black and `1` represents white.
|
| 374 |
|
| 375 |
#### Grayscale Color Space
|
| 376 |
In this Color Space, we have black, white, and multiple shades of gray to represent the image.
|
| 377 |
+
Here, `0` represents black, `255` represents white, and `1` to `254` represent various shades of gray.
|
| 378 |
|
| 379 |
#### RGB Color Space
|
| 380 |
In this Color Space, we create a 3D structure with three 2D channels, namely blue, green, and red channels, where:
|
| 381 |
+
- `0` represents the absence of color.
|
| 382 |
+
- `255` represents the presence of color.
|
| 383 |
|
| 384 |
These 3 channels are stacked one after another like a layered structure:
|
| 385 |
+
- **Blue channel**: `0` represents black, `255` represents blue, and `1` to `254` represent multiple shades of blue.
|
| 386 |
+
- **Green channel**: `0` represents black, `255` represents green, and `1` to `254` represent multiple shades of green.
|
| 387 |
+
- **Red channel**: `0` represents black, `255` represents red, and `1` to `254` represent multiple shades of red.
|
| 388 |
""")
|
| 389 |
|
| 390 |
basic_operations_button = st.markdown("""
|
|
|
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
+
video_button = st.button("Videos")
|
| 408 |
+
if video_button:
|
| 409 |
+
st.markdown("<h1>Handling Video Data</h1>", unsafe_allow_html=True)
|
| 410 |
+
st.write("""
|
| 411 |
+
### What is a Video?
|
| 412 |
+
A video is a sequence of images, called frames, displayed rapidly one after another to create the illusion of motion.
|
| 413 |
+
|
| 414 |
+
### Working with Video Data in OpenCV
|
| 415 |
+
In OpenCV, we can handle video data using the `cv2.VideoCapture()` method, which allows us to capture video frames from a video file or a live camera feed.
|
| 416 |
+
|
| 417 |
+
#### Converting a Video into Frames
|
| 418 |
+
The `VideoCapture()` method is used to load a video, and you can read each frame individually using the `read()` method. Below is an example of how to extract frames from a video:
|
| 419 |
+
|
| 420 |
+
```python
|
| 421 |
+
import cv2
|
| 422 |
+
|
| 423 |
+
# Open the video file
|
| 424 |
+
video_path = 'your_video.mp4'
|
| 425 |
+
cap = cv2.VideoCapture(video_path)
|
| 426 |
+
|
| 427 |
+
while cap.isOpened():
|
| 428 |
+
ret, frame = cap.read()
|
| 429 |
+
if ret:
|
| 430 |
+
# Process each frame
|
| 431 |
+
cv2.imshow('Frame', frame) # Show the frame
|
| 432 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 433 |
+
break
|
| 434 |
+
else:
|
| 435 |
+
break
|
| 436 |
+
|
| 437 |
+
cap.release()
|
| 438 |
+
cv2.destroyAllWindows()
|
| 439 |
+
```
|
| 440 |
+
|
| 441 |
+
#### Playing a Video
|
| 442 |
+
To play a video, simply load it with `cv2.VideoCapture()` and display the frames in a loop:
|
| 443 |
+
|
| 444 |
+
```python
|
| 445 |
+
import cv2
|
| 446 |
+
|
| 447 |
+
# Open the video file
|
| 448 |
+
video_path = 'your_video.mp4'
|
| 449 |
+
cap = cv2.VideoCapture(video_path)
|
| 450 |
+
|
| 451 |
+
while cap.isOpened():
|
| 452 |
+
ret, frame = cap.read()
|
| 453 |
+
if ret:
|
| 454 |
+
cv2.imshow('Video', frame) # Display the frame
|
| 455 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 456 |
+
break
|
| 457 |
+
else:
|
| 458 |
+
break
|
| 459 |
+
|
| 460 |
+
cap.release()
|
| 461 |
+
cv2.destroyAllWindows()
|
| 462 |
+
```
|
| 463 |
+
|
| 464 |
+
#### Live Video Capturing
|
| 465 |
+
You can use the webcam for live video capturing by passing `0` to `cv2.VideoCapture()`, which opens the default camera:
|
| 466 |
+
|
| 467 |
+
```python
|
| 468 |
+
import cv2
|
| 469 |
+
|
| 470 |
+
# Open the webcam (0 refers to the default camera)
|
| 471 |
+
cap = cv2.VideoCapture(0)
|
| 472 |
+
|
| 473 |
+
while cap.isOpened():
|
| 474 |
+
ret, frame = cap.read()
|
| 475 |
+
if ret:
|
| 476 |
+
cv2.imshow('Live Video', frame) # Display the live frame
|
| 477 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 478 |
+
break
|
| 479 |
+
else:
|
| 480 |
+
break
|
| 481 |
+
|
| 482 |
+
cap.release()
|
| 483 |
+
cv2.destroyAllWindows()
|
| 484 |
+
```
|
| 485 |
|
| 486 |
+
In this example:
|
| 487 |
+
- `VideoCapture(0)` captures live video from the webcam.
|
| 488 |
+
- `cv2.imshow()` displays the video frames.
|
| 489 |
+
- The loop will run until the video ends or you press 'q' to quit.
|
| 490 |
+
""")
|
| 491 |
+
|
| 492 |
+
if st.button("Open Jupyter Notebook"):
|
| 493 |
+
st.session_state['jupyter_clicked'] = True
|
| 494 |
+
|
| 495 |
+
if st.session_state['jupyter_clicked']:
|
| 496 |
+
st.markdown(
|
| 497 |
+
"""
|
| 498 |
+
<div class="section">
|
| 499 |
+
<div class="header">Jupyter Notebook for Handling Video Data</div>
|
| 500 |
+
<div class="content">This Jupyter notebook explains how to handle video data, convert videos into frames, and capture live video using OpenCV.</div>
|
| 501 |
+
</div>
|
| 502 |
+
""",
|
| 503 |
+
unsafe_allow_html=True,
|
| 504 |
+
)
|
| 505 |
+
# Embed the converted HTML file for the notebook
|
| 506 |
+
notebook_html_path = "notebooks/handling_video_data.html"
|
| 507 |
+
with open(notebook_html_path, "r") as f:
|
| 508 |
+
notebook_html = f.read()
|
| 509 |
+
st.components.v1.html(notebook_html, height=500, scrolling=True)
|
| 510 |
+
|
| 511 |
+
projects_button = st.markdown("""
|
| 512 |
+
<a href="?page=projects" target="_blank">
|
| 513 |
+
<button style="font-size:16px;padding:10px 20px;margin-top:20px;">Projects on Image and Video Data</button>
|
| 514 |
+
</a>
|
| 515 |
+
|
| 516 |
+
""", unsafe_allow_html=True)
|
| 517 |
|
| 518 |
+
|
|
|
|
| 519 |
st.button("Text Files")
|
| 520 |
st.button("Audio")
|
| 521 |
|
|
|
|
| 530 |
st.write("""
|
| 531 |
### Operations on image data
|
| 532 |
There are 3 major operations which can be performed on an image, namely:
|
| 533 |
+
1. **Reading an Image**
|
| 534 |
+
2. **Writing an Image**
|
| 535 |
+
3. **Showing an Image**
|
| 536 |
#### Reading an Image
|
| 537 |
+
Import the `cv2` module and use the method `imread()` to convert an image file into a NumPy array.
|
| 538 |
#### Writing an Image
|
| 539 |
+
Import the `cv2` module and use the method `imwrite()` to convert a NumPy array back into an image file.
|
| 540 |
#### Showing an Image
|
| 541 |
+
Import the `cv2` module and use the method `imshow()` to display an array in the form of an image via a popup window.
|
| 542 |
""")
|
| 543 |
|
| 544 |
if st.button("Open Jupyter Notebook"):
|
|
|
|
| 572 |
st.markdown(custom_css, unsafe_allow_html=True)
|
| 573 |
st.markdown("<h1>Working on the Image</h1>", unsafe_allow_html=True)
|
| 574 |
st.write("""
|
| 575 |
+
### Understanding `split()` Method
|
| 576 |
+
For this operation, we have to import the `cv2` module and use the `split()` method.
|
| 577 |
|
| 578 |
+
The `split()` method is used to separate a multi-channel image into its individual single-channel components.
|
| 579 |
|
| 580 |
#### Example Code:
|
| 581 |
+
```python
|
| 582 |
import cv2
|
| 583 |
img = cv2.imread('image.jpg') # Read an image
|
| 584 |
b, g, r = cv2.split(img) # Splits into Blue, Green, and Red channels
|
| 585 |
+
```
|
| 586 |
+
### Understanding `merge()` Method
|
| 587 |
+
For this operation, we have to import the `cv2` module and use the `merge()` method.
|
| 588 |
|
| 589 |
+
The `merge()` method is used to combine multiple single-channel images into a single multi-channel image.
|
|
|
|
|
|
|
|
|
|
| 590 |
|
| 591 |
#### Example Code:
|
| 592 |
+
```python
|
| 593 |
import cv2
|
| 594 |
merged_img = cv2.merge([b, g, r]) # Combines Blue, Green, and Red channels into one image
|
| 595 |
+
```
|
| 596 |
""")
|
| 597 |
|
| 598 |
if st.button("Open Jupyter Notebook"):
|
|
|
|
| 627 |
st.markdown("<h1>Conversion between Color Spaces</h1>", unsafe_allow_html=True)
|
| 628 |
st.write("""
|
| 629 |
### How to Convert One Color Space to Another?
|
| 630 |
+
In the `cv2` module, we have an in-built method called `cvtColor()` along with specific parameters for each conversion.
|
| 631 |
#### Common Conversions:
|
| 632 |
+
1. **BGR to Grayscale**:
|
| 633 |
+
- Use the parameter `COLOR_BGR2GRAY` inside the `cvtColor()` method.
|
| 634 |
+
```python
|
| 635 |
import cv2
|
| 636 |
img = cv2.imread('image.jpg')
|
| 637 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 638 |
+
```
|
| 639 |
+
2. **Grayscale to BGR**:
|
| 640 |
+
- Use the parameter `COLOR_GRAY2BGR` inside the `cvtColor()` method.
|
| 641 |
+
```python
|
| 642 |
import cv2
|
| 643 |
img = cv2.imread('gray_image.jpg', 0) # Load a grayscale image
|
| 644 |
bgr_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 645 |
+
```
|
| 646 |
+
3. **BGR to RGB**:
|
| 647 |
+
- Use the parameter `COLOR_BGR2RGB` inside the `cvtColor()` method.
|
| 648 |
+
```python
|
| 649 |
import cv2
|
| 650 |
img = cv2.imread('image.jpg')
|
| 651 |
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 652 |
+
```
|
| 653 |
""")
|
| 654 |
|
| 655 |
if st.button("Open Jupyter Notebook"):
|
|
|
|
| 687 |
### How to Apply Affine Transformations on Images?
|
| 688 |
Affine transformations are used in image augmentation to manipulate and alter images by applying operations such as translation, rotation, scaling, and shearing. These transformations help to generate new images from the existing ones, thereby increasing the dataset diversity.
|
| 689 |
#### Common Affine Transformations:
|
| 690 |
+
1. **Translation**:
|
| 691 |
- Shift the image by a specified number of pixels on the X and Y axes.
|
| 692 |
+
```python
|
| 693 |
import cv2
|
| 694 |
import numpy as np
|
| 695 |
img = cv2.imread('image.jpg')
|
| 696 |
rows, cols, _ = img.shape
|
| 697 |
M = np.float32([[1, 0, 50], [0, 1, 100]]) # Translation matrix
|
| 698 |
translated_img = cv2.warpAffine(img, M, (cols, rows))
|
| 699 |
+
```
|
| 700 |
+
2. **Rotation**:
|
| 701 |
- Rotate the image by an angle at a specified center.
|
| 702 |
+
```python
|
| 703 |
import cv2
|
| 704 |
import numpy as np
|
| 705 |
img = cv2.imread('image.jpg')
|
| 706 |
rows, cols, _ = img.shape
|
| 707 |
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1) # Rotation matrix
|
| 708 |
rotated_img = cv2.warpAffine(img, M, (cols, rows))
|
| 709 |
+
```
|
| 710 |
+
3. **Scaling**:
|
| 711 |
- Increase or decrease the image size by a particular factor.
|
| 712 |
+
```python
|
| 713 |
import cv2
|
| 714 |
img = cv2.imread('image.jpg')
|
| 715 |
scaled_img = cv2.resize(img, None, fx=1.5, fy=1.5) # Scaling by factor of 1.5
|
| 716 |
+
```
|
| 717 |
+
4. **Shearing**:
|
| 718 |
- Apply shearing transformation to stretch the image from its edges.
|
| 719 |
+
```python
|
| 720 |
import cv2
|
| 721 |
import numpy as np
|
| 722 |
img = cv2.imread('image.jpg')
|
| 723 |
rows, cols, _ = img.shape
|
| 724 |
M = np.float32([[1, 0.5, 0], [0.5, 1, 0]]) # Shearing matrix
|
| 725 |
sheared_img = cv2.warpAffine(img, M, (cols, rows))
|
| 726 |
+
```
|
| 727 |
+
5. **Cropping**:
|
| 728 |
- Extract or cut a specific part of the image.
|
| 729 |
+
```python
|
| 730 |
import cv2
|
| 731 |
img = cv2.imread('image.jpg')
|
| 732 |
cropped_img = img[50:200, 100:300] # Manually cropping using slicing
|
| 733 |
+
```
|
| 734 |
#### How to Combine Transformations?
|
| 735 |
You can combine translation, rotation, scaling, and shearing into a single transformation matrix:
|
| 736 |
+
```python
|
| 737 |
import cv2
|
| 738 |
import numpy as np
|
| 739 |
img = cv2.imread('image.jpg')
|
| 740 |
rows, cols, _ = img.shape
|
| 741 |
M = np.float32([[1.2, 0.5, 100], [0.5, 1.2, 50]]) # Combined scaling, shearing, and translation
|
| 742 |
transformed_img = cv2.warpAffine(img, M, (cols, rows))
|
| 743 |
+
```
|
| 744 |
""")
|
| 745 |
|
| 746 |
if st.button("Open Jupyter Notebook"):
|
|
|
|
| 762 |
notebook_html = f.read()
|
| 763 |
st.components.v1.html(notebook_html, height=500, scrolling=True)
|
| 764 |
|
| 765 |
+
|
| 766 |
+
img_aug_button = st.markdown("""
|
| 767 |
+
<a href="?page=img_aug" target="_blank">
|
| 768 |
+
<button style="font-size:16px;padding:10px 20px;margin-top:20px;">Image Augmentation</button>
|
| 769 |
</a>
|
| 770 |
|
| 771 |
""", unsafe_allow_html=True)
|
|
|
|
| 779 |
### What is a Video?
|
| 780 |
A video is a sequence of images, called frames, displayed rapidly one after another to create the illusion of motion.
|
| 781 |
### Working with Video Data in OpenCV
|
| 782 |
+
In OpenCV, we can handle video data using the `cv2.VideoCapture()` method, which allows us to capture video frames from a video file or a live camera feed.
|
| 783 |
#### Converting a Video into Frames
|
| 784 |
+
The `VideoCapture()` method is used to load a video, and you can read each frame individually using the `read()` method. Below is an example of how to extract frames from a video:
|
| 785 |
+
```python
|
| 786 |
import cv2
|
| 787 |
# Open the video file
|
| 788 |
video_path = 'your_video.mp4'
|
|
|
|
| 798 |
break
|
| 799 |
cap.release()
|
| 800 |
cv2.destroyAllWindows()
|
| 801 |
+
```
|
| 802 |
#### Playing a Video
|
| 803 |
+
To play a video, simply load it with `cv2.VideoCapture()` and display the frames in a loop:
|
| 804 |
|
| 805 |
+
```python
|
| 806 |
import cv2
|
| 807 |
# Open the video file
|
| 808 |
video_path = 'your_video.mp4'
|
|
|
|
| 817 |
break
|
| 818 |
cap.release()
|
| 819 |
cv2.destroyAllWindows()
|
| 820 |
+
```
|
| 821 |
#### Live Video Capturing
|
| 822 |
+
You can use the webcam for live video capturing by passing `0` to `cv2.VideoCapture()`, which opens the default camera:
|
| 823 |
+
```python
|
| 824 |
import cv2
|
| 825 |
# Open the webcam (0 refers to the default camera)
|
| 826 |
cap = cv2.VideoCapture(0)
|
|
|
|
| 834 |
break
|
| 835 |
cap.release()
|
| 836 |
cv2.destroyAllWindows()
|
| 837 |
+
```
|
| 838 |
In this example:
|
| 839 |
+
- `VideoCapture(0)` captures live video from the webcam.
|
| 840 |
+
- `cv2.imshow()` displays the video frames.
|
| 841 |
- The loop will run until the video ends or you press 'q' to quit.
|
| 842 |
""")
|
| 843 |
|
|
|
|
| 874 |
st.write("""
|
| 875 |
### 1. Converting an Image into Tabular Data
|
| 876 |
This amazing project explains how we can convert an image into tabular data. It involves extracting pixel values from an image and representing them as tabular data for analysis or machine learning tasks.
|
| 877 |
+
[Check out the project on GitHub π](https://github.com/karthiksagirala13/Innomatics/blob/main/converting_image_into_tabular_data.ipynb)
|
| 878 |
""")
|
| 879 |
|
| 880 |
st.write("""
|
| 881 |
### 2. Converting a Video into Tabular Data
|
| 882 |
In this project, we explore how we can convert a video into tabular data by extracting frames and storing the pixel data of each frame as rows in a table. This allows us to analyze videos in a more structured form.
|
| 883 |
+
[Check out the project on GitHub π](https://github.com/karthiksagirala13/Innomatics/blob/main/converting_videos_into_tabular_data.ipynb)
|
| 884 |
""")
|
| 885 |
|
| 886 |
st.write("""
|
| 887 |
### 3. Animation Project
|
| 888 |
This project demonstrates how to create interesting animation videos using the OpenCV package. Youβll learn how to manipulate frames and add effects to create smooth animations.
|
| 889 |
+
[Check out the project on GitHub π](https://github.com/karthiksagirala13/Innomatics/blob/main/Animations%20Project.ipynb)
|
| 890 |
""")
|
| 891 |
|
| 892 |
st.write("""
|
| 893 |
### 4. GIF Project
|
| 894 |
This project teaches how to create an engaging GIF using OpenCV. We will learn how to generate frame sequences and combine them into a GIF format that can be used for sharing or websites.
|
| 895 |
+
[Check out the project on GitHub π](https://github.com/karthiksagirala13/Innomatics/blob/main/GIF_project.ipynb)
|
| 896 |
""")
|
| 897 |
|
| 898 |
st.write("""
|
| 899 |
### 5. Cropping Tool
|
| 900 |
This project walks through how to build a cropping tool using OpenCV, allowing users to select and extract specific regions of an image interactively.
|
| 901 |
+
[Check out the project on GitHub π](https://github.com/karthiksagirala13/Innomatics/blob/main/cropping_tool.ipynb)
|
| 902 |
""")
|
| 903 |
|
| 904 |
main_button = st.markdown("""
|
|
|
|
| 908 |
|
| 909 |
""", unsafe_allow_html=True)
|
| 910 |
|
| 911 |
+
|
| 912 |
+
|
| 913 |
+
|
| 914 |
+
def img_aug_page():
|
| 915 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 916 |
+
st.title("Image Augmentation Techniques with OpenCV")
|
| 917 |
+
|
| 918 |
+
st.markdown("""
|
| 919 |
+
<div class="section">
|
| 920 |
+
<h2>What is Image Augmentation?</h2>
|
| 921 |
+
<p>
|
| 922 |
+
Image augmentation is a technique used to artificially expand the size of a dataset by creating modified versions of images.
|
| 923 |
+
These modifications help improve the performance and robustness of machine learning models by enabling them to generalize better to unseen data.
|
| 924 |
+
</p>
|
| 925 |
+
<ul>
|
| 926 |
+
<li><strong>Scaling:</strong> Resizing the image.</li>
|
| 927 |
+
<li><strong>Translation:</strong> Shifting the image along X and Y axes.</li>
|
| 928 |
+
<li><strong>Shearing:</strong> Skewing the image along an axis.</li>
|
| 929 |
+
<li><strong>Rotation:</strong> Rotating the image by a specified angle.</li>
|
| 930 |
+
<li><strong>Cropping:</strong> Extracting a specific region of interest from the image.</li>
|
| 931 |
+
</ul>
|
| 932 |
+
<p>
|
| 933 |
+
These techniques are widely used in computer vision tasks like object detection, image classification, and segmentation.
|
| 934 |
+
</p>
|
| 935 |
+
</div>
|
| 936 |
+
""", unsafe_allow_html=True)
|
| 937 |
+
|
| 938 |
+
# Upload an image
|
| 939 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 940 |
+
|
| 941 |
+
if uploaded_file:
|
| 942 |
+
image = np.array(Image.open(uploaded_file))
|
| 943 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 944 |
+
|
| 945 |
+
# Augmentation options
|
| 946 |
+
st.sidebar.header("Select Augmentation Technique")
|
| 947 |
+
options = st.sidebar.multiselect(
|
| 948 |
+
"Choose techniques",
|
| 949 |
+
["Scaling", "Translation", "Shearing", "Rotation", "Cropping"]
|
| 950 |
+
)
|
| 951 |
+
|
| 952 |
+
augmented_images = []
|
| 953 |
+
|
| 954 |
+
# Scaling
|
| 955 |
+
if "Scaling" in options:
|
| 956 |
+
sx = st.sidebar.slider("Scale Factor X", 0.5, 2.0, 1.0, 0.1)
|
| 957 |
+
sy = st.sidebar.slider("Scale Factor Y", 0.5, 2.0, 1.0, 0.1)
|
| 958 |
+
h, w = image.shape[:2]
|
| 959 |
+
scaling_matrix = np.array([[sx, 0, 0], [0, sy, 0]], dtype=np.float32)
|
| 960 |
+
scaled_img = cv2.warpAffine(image, scaling_matrix, (int(w * sx), int(h * sy)))
|
| 961 |
+
augmented_images.append(("Scaled Image", scaled_img))
|
| 962 |
+
|
| 963 |
+
# Translation
|
| 964 |
+
if "Translation" in options:
|
| 965 |
+
tx = st.sidebar.slider("Translate X (pixels)", -100, 100, 0, 5)
|
| 966 |
+
ty = st.sidebar.slider("Translate Y (pixels)", -100, 100, 0, 5)
|
| 967 |
+
h, w = image.shape[:2]
|
| 968 |
+
translation_matrix = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
|
| 969 |
+
translated_img = cv2.warpAffine(image, translation_matrix, (w + tx, h + ty))
|
| 970 |
+
augmented_images.append(("Translated Image", translated_img))
|
| 971 |
+
|
| 972 |
+
# Shearing
|
| 973 |
+
if "Shearing" in options:
|
| 974 |
+
shx = st.sidebar.slider("Shear X", 0.0, 2.0, 0.0, 0.1)
|
| 975 |
+
shy = st.sidebar.slider("Shear Y", 0.0, 2.0, 0.0, 0.1)
|
| 976 |
+
h, w = image.shape[:2]
|
| 977 |
+
shearing_matrix = np.array([[1, shx, 0], [shy, 1, 0]], dtype=np.float32)
|
| 978 |
+
sheared_img = cv2.warpAffine(image, shearing_matrix, (int(w * 1.5), int(h * 1.5)))
|
| 979 |
+
augmented_images.append(("Sheared Image", sheared_img))
|
| 980 |
+
|
| 981 |
+
# Rotation
|
| 982 |
+
if "Rotation" in options:
|
| 983 |
+
angle = st.sidebar.slider("Rotation Angle (degrees)", -180, 180, 0, 5)
|
| 984 |
+
h, w = image.shape[:2]
|
| 985 |
+
center = (w // 2, h // 2)
|
| 986 |
+
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1)
|
| 987 |
+
rotated_img = cv2.warpAffine(image, rotation_matrix, (w, h))
|
| 988 |
+
augmented_images.append(("Rotated Image", rotated_img))
|
| 989 |
+
|
| 990 |
+
# Cropping
|
| 991 |
+
if "Cropping" in options:
|
| 992 |
+
x1 = st.sidebar.slider("Crop X1", 0, image.shape[1] - 1, 0, 1)
|
| 993 |
+
y1 = st.sidebar.slider("Crop Y1", 0, image.shape[0] - 1, 0, 1)
|
| 994 |
+
x2 = st.sidebar.slider("Crop X2", x1 + 1, image.shape[1], image.shape[1], 1)
|
| 995 |
+
y2 = st.sidebar.slider("Crop Y2", y1 + 1, image.shape[0], image.shape[0], 1)
|
| 996 |
+
cropped_img = image[y1:y2, x1:x2]
|
| 997 |
+
augmented_images.append(("Cropped Image", cropped_img))
|
| 998 |
+
|
| 999 |
+
# Display augmented images
|
| 1000 |
+
if augmented_images:
|
| 1001 |
+
st.write("### Augmented Images")
|
| 1002 |
+
for label, img in augmented_images:
|
| 1003 |
+
st.image(img, caption=label, use_column_width=True)
|
| 1004 |
+
|
| 1005 |
+
video_data_button = st.markdown("""
|
| 1006 |
+
<a href="?page=video_data" target="_blank">
|
| 1007 |
+
<button style="font-size:16px;padding:10px 20px;margin-top:20px;">Handling Video Data</button>
|
| 1008 |
+
</a>
|
| 1009 |
+
|
| 1010 |
+
""", unsafe_allow_html=True)
|
| 1011 |
|
| 1012 |
params = st.query_params # Access query parameters
|
| 1013 |
+
page = params.get("page", ["main","data_collection", "basic_operations", "working_on_image","converting_color_spaces","affine_transformations","img_aug", "video_data","projects"]) # Default to 'main' if 'page' doesn't exist
|
| 1014 |
|
| 1015 |
# Render the page based on the 'page' query parameter
|
| 1016 |
if page == "data_collection":
|
|
|
|
| 1027 |
handling_video_data_page()
|
| 1028 |
elif page == "projects":
|
| 1029 |
image_video_projects_page()
|
| 1030 |
+
elif page == "img_aug":
|
| 1031 |
+
img_aug_page()
|
| 1032 |
else:
|
| 1033 |
main_page()
|