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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1276 |
|
| 1277 |
if st.button('Back to unstructured data'):
|
| 1278 |
navigate_to('unstructured_data')
|
|
@@ -1292,14 +1653,248 @@ elif st.session_state.page == "opencv":
|
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1295 |
#------------------------------------------------------ blank ------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1296 |
|
| 1297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1298 |
elif st.session_state.page == "blank":
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
|
|
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
+
if st.button('Advanced OPENCV'):
|
| 1277 |
+
navigate_to('advopencv')
|
| 1278 |
+
|
| 1279 |
+
|
| 1280 |
+
if st.button('Back to unstructured data'):
|
| 1281 |
+
navigate_to('unstructured_data')
|
| 1282 |
+
|
| 1283 |
+
if st.button('Back to Data collection'):
|
| 1284 |
+
navigate_to('data_collection')
|
| 1285 |
+
if st.button("Back to Main Page"):
|
| 1286 |
+
navigate_to("main")
|
| 1287 |
+
|
| 1288 |
+
|
| 1289 |
+
|
| 1290 |
+
|
| 1291 |
+
|
| 1292 |
+
|
| 1293 |
+
|
| 1294 |
+
|
| 1295 |
+
|
| 1296 |
+
#------------------------------------------------------ ADvanced Opencv ------------------------------------------
|
| 1297 |
+
|
| 1298 |
+
|
| 1299 |
+
|
| 1300 |
+
elif st.session_state.page == "advopencv":
|
| 1301 |
+
st.title("Advanced OpenCV Concepts")
|
| 1302 |
+
|
| 1303 |
+
# Section: Converting Image Colors
|
| 1304 |
+
st.header("1. Converting Image Colors with `cv2.cvtColor()`")
|
| 1305 |
+
st.write("""
|
| 1306 |
+
OpenCV allows you to easily convert images between different color spaces using `cv2.cvtColor()`.
|
| 1307 |
+
- Common conversions include:
|
| 1308 |
+
- **BGR to RGB** (used for correct color representation in visualization)
|
| 1309 |
+
- **BGR to Grayscale** (for black-and-white processing)
|
| 1310 |
+
""")
|
| 1311 |
+
|
| 1312 |
+
st.subheader("Example: BGR to RGB Conversion")
|
| 1313 |
+
st.code("""
|
| 1314 |
+
import cv2
|
| 1315 |
+
img_bgr = cv2.imread("path_to_image.jpg") # Read image in BGR format
|
| 1316 |
+
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 1317 |
+
cv2.imshow("RGB Image", img_rgb)
|
| 1318 |
+
cv2.waitKey(0)
|
| 1319 |
+
cv2.destroyAllWindows()
|
| 1320 |
+
""", language="python")
|
| 1321 |
+
|
| 1322 |
+
st.subheader("Example: BGR to Grayscale Conversion")
|
| 1323 |
+
st.code("""
|
| 1324 |
+
import cv2
|
| 1325 |
+
img_bgr = cv2.imread("path_to_image.jpg") # Read image in BGR format
|
| 1326 |
+
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) # Convert to Grayscale
|
| 1327 |
+
cv2.imshow("Grayscale Image", img_gray)
|
| 1328 |
+
cv2.waitKey(0)
|
| 1329 |
+
cv2.destroyAllWindows()
|
| 1330 |
+
""", language="python")
|
| 1331 |
+
|
| 1332 |
+
st.image(r"image/colortogray.jpg", caption="Color Conversions: BGR to RGB and Grayscale")
|
| 1333 |
+
|
| 1334 |
+
st.markdown("---")
|
| 1335 |
+
|
| 1336 |
+
# Section: Splitting and Merging Channels
|
| 1337 |
+
st.header("2. Splitting and Merging Channels in OpenCV")
|
| 1338 |
+
st.write("""
|
| 1339 |
+
OpenCV allows you to split the three color channels (Blue, Green, Red) and work on them individually. You can later merge them back into an image using `cv2.merge()`.
|
| 1340 |
+
- **Splitting**: Separates the image into its B, G, and R channels.
|
| 1341 |
+
- **Merging**: Combines the B, G, and R channels back into a single image.
|
| 1342 |
+
""")
|
| 1343 |
+
|
| 1344 |
+
st.subheader("Example: Splitting Channels")
|
| 1345 |
+
st.code("""
|
| 1346 |
+
import cv2
|
| 1347 |
+
img_bgr = cv2.imread("path_to_image.jpg") # Read image
|
| 1348 |
+
b, g, r = cv2.split(img_bgr) # Split into Blue, Green, and Red channels
|
| 1349 |
+
cv2.imshow("Blue Channel", b)
|
| 1350 |
+
cv2.imshow("Green Channel", g)
|
| 1351 |
+
cv2.imshow("Red Channel", r)
|
| 1352 |
+
cv2.waitKey(0)
|
| 1353 |
+
cv2.destroyAllWindows()
|
| 1354 |
+
""", language="python")
|
| 1355 |
+
st.image(r"image/bgr.png", caption="Splitting and Merging Channels in OpenCV")
|
| 1356 |
+
|
| 1357 |
+
st.subheader("Example: Merging Channels")
|
| 1358 |
+
st.code("""
|
| 1359 |
+
import cv2
|
| 1360 |
+
img_bgr = cv2.imread("path_to_image.jpg") # Read image
|
| 1361 |
+
b, g, r = cv2.split(img_bgr) # Split channels
|
| 1362 |
+
merged_img = cv2.merge([b, g, r]) # Merge channels back
|
| 1363 |
+
cv2.imshow("Merged Image", merged_img)
|
| 1364 |
+
cv2.waitKey(0)
|
| 1365 |
+
cv2.destroyAllWindows()
|
| 1366 |
+
""", language="python")
|
| 1367 |
+
|
| 1368 |
+
st.image(r"image/original.png", caption="Splitting and Merging Channels in OpenCV")
|
| 1369 |
+
|
| 1370 |
+
st.markdown("---")
|
| 1371 |
+
|
| 1372 |
+
|
| 1373 |
+
|
| 1374 |
+
|
| 1375 |
+
|
| 1376 |
+
|
| 1377 |
+
if st.button('Video in OpenCV'):
|
| 1378 |
+
navigate_to('video')
|
| 1379 |
+
if st.button('Back to unstructured data'):
|
| 1380 |
+
navigate_to('unstructured_data')
|
| 1381 |
+
|
| 1382 |
+
if st.button('Back to Data collection'):
|
| 1383 |
+
navigate_to('data_collection')
|
| 1384 |
+
if st.button("Back to Main Page"):
|
| 1385 |
+
navigate_to("main")
|
| 1386 |
+
|
| 1387 |
+
|
| 1388 |
+
|
| 1389 |
+
|
| 1390 |
+
|
| 1391 |
+
|
| 1392 |
+
|
| 1393 |
+
|
| 1394 |
+
|
| 1395 |
+
|
| 1396 |
+
#------------------------------------------------------ Video ------------------------------------------
|
| 1397 |
+
elif st.session_state.page == "video":
|
| 1398 |
+
|
| 1399 |
+
st.title("Video Handling with OpenCV")
|
| 1400 |
+
|
| 1401 |
+
# Section 1: Introduction to Video Handling
|
| 1402 |
+
st.header("1. Introduction to Video Handling")
|
| 1403 |
+
st.write("""
|
| 1404 |
+
In OpenCV, videos are treated as a sequence of images called frames. You can process videos using the `cv2.VideoCapture()` class, which allows you to:
|
| 1405 |
+
- Read video files from your system.
|
| 1406 |
+
- Capture live video from a webcam or other video input devices.
|
| 1407 |
+
- Process each frame in the video stream individually.
|
| 1408 |
+
""")
|
| 1409 |
+
|
| 1410 |
+
#st.image("https://via.placeholder.com/600x400.png?text=Video+Frames", caption="Frames in a Video", use_container_width=True)
|
| 1411 |
+
|
| 1412 |
+
st.markdown("---")
|
| 1413 |
+
|
| 1414 |
+
|
| 1415 |
+
|
| 1416 |
+
|
| 1417 |
+
# Section 2: Reading a Video File
|
| 1418 |
+
st.header("2. Reading a Video File")
|
| 1419 |
+
st.write("""
|
| 1420 |
+
To read a video file, OpenCV uses the `cv2.VideoCapture()` function. It loads the video file and allows you to process each frame sequentially.
|
| 1421 |
+
The following example demonstrates how to:
|
| 1422 |
+
- Read frames from a video file.
|
| 1423 |
+
- Display the video in a window.
|
| 1424 |
+
""")
|
| 1425 |
+
|
| 1426 |
+
st.subheader("Example: Reading and Displaying a Video")
|
| 1427 |
+
st.code("""
|
| 1428 |
+
import cv2
|
| 1429 |
+
|
| 1430 |
+
# Open the video file
|
| 1431 |
+
video = cv2.VideoCapture("path_to_video.mp4")
|
| 1432 |
+
|
| 1433 |
+
|
| 1434 |
+
# Loop to read and display frames
|
| 1435 |
+
while True:
|
| 1436 |
+
suc, frame = video.read() # Read a frame
|
| 1437 |
+
if not suc:
|
| 1438 |
+
print("Video Ended")
|
| 1439 |
+
break
|
| 1440 |
+
|
| 1441 |
+
cv2.imshow("Video Playback", frame) # Display the frame
|
| 1442 |
+
|
| 1443 |
+
# Break loop on 'q' key press
|
| 1444 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1445 |
+
break
|
| 1446 |
+
|
| 1447 |
+
video.release() # Release the video file
|
| 1448 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 1449 |
+
""", language="python")
|
| 1450 |
+
|
| 1451 |
+
#st.image("https://via.placeholder.com/600x400.png?text=Reading+Video", caption="Reading and Displaying a Video File")
|
| 1452 |
+
|
| 1453 |
+
st.markdown("---")
|
| 1454 |
+
st.header("Understanding `cv2.waitKey()` and Key Input")
|
| 1455 |
+
st.write("""
|
| 1456 |
+
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:
|
| 1457 |
+
- **`cv2.waitKey(1)`**:
|
| 1458 |
+
- Waits for a key press for `1` millisecond.
|
| 1459 |
+
- Returns the ASCII value of the key pressed, or `-1` if no key is pressed.
|
| 1460 |
+
- **`& 255`**:
|
| 1461 |
+
- Masks the higher-order bits to ensure compatibility across systems.
|
| 1462 |
+
- Extracts only the last 8 bits (ASCII value).
|
| 1463 |
+
- **`ord('q')`**:
|
| 1464 |
+
- Provides the ASCII value of the character `'q'`.
|
| 1465 |
+
- The condition checks if the user pressed the `'q'` key to quit the program.
|
| 1466 |
+
""")
|
| 1467 |
+
|
| 1468 |
+
st.subheader("Combined Condition")
|
| 1469 |
+
st.write("""
|
| 1470 |
+
The full condition:
|
| 1471 |
+
```python
|
| 1472 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1473 |
+
break
|
| 1474 |
+
```
|
| 1475 |
+
This checks if the user pressed the `'q'` key during video processing. If true, the loop exits, and the program ends gracefully.
|
| 1476 |
+
""")
|
| 1477 |
+
st.markdown("---")
|
| 1478 |
+
|
| 1479 |
+
# Section 3: Using `cv2.VideoCapture` for Live Capture
|
| 1480 |
+
st.header("3. Capturing Video from Webcam")
|
| 1481 |
+
st.write("""
|
| 1482 |
+
The `cv2.VideoCapture()` function can also be used to capture live video from your webcam or connected camera devices.
|
| 1483 |
+
""")
|
| 1484 |
+
|
| 1485 |
+
st.subheader("Basic Example: Capturing Video from Webcam")
|
| 1486 |
+
st.code("""
|
| 1487 |
+
import cv2
|
| 1488 |
+
|
| 1489 |
+
# Open video capture (0 for primary webcam)
|
| 1490 |
+
video = cv2.VideoCapture(0)
|
| 1491 |
+
|
| 1492 |
+
# Loop to read frames
|
| 1493 |
+
while True:
|
| 1494 |
+
suc, frame = video.read() # Read a frame
|
| 1495 |
+
if not suc:
|
| 1496 |
+
break
|
| 1497 |
+
cv2.imshow("Webcam", frame) # Display the frame
|
| 1498 |
+
|
| 1499 |
+
# Break loop on 'q' key press
|
| 1500 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1501 |
+
break
|
| 1502 |
+
|
| 1503 |
+
video.release() # Release the video capture object
|
| 1504 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 1505 |
+
""", language="python")
|
| 1506 |
+
|
| 1507 |
+
#st.image("https://via.placeholder.com/600x400.png?text=Webcam+Capture", caption="Webcam Video Capture Example")
|
| 1508 |
+
|
| 1509 |
+
st.markdown("---")
|
| 1510 |
+
|
| 1511 |
+
# Section 4: Converting Video to Grayscale
|
| 1512 |
+
st.header("4. Processing Video: Converting to Grayscale")
|
| 1513 |
+
st.write("""
|
| 1514 |
+
You can process each frame of the video in real-time. The following example demonstrates how to:
|
| 1515 |
+
- Convert each frame of a video to grayscale.
|
| 1516 |
+
- Display the processed video.
|
| 1517 |
+
""")
|
| 1518 |
+
|
| 1519 |
+
st.subheader("Example: Converting Video to Grayscale")
|
| 1520 |
+
st.code("""
|
| 1521 |
+
import cv2
|
| 1522 |
+
|
| 1523 |
+
# Open the video file
|
| 1524 |
+
video = cv2.VideoCapture("path_to_video.mp4")
|
| 1525 |
+
|
| 1526 |
+
# Loop to read and process frames
|
| 1527 |
+
while True:
|
| 1528 |
+
suc, frame = video.read() # Read a frame
|
| 1529 |
+
if not suc:
|
| 1530 |
+
break
|
| 1531 |
+
|
| 1532 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert frame to grayscale
|
| 1533 |
+
cv2.imshow("Grayscale Video", gray_frame) # Display the processed frame
|
| 1534 |
+
|
| 1535 |
+
# Break loop on 'q' key press
|
| 1536 |
+
if cv2.waitKey(30) & 255 == ord('q'):
|
| 1537 |
+
break
|
| 1538 |
+
|
| 1539 |
+
video.release() # Release the video file
|
| 1540 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 1541 |
+
""", language="python")
|
| 1542 |
+
|
| 1543 |
+
#st.image("https://via.placeholder.com/600x400.png?text=Grayscale+Video", caption="Grayscale Video Processing Example")
|
| 1544 |
+
|
| 1545 |
+
st.markdown("---")
|
| 1546 |
+
|
| 1547 |
+
# Section 5: Splitting Channels in a Video Frame
|
| 1548 |
+
st.header("5. Splitting Channels in a Video Frame")
|
| 1549 |
+
st.write("""
|
| 1550 |
+
You can split the three color channels (Blue, Green, and Red) from a video frame and process them individually. This example demonstrates splitting channels and displaying them separately.
|
| 1551 |
+
""")
|
| 1552 |
+
|
| 1553 |
+
st.subheader("Example: Splitting Video Frame Channels")
|
| 1554 |
+
st.code("""
|
| 1555 |
+
import cv2
|
| 1556 |
+
|
| 1557 |
+
# Open video capture
|
| 1558 |
+
video = cv2.VideoCapture("path_to_video.mp4") # Replace with 0 for webcam
|
| 1559 |
+
|
| 1560 |
+
while True:
|
| 1561 |
+
suc, frame = video.read()
|
| 1562 |
+
if not suc:
|
| 1563 |
+
break
|
| 1564 |
+
|
| 1565 |
+
# Split the frame into channels
|
| 1566 |
+
b, g, r = cv2.split(frame)
|
| 1567 |
+
|
| 1568 |
+
# Merge and display individual channels
|
| 1569 |
+
blue_img = cv2.merge([b, g*0, r*0])
|
| 1570 |
+
green_img = cv2.merge([b*0, g, r*0])
|
| 1571 |
+
red_img = cv2.merge([b*0, g*0, r])
|
| 1572 |
+
|
| 1573 |
+
cv2.imshow("Original Frame", frame)
|
| 1574 |
+
cv2.imshow("Blue Channel", blue_img)
|
| 1575 |
+
cv2.imshow("Green Channel", green_img)
|
| 1576 |
+
cv2.imshow("Red Channel", red_img)
|
| 1577 |
+
|
| 1578 |
+
# Break loop on 'q' key press
|
| 1579 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1580 |
+
break
|
| 1581 |
+
|
| 1582 |
+
video.release()
|
| 1583 |
+
cv2.destroyAllWindows()
|
| 1584 |
+
""", language="python")
|
| 1585 |
+
|
| 1586 |
+
# st.image("https://via.placeholder.com/600x400.png?text=Splitting+Video+Channels", caption="Splitting Channels from a Video Frame")
|
| 1587 |
+
|
| 1588 |
+
st.markdown("---")
|
| 1589 |
+
|
| 1590 |
+
# Section 6: Saving a Specific Frame
|
| 1591 |
+
st.header("6. Capturing and Saving a Specific Frame")
|
| 1592 |
+
st.write("""
|
| 1593 |
+
Use OpenCV to capture a specific frame from a video and save it as an image file. The following example captures a frame when the 's' key is pressed.
|
| 1594 |
+
""")
|
| 1595 |
+
|
| 1596 |
+
st.subheader("Example: Saving a Frame")
|
| 1597 |
+
st.code("""
|
| 1598 |
+
import cv2
|
| 1599 |
+
|
| 1600 |
+
video = cv2.VideoCapture("path_to_video.mp4") # Replace with 0 for webcam
|
| 1601 |
+
|
| 1602 |
+
while True:
|
| 1603 |
+
suc, frame = video.read()
|
| 1604 |
+
if not suc:
|
| 1605 |
+
break
|
| 1606 |
+
|
| 1607 |
+
cv2.imshow("Video", frame)
|
| 1608 |
+
|
| 1609 |
+
# Save frame on 's' key press
|
| 1610 |
+
if cv2.waitKey(1) & 255 == ord('s'):
|
| 1611 |
+
cv2.imwrite("captured_frame.jpg", frame)
|
| 1612 |
+
print("Frame saved as captured_frame.jpg")
|
| 1613 |
+
|
| 1614 |
+
# Break loop on 'q' key press
|
| 1615 |
+
if cv2.waitKey(1) & 255 == ord('q'):
|
| 1616 |
+
break
|
| 1617 |
+
|
| 1618 |
+
video.release()
|
| 1619 |
+
cv2.destroyAllWindows()
|
| 1620 |
+
""", language="python")
|
| 1621 |
+
|
| 1622 |
+
#st.image("https://via.placeholder.com/600x400.png?text=Capture+and+Save+Frame", caption="Capturing and Saving Frames from Video")
|
| 1623 |
+
|
| 1624 |
+
st.markdown("---")
|
| 1625 |
+
|
| 1626 |
+
|
| 1627 |
+
|
| 1628 |
+
|
| 1629 |
+
|
| 1630 |
+
|
| 1631 |
+
|
| 1632 |
+
if st.button('Image Augmentation'):
|
| 1633 |
+
navigate_to('augmentation')
|
| 1634 |
+
|
| 1635 |
+
if st.button('Opencv'):
|
| 1636 |
+
navigate_to('advopencv')
|
| 1637 |
|
| 1638 |
if st.button('Back to unstructured data'):
|
| 1639 |
navigate_to('unstructured_data')
|
|
|
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
+
|
| 1657 |
+
|
| 1658 |
+
|
| 1659 |
+
|
| 1660 |
+
|
| 1661 |
#------------------------------------------------------ blank ------------------------------------------
|
| 1662 |
+
elif st.session_state.page == "augmentation":
|
| 1663 |
+
st.title("Image Augmentation Techniques with OpenCV")
|
| 1664 |
+
|
| 1665 |
+
# Introduction to Image Augmentation
|
| 1666 |
+
st.header("What is Image Augmentation?")
|
| 1667 |
+
st.write("""
|
| 1668 |
+
Image augmentation is a technique used to artificially expand the size of a dataset by creating modified versions of images.
|
| 1669 |
+
These modifications help improve the performance and robustness of machine learning models by enabling them to generalize better to unseen data.
|
| 1670 |
+
Common augmentation techniques include:
|
| 1671 |
+
- **Scaling**: Resizing the image.
|
| 1672 |
+
- **Translation**: Shifting the image along X and Y axes.
|
| 1673 |
+
- **Shearing**: Skewing the image along an axis.
|
| 1674 |
+
- **Rotation**: Rotating the image by a specified angle.
|
| 1675 |
+
- **Cropping**: Extracting a specific region of interest from the image.
|
| 1676 |
+
|
| 1677 |
+
These techniques are widely used in computer vision tasks like object detection, image classification, and segmentation.
|
| 1678 |
+
""")
|
| 1679 |
+
|
| 1680 |
+
st.image(r"image/augmentaion.png", caption="Overview of Image Augmentation", use_container_width=True)
|
| 1681 |
+
|
| 1682 |
+
st.markdown("---")
|
| 1683 |
+
st.header("1. Scaling")
|
| 1684 |
+
st.write("""
|
| 1685 |
+
Scaling resizes the image, either enlarging or reducing it.
|
| 1686 |
+
OpenCV provides functions like `cv2.resize()` and `cv2.warpAffine()` for this purpose.
|
| 1687 |
+
Scaling is useful for preparing images for machine learning models or adjusting their resolution.
|
| 1688 |
+
""")
|
| 1689 |
+
st.image(r"image/scaling1.webp", caption="Scaling Example", use_container_width=True)
|
| 1690 |
+
st.subheader('Code:')
|
| 1691 |
+
st.code("""
|
| 1692 |
+
import cv2
|
| 1693 |
+
import numpy as np
|
| 1694 |
+
|
| 1695 |
+
sx, sy = 1.5, 1.5 # Scaling factors
|
| 1696 |
+
tx, ty = 0, 0 # Translation (no translation)
|
| 1697 |
+
s_mat = np.array([[sx, 0, tx], [0, sy, ty]], dtype=np.float32) # Scaling matrix
|
| 1698 |
+
scaled_img = cv2.warpAffine(img, s_mat, (int(1.5 * 500), int(1.5 * 333))) # Scaled image
|
| 1699 |
+
cv2.imshow("Original", img)
|
| 1700 |
+
cv2.imshow("Scaled", scaled_img)
|
| 1701 |
+
cv2.waitKey(0)
|
| 1702 |
+
cv2.destroyAllWindows()
|
| 1703 |
+
""", language="python")
|
| 1704 |
+
|
| 1705 |
+
st.markdown("---")
|
| 1706 |
+
|
| 1707 |
+
# Section 2: Translation
|
| 1708 |
+
st.header("2. Translation")
|
| 1709 |
+
st.write("""
|
| 1710 |
+
Translation shifts an image along the X and Y axes. OpenCV uses the `cv2.warpAffine()` function with a translation matrix for this transformation.
|
| 1711 |
+
It’s useful for augmenting datasets by moving objects within images.
|
| 1712 |
+
""")
|
| 1713 |
+
st.image(r"image/translation1.png", caption="Translation Example", use_container_width=True)
|
| 1714 |
+
st.subheader('Code:')
|
| 1715 |
+
st.code("""
|
| 1716 |
+
import cv2
|
| 1717 |
+
import numpy as np
|
| 1718 |
+
|
| 1719 |
+
tx, ty = 50, 50 # Shift by 50 pixels along both axes
|
| 1720 |
+
t_mat = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32) # Translation matrix
|
| 1721 |
+
translated_img = cv2.warpAffine(img, t_mat, (img.shape[1] + tx, img.shape[0] + ty))
|
| 1722 |
+
cv2.imshow("Original", img)
|
| 1723 |
+
cv2.imshow("Translated", translated_img)
|
| 1724 |
+
cv2.waitKey(0)
|
| 1725 |
+
cv2.destroyAllWindows()
|
| 1726 |
+
""", language="python")
|
| 1727 |
+
|
| 1728 |
+
st.markdown("---")
|
| 1729 |
+
|
| 1730 |
+
# Section 3: Shearing
|
| 1731 |
+
st.header("3. Shearing")
|
| 1732 |
+
st.write("""
|
| 1733 |
+
Shearing skews an image along the X or Y axis. OpenCV uses a shearing matrix with the `cv2.warpAffine()` function to apply this transformation.
|
| 1734 |
+
Shearing is less commonly used in real-world applications but can be useful for certain data augmentation tasks.
|
| 1735 |
+
""")
|
| 1736 |
+
st.image(r"image/sheering1.png", caption="Shearing Example", use_container_width=True)
|
| 1737 |
+
st.subheader('Code:')
|
| 1738 |
+
st.code("""
|
| 1739 |
+
import cv2
|
| 1740 |
+
import numpy as np
|
| 1741 |
+
|
| 1742 |
+
shx, shy = 5, 2 # Shearing factors
|
| 1743 |
+
shear_mat = np.array([[1, shx, 0], [shy, 1, 0]], dtype=np.float32) # Shearing matrix
|
| 1744 |
+
sheared_img = cv2.warpAffine(img, shear_mat, (int(3 * 500), int(3 * 333)))
|
| 1745 |
+
cv2.imshow("Original", img)
|
| 1746 |
+
cv2.imshow("Sheared", sheared_img)
|
| 1747 |
+
cv2.waitKey(0)
|
| 1748 |
+
cv2.destroyAllWindows()
|
| 1749 |
+
""", language="python")
|
| 1750 |
+
|
| 1751 |
+
st.markdown("---")
|
| 1752 |
+
|
| 1753 |
+
# Section 4: Rotation
|
| 1754 |
+
st.header("4. Rotation")
|
| 1755 |
+
st.write("""
|
| 1756 |
+
Rotation rotates an image around a specific point. OpenCV provides the `cv2.getRotationMatrix2D()` function to create a rotation matrix for use with `cv2.warpAffine()`.
|
| 1757 |
+
""")
|
| 1758 |
+
st.image(r"image/rotation1.png", caption="Rotation Example", use_container_width=True)
|
| 1759 |
+
st.subheader('Code:')
|
| 1760 |
+
st.code("""
|
| 1761 |
+
import cv2
|
| 1762 |
+
|
| 1763 |
+
center = (img.shape[1] // 2, img.shape[0] // 2) # Center of rotation
|
| 1764 |
+
angle = 90 # Rotation angle in degrees
|
| 1765 |
+
scale = 1 # Scale factor (1 means no scaling)
|
| 1766 |
+
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
|
| 1767 |
+
rotated_img = cv2.warpAffine(img, rotation_matrix, (img.shape[1], img.shape[0]))
|
| 1768 |
+
cv2.imshow("Original", img)
|
| 1769 |
+
cv2.imshow("Rotated", rotated_img)
|
| 1770 |
+
cv2.waitKey(0)
|
| 1771 |
+
cv2.destroyAllWindows()
|
| 1772 |
+
""", language="python")
|
| 1773 |
+
|
| 1774 |
+
st.markdown("---")
|
| 1775 |
+
|
| 1776 |
+
# Section 5: Cropping
|
| 1777 |
+
st.header("5. Cropping")
|
| 1778 |
+
st.write("""
|
| 1779 |
+
Cropping extracts a specific region from an image. This is a simple yet effective way to focus on specific parts of an image or reduce its size.
|
| 1780 |
+
""")
|
| 1781 |
+
st.image(r"image/cropping1.png", caption="Cropping Example", use_container_width=True)
|
| 1782 |
+
st.subheader('Code:')
|
| 1783 |
+
st.code("""
|
| 1784 |
+
import cv2
|
| 1785 |
+
|
| 1786 |
+
# Define the region to crop
|
| 1787 |
+
cropped_img = img[65:192, 187:323] # Rows: 65 to 192, Columns: 187 to 323
|
| 1788 |
+
cv2.imshow("Original", img)
|
| 1789 |
+
cv2.imshow("Cropped", cropped_img)
|
| 1790 |
+
cv2.waitKey(0)
|
| 1791 |
+
cv2.destroyAllWindows()
|
| 1792 |
+
""", language="python")
|
| 1793 |
+
|
| 1794 |
+
st.markdown("---")
|
| 1795 |
+
|
| 1796 |
+
|
| 1797 |
+
|
| 1798 |
+
|
| 1799 |
|
| 1800 |
|
| 1801 |
+
|
| 1802 |
+
if st.button('Projects'):
|
| 1803 |
+
navigate_to('projects')
|
| 1804 |
+
|
| 1805 |
+
if st.button('Opencv'):
|
| 1806 |
+
navigate_to('advopencv')
|
| 1807 |
+
|
| 1808 |
+
if st.button('Back to unstructured data'):
|
| 1809 |
+
navigate_to('unstructured_data')
|
| 1810 |
+
|
| 1811 |
+
if st.button('Back to Data collection'):
|
| 1812 |
+
navigate_to('data_collection')
|
| 1813 |
+
if st.button("Back to Main Page"):
|
| 1814 |
+
navigate_to("main")
|
| 1815 |
+
|
| 1816 |
+
|
| 1817 |
+
|
| 1818 |
+
|
| 1819 |
+
|
| 1820 |
+
|
| 1821 |
+
|
| 1822 |
+
|
| 1823 |
+
#------------------------------------------------------ Projects ------------------------------------------
|
| 1824 |
+
elif st.session_state.page == "projects":
|
| 1825 |
+
|
| 1826 |
+
st.title("My OpenCV Projects")
|
| 1827 |
+
|
| 1828 |
+
# Project 1: Don't Drink and Drive
|
| 1829 |
+
st.header("🚫 Don't Drink and Drive: A Visual Story with Python (OpenCV) 🚗🍷")
|
| 1830 |
+
st.write("""
|
| 1831 |
+
I'm thrilled to share this impactful project using Python and OpenCV: an animation video that emphasizes the critical message—**“Don’t Drink & Drive.”**
|
| 1832 |
+
|
| 1833 |
+
### 🔹 The Animation Story:
|
| 1834 |
+
- It begins with a lively bar scene 🍻, where fun quickly takes a dangerous turn.
|
| 1835 |
+
- A drunken individual makes the risky decision to drive 🚙, leading to a tragic accident 💥.
|
| 1836 |
+
- **Message**: This animation serves as a strong reminder: Safety first—never drink and drive! 💡
|
| 1837 |
+
|
| 1838 |
+
### 🛠️ Technologies Used:
|
| 1839 |
+
- `cv2.line`, `cv2.circle`, `cv2.rectangle`: Drawing roads 🛣️, characters 👥, and vehicles 🚗.
|
| 1840 |
+
- `cv2.putText`: Displaying the powerful message “Don’t Drink & Drive” ✋🚫.
|
| 1841 |
+
- `cv2.setMouseCallback`: Making the scenes interactive with mouse events 🖱️.
|
| 1842 |
+
- `cv2.imshow` & `cv2.waitKey`: Bringing the animation to life frame-by-frame 🎞️.
|
| 1843 |
+
""")
|
| 1844 |
+
|
| 1845 |
+
# Button for GitHub link
|
| 1846 |
+
if st.button('View this Animation on GitHub'):
|
| 1847 |
+
st.markdown("[View this on GitHub](https://github.com/Kaustubh102/opencv_animation_gif)", unsafe_allow_html=True)
|
| 1848 |
+
|
| 1849 |
+
st.markdown("---")
|
| 1850 |
+
|
| 1851 |
+
# Project 2: The GIF - Debugging Frustration
|
| 1852 |
+
st.header("🔹 The GIF: Debugging Frustration Captured in Motion 🐞🎶")
|
| 1853 |
+
st.write("""
|
| 1854 |
+
This project captures the frustration from coding bugs 🐞 in a fun and creative way! The GIF depicts:
|
| 1855 |
+
- A keyboard transforming into a **therapy drum 🥁**, symbolizing how we often just need to drum out the stress of debugging!
|
| 1856 |
+
|
| 1857 |
+
### 🛠️ Technologies Used:
|
| 1858 |
+
- `cv2.line`, `cv2.circle`, `cv2.rectangle`: Drawing creative elements.
|
| 1859 |
+
- `cv2.putText`: Adding text for context and humor.
|
| 1860 |
+
- `cv2.imshow` & `cv2.waitKey`: Creating the animated effect.
|
| 1861 |
+
- **Fun Twist**: Sometimes all you need is to drum out the stress! 🤯
|
| 1862 |
+
""")
|
| 1863 |
+
|
| 1864 |
+
# Button for GitHub link
|
| 1865 |
+
if st.button('View this GIF on GitHub'):
|
| 1866 |
+
st.markdown("[View this on GitHub](https://github.com/Kaustubh102/opencv_animation_gif)", unsafe_allow_html=True)
|
| 1867 |
+
|
| 1868 |
+
st.markdown("---")
|
| 1869 |
+
|
| 1870 |
+
|
| 1871 |
+
|
| 1872 |
+
|
| 1873 |
+
if st.button('Back to Semistructured data'):
|
| 1874 |
+
navigate_to('semistructured_data')
|
| 1875 |
+
|
| 1876 |
+
if st.button('Back to Data collection'):
|
| 1877 |
+
navigate_to('data_collection')
|
| 1878 |
+
if st.button("Back to Main Page"):
|
| 1879 |
+
navigate_to("main")
|
| 1880 |
+
|
| 1881 |
+
|
| 1882 |
+
|
| 1883 |
+
|
| 1884 |
+
#------------------------------------------------------ blank ------------------------------------------
|
| 1885 |
elif st.session_state.page == "blank":
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
+
if st.button('Back to Semistructured data'):
|
| 1891 |
+
navigate_to('semistructured_data')
|
| 1892 |
+
|
| 1893 |
+
if st.button('Back to Data collection'):
|
| 1894 |
+
navigate_to('data_collection')
|
| 1895 |
+
if st.button("Back to Main Page"):
|
| 1896 |
+
navigate_to("main")
|
| 1897 |
+
|
| 1898 |
|
| 1899 |
|
| 1900 |
|