hari3485 commited on
Commit
61e591e
·
verified ·
1 Parent(s): 88fde66

Update pages/Data Collection.py

Browse files
Files changed (1) hide show
  1. pages/Data Collection.py +298 -0
pages/Data Collection.py CHANGED
@@ -1357,7 +1357,305 @@ def opencv_projects_page():
1357
  def Image_Augmentation_page():
1358
 
1359
  st.title("Image Augmentation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1361
  if st.button("Back to Home"):
1362
  st.session_state['page'] = "home"
1363
 
 
1357
  def Image_Augmentation_page():
1358
 
1359
  st.title("Image Augmentation")
1360
+ # Heading
1361
+ st.markdown("""
1362
+ <h3 style="color: #9400d3;">What is Image Augmentation?</h3>
1363
+ """, unsafe_allow_html=True)
1364
+
1365
+ # Definition
1366
+ st.write("""
1367
+ Image augmentation is a method used to enhance the size and variety of an image dataset by applying transformations to existing images.
1368
+ These transformations introduce variations while preserving the core features of the image, making it useful for training machine learning models to handle diverse inputs.
1369
+
1370
+ *How It Works*
1371
+ Image augmentation applies transformations like resizing, rotation, flipping, and more to the original image. These changes simulate real-world variations, ensuring that machine learning models can identify patterns even with differences in perspective, scale, or lighting conditions.
1372
+
1373
+ The key idea is to preserve the original features of the image while introducing diversity. For example, if we take an image and apply five different transformations, we generate five new variations of that image. This provides the model with more data to learn from, improving its performance and ability to generalize.
1374
+ """)
1375
+
1376
+ # Types of Image Augmentation
1377
+
1378
+ st.markdown("""
1379
+ <h3 style="color: #9400d3;">Types of Image Augmentation</h3>
1380
+ """, unsafe_allow_html=True)
1381
+
1382
+ st.write("""
1383
+ Image augmentation is broadly categorized into two types:
1384
+
1385
+ 1. *Affine Transformations*
1386
+ 2. *Non-Affine Transformations*
1387
+ """)
1388
+
1389
+ # Affine Transformations
1390
+
1391
+ st.markdown("""
1392
+ <h3 style="color: #9400d3;">Affine Transformations</h3>
1393
+ """, unsafe_allow_html=True)
1394
+
1395
+ st.write("""
1396
+ *Affine Transformations* are transformations where:
1397
+
1398
+ 1. The transformed image and the original image maintain *parallelism between lines*.
1399
+ 2. In some cases, the *angle between lines* and the *length of the lines* may also be preserved.
1400
+
1401
+ These transformations ensure that the geometric relationships within the image remain intact, even as the image is resized, rotated, or shifted.
1402
+
1403
+ Affine transformations are performed using a mathematical operation known as an *Affine Matrix*, which maps the original image coordinates to new coordinates.
1404
+ """)
1405
 
1406
+ st.markdown("""
1407
+ <h3 style="color: #e25822;">Common Affine Transformations:</h3>
1408
+ """, unsafe_allow_html=True)
1409
+
1410
+ st.write("""
1411
+ 1. *Scaling*: Changing the size of the image while maintaining its proportions.
1412
+ 2. *Translation*: Shifting the image horizontally, vertically, or both.
1413
+ 3. *Rotation*: Rotating the image around a specified center point.
1414
+ 4. *Shearing*: Slanting the image along the x or y axis, creating a skewed effect.
1415
+ 5. *Cropping*: Extracting a specific portion of the image, usually to focus on a region of interest.
1416
+
1417
+ These transformations are linear, meaning the relationships between points in the image remain consistent.
1418
+ """)
1419
+
1420
+ st.image(
1421
+ "https://huggingface.co/spaces/LakshmiHarika/MachineLearning/resolve/main/Images/affine_transformations.png",
1422
+ use_container_width=True)
1423
+
1424
+ # Explanation for Translation
1425
+ st.markdown("""
1426
+ <h3 style="color: #9400d3;">Translation</h3>
1427
+ """, unsafe_allow_html=True)
1428
+
1429
+ st.write("""
1430
+ *Translation* involves moving an image from one location to another along the x-axis, y-axis, or both. It adjusts the position of the image on the canvas without modifying its original content.
1431
+
1432
+ The transformation is performed using a translation matrix:
1433
+ """)
1434
+
1435
+ st.write("""
1436
+ The translation matrix is represented as:
1437
+ [[1, 0, tx], [0, 1, ty]]
1438
+
1439
+ Here:
1440
+ - *tx*: Specifies the shift along the x-axis (horizontal axis).
1441
+ - *ty*: Specifies the shift along the y-axis (vertical axis).
1442
+
1443
+ """)
1444
+
1445
+ st.code("""
1446
+ # Load the image
1447
+ img = cv2.imread('path_to_image.jpg')
1448
+
1449
+ # Define translation parameters
1450
+ tx = 100 # Shift 100 pixels along the x-axis
1451
+ ty = 50 # Shift 50 pixels along the y-axis
1452
+
1453
+ # Create the translation matrix
1454
+ translation_matrix = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
1455
+
1456
+ # Apply translation
1457
+ translated_img = cv2.warpAffine(img, translation_matrix, (300, 300))
1458
+
1459
+ # Display the images
1460
+ cv2.imshow("Original Image", img)
1461
+ cv2.imshow("Translated Image", translated_img)
1462
+ cv2.waitKey(0)
1463
+ cv2.destroyAllWindows()
1464
+ """, language="python")
1465
+
1466
+ # Explanation for Rotation
1467
+ st.markdown("""
1468
+ <h3 style="color: #9400d3;">Rotation</h3>
1469
+ """, unsafe_allow_html=True)
1470
+
1471
+ st.write("""
1472
+ *Rotation* involves rotating an image around a specified center point by a given angle. It changes the orientation of the image while preserving its content.
1473
+
1474
+ The rotation is performed using a rotation matrix:
1475
+ [[cos(θ), -sin(θ), tx], [sin(θ), cos(θ), ty]]
1476
+ Here:
1477
+ - *θ (theta)*: Specifies the rotation angle in degrees.
1478
+ - *tx, ty*: Specifies the adjustments to reposition the rotated image.
1479
+ - *Scale*: A factor that can resize the image during rotation.
1480
+ """)
1481
+
1482
+ # Code Example
1483
+ st.code("""
1484
+ # Load the image
1485
+ img = cv2.imread('path_to_image.jpg')
1486
+
1487
+ # Define the rotation matrix
1488
+ r_m = cv2.getRotationMatrix2D((1347, 900), 50, 1) # Center at (1347, 900), Rotate by 50 degrees, Scale = 1
1489
+
1490
+ # Apply rotation
1491
+ r_img = cv2.warpAffine(img, r_m, (580, 500), borderMode=cv2.BORDER_DEFAULT)
1492
+
1493
+ # Display the images
1494
+ cv2.imshow("Original Image", img)
1495
+ cv2.imshow("Rotated Image", r_img)
1496
+
1497
+ cv2.waitKey(0)
1498
+ cv2.destroyAllWindows()
1499
+ """, language="python")
1500
+
1501
+ # Explanation for Direct Rotation
1502
+ st.markdown("""
1503
+ <h3 style="color: #9400d3;">Direct Rotation Using cv2.rotate</h3>
1504
+ """, unsafe_allow_html=True)
1505
+
1506
+ st.write("""
1507
+ OpenCV provides a direct method for rotating images with predefined angles: cv2.rotate.
1508
+ This method simplifies rotation operations for 90°, 180°, and 270° (clockwise or counterclockwise) without requiring a custom rotation matrix.
1509
+
1510
+ - **cv2.ROTATE_180**: Rotates the image by 180°.
1511
+ - **cv2.ROTATE_90_CLOCKWISE**: Rotates the image by 90° clockwise.
1512
+ - **cv2.ROTATE_90_COUNTERCLOCKWISE**: Rotates the image by 90° counterclockwise.
1513
+ """)
1514
+
1515
+ # Code Example
1516
+ st.code("""
1517
+ # Rotate the image using predefined rotation modes
1518
+ img1 = cv2.rotate(img, cv2.ROTATE_180) # Rotate 180 degrees
1519
+ img2 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) # Rotate 90 degrees clockwise
1520
+ img3 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # Rotate 90 degrees counterclockwise
1521
+
1522
+ # Display the images
1523
+ cv2.imshow("Original Image", img)
1524
+ cv2.imshow("Rotated 180°", img1)
1525
+ cv2.imshow("Rotated 90° Clockwise", img2)
1526
+ cv2.imshow("Rotated 90° Counterclockwise", img3)
1527
+
1528
+ cv2.waitKey(0)
1529
+ cv2.destroyAllWindows()
1530
+ """, language="python")
1531
+
1532
+ # Explanation for Shearing
1533
+ st.markdown("""
1534
+ <h3 style="color: #9400d3;">Shearing</h3>
1535
+ """, unsafe_allow_html=True)
1536
+
1537
+ st.write("""
1538
+ *Shearing* is a transformation that slants the shape of an image along the x-axis, y-axis, or both. It skews the content of the image, creating a shifted or stretched effect.
1539
+
1540
+ The transformation is performed using a shearing matrix:
1541
+ """)
1542
+
1543
+ st.write("""
1544
+ The shearing matrix is represented as:
1545
+
1546
+ For x-axis shear:
1547
+ [[1, shx, 0], [0, 1, 0]]
1548
+
1549
+ For y-axis shear:
1550
+ [[1, 0, 0], [shy, 1, 0]]
1551
+
1552
+ Here:
1553
+ - *shx*: Shear factor along the x-axis.
1554
+ - *shy*: Shear factor along the y-axis.
1555
+ """)
1556
+
1557
+ st.code("""
1558
+ # Load the image
1559
+ img = cv2.imread('path_to_image.jpg')
1560
+
1561
+ # Define shearing parameters
1562
+ shx = 1 # Shear factor along the x-axis
1563
+ shy = 3 # Shear factor along the y-axis
1564
+ tx = 0 # Translation along the x-axis
1565
+ ty = 0 # Translation along the y-axis
1566
+
1567
+ # Create the shearing matrix
1568
+ shearing_matrix = np.array([[1, shx, tx], [shy, 1, ty]], dtype=np.float32)
1569
+
1570
+ # Apply the shearing transformation
1571
+ sheared_img = cv2.warpAffine(img, shearing_matrix, (300, 300))
1572
+
1573
+ # Display the original and sheared images
1574
+ cv2.imshow("Original Image", img)
1575
+ cv2.imshow("Sheared Image", sheared_img)
1576
+
1577
+ cv2.waitKey(0)
1578
+ cv2.destroyAllWindows()
1579
+ """, language="python")
1580
+
1581
+
1582
+ # Explanation for Scaling
1583
+ st.markdown("""
1584
+ <h3 style="color: #9400d3;">Scaling</h3>
1585
+ """, unsafe_allow_html=True)
1586
+
1587
+ st.write("""
1588
+ *Scaling* is a transformation that changes the size of an image. It can be used to enlarge or shrink the image while maintaining its original proportions or altering them.
1589
+
1590
+ Scaling is performed using a scaling matrix:
1591
+ """)
1592
+
1593
+ st.write("""
1594
+ The scaling matrix is represented as:
1595
+ [[sx, 0, 0], [0, sy, 0]]
1596
+ Here:
1597
+ - *sx*: Scaling factor along the x-axis.
1598
+ - *sy*: Scaling factor along the y-axis.
1599
+ - If sx and sy are greater than 1, the image is enlarged.
1600
+ - If sx and sy are less than 1, the image is shrunk.
1601
+ """)
1602
+
1603
+ st.code("""
1604
+ # Load the image
1605
+ img = cv2.imread('path_to_image.jpg')
1606
+
1607
+ # Define scaling and translation parameters
1608
+ sx, sy = 2, 1 # Scale by 2 along the x-axis and 1 along the y-axis
1609
+ tx, ty = 0, 0 # No translation
1610
+
1611
+ # Create the scaling matrix
1612
+ scaling_matrix = np.array([[sx, 0, tx], [0, sy, ty]], dtype=np.float32)
1613
+
1614
+ # Apply scaling
1615
+ scaled_img = cv2.warpAffine(img, scaling_matrix, (2 * 300, 300))
1616
+
1617
+ # Display the images
1618
+ cv2.imshow("Original Image", img)
1619
+ cv2.imshow("Scaled Image", scaled_img)
1620
+
1621
+ cv2.waitKey(0)
1622
+ cv2.destroyAllWindows()
1623
+ """, language="python")
1624
+
1625
+
1626
+ # Explanation for Cropping
1627
+ st.markdown("""
1628
+ <h3 style="color: #9400d3;">Cropping</h3>
1629
+ """, unsafe_allow_html=True)
1630
+
1631
+ st.write("""
1632
+ *Cropping* is a transformation that extracts a specific portion of an image, usually to focus on a region of interest.
1633
+ It is achieved by selecting a rectangular region of the image using pixel coordinates.
1634
+
1635
+ The process involves defining the coordinates for:
1636
+ - *Top-left corner (x1, y1)*: Starting point of the crop.
1637
+ - *Bottom-right corner (x2, y2)*: Ending point of the crop.
1638
+ """)
1639
+
1640
+ st.code("""
1641
+ # Load the image
1642
+ img = cv2.imread('path_to_image.jpg')
1643
+
1644
+ # Define crop coordinates
1645
+ x1, y1 = 50, 50 # Top-left corner
1646
+ x2, y2 = 200, 200 # Bottom-right corner
1647
+
1648
+ # Crop the image
1649
+ cropped_img = img[y1:y2, x1:x2]
1650
+
1651
+ # Display the images
1652
+ cv2.imshow("Original Image", img)
1653
+ cv2.imshow("Cropped Image", cropped_img)
1654
+
1655
+ cv2.waitKey(0)
1656
+ cv2.destroyAllWindows()
1657
+ """, language="python")
1658
+
1659
  if st.button("Back to Home"):
1660
  st.session_state['page'] = "home"
1661