Spaces:
Sleeping
Sleeping
File size: 113,529 Bytes
6bbbfda 8e1f75a 6bbbfda 8e1f75a 6bbbfda 8e1f75a 6bbbfda 8e1f75a 6bbbfda 8e1f75a 6bbbfda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 |
from pathlib import Path
import logging
import os
import tempfile
import uuid
from typing import Optional, List, Dict, Tuple, Union
import io
import requests
import asyncio
import numpy as np
import cloudinary
import cloudinary.uploader
import sys
# Functions for enhanced plastic detection
def detect_beach_scene(img, hsv=None):
"""
Detect if an image contains a beach or water scene.
Args:
img: OpenCV image in BGR format
hsv: Pre-computed HSV image (optional)
Returns:
Boolean indicating if beach/water is present
"""
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Check for beach sand colors
sand_mask = cv2.inRange(hsv, np.array([10, 20, 120]), np.array([40, 80, 255]))
# Check for water/ocean colors
water_mask = cv2.inRange(hsv, np.array([80, 40, 40]), np.array([140, 255, 255]))
# Check for sky blue
sky_mask = cv2.inRange(hsv, np.array([90, 30, 170]), np.array([130, 90, 255]))
# Calculate ratios
h, w = img.shape[:2]
total_pixels = h * w
sand_ratio = np.sum(sand_mask > 0) / total_pixels
water_ratio = np.sum(water_mask > 0) / total_pixels
sky_ratio = np.sum(sky_mask > 0) / total_pixels
# Return True if significant beach/water features are present
return (sand_ratio > 0.15) or (water_ratio > 0.15) or (sand_ratio + water_ratio + sky_ratio > 0.4)
def detect_plastic_bottles(img, hsv=None):
"""
Specialized detection for plastic bottles in beach/water scenes.
Args:
img: OpenCV image in BGR format
hsv: Pre-computed HSV image (optional)
Returns:
List of detected regions with bounding boxes and confidence scores
"""
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Create masks for different types of plastic bottles
clear_bottle_mask = cv2.inRange(hsv, np.array([0, 0, 120]), np.array([180, 60, 255]))
blue_bottle_mask = cv2.inRange(hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
# Combine masks
combined_mask = cv2.bitwise_or(clear_bottle_mask, blue_bottle_mask)
# Apply morphological operations to clean up mask
kernel = np.ones((5, 5), np.uint8)
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_CLOSE, kernel)
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, kernel)
# Find contours
contours, _ = cv2.findContours(combined_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter and process contours
plastic_regions = []
for contour in contours:
area = cv2.contourArea(contour)
if area < 200:
continue # Skip small regions
x, y, w, h = cv2.boundingRect(contour)
# Skip if aspect ratio doesn't match typical bottles (bottles are taller than wide)
aspect_ratio = w / h if h > 0 else 0
if not (0.2 < aspect_ratio < 0.8) and h > 30:
continue
# Get region for additional analysis
roi = img[y:y+h, x:x+w]
if roi.size == 0:
continue
# Check shape characteristics
confidence = 0.65 # Base confidence
# If shape is very bottle-like, increase confidence
if 0.25 < aspect_ratio < 0.5 and h > 50:
confidence = 0.85
plastic_regions.append({
"bbox": [x, y, x+w, y+h],
"confidence": confidence,
"class": "plastic bottle"
})
return plastic_regions
def check_for_plastic_bottle(roi, roi_hsv=None):
"""
Check if an image region contains a plastic bottle based on color and shape.
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if region likely contains a plastic bottle
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Check aspect ratio (bottles are typically taller than wide)
aspect_ratio = w / h
if not (0.2 < aspect_ratio < 0.8):
return False
# Check for clear plastic areas
clear_mask = cv2.inRange(roi_hsv, np.array([0, 0, 120]), np.array([180, 60, 255]))
clear_ratio = np.sum(clear_mask > 0) / (h * w)
# Check for blue bottle cap areas
blue_mask = cv2.inRange(roi_hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
blue_ratio = np.sum(blue_mask > 0) / (h * w)
# Check for typical bottle colors
plastic_colors_present = (clear_ratio > 0.4) or (blue_ratio > 0.1)
# Convert to grayscale for edge/shape analysis
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Look for edges that could indicate bottle shape
edges = cv2.Canny(gray, 50, 150)
# Check for vertical edges typical in bottles
vertical_edge_count = np.sum(edges > 0) / (h * w)
has_bottle_edges = vertical_edge_count > 0.05
# Combine checks
return plastic_colors_present and has_bottle_edges
def check_for_plastic_waste(roi, roi_hsv=None):
"""
Check if an image region contains plastic waste based on color and texture.
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if region likely contains plastic waste
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Check for plastic-like colors
plastic_mask = cv2.inRange(roi_hsv, np.array([0, 0, 100]), np.array([180, 100, 255]))
plastic_ratio = np.sum(plastic_mask > 0) / (h * w)
# Check for bright colors often found in plastic waste
bright_mask = cv2.inRange(roi_hsv, np.array([0, 50, 150]), np.array([180, 255, 255]))
bright_ratio = np.sum(bright_mask > 0) / (h * w)
# Convert to grayscale for texture analysis
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Calculate texture uniformity (plastics often have uniform texture)
std_dev = np.std(gray)
uniform_texture = std_dev < 40
# Apply combined criteria
is_plastic = (plastic_ratio > 0.3 or bright_ratio > 0.2) and uniform_texture
return is_plastic
def check_for_ship(roi, roi_hsv=None):
"""
Check if an image region contains a ship based on color and shape.
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if region likely contains a ship
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Ships typically have a horizontal profile
aspect_ratio = w / h
if aspect_ratio < 1.0: # If taller than wide, probably not a ship
return False
# Convert to grayscale for edge detection
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Look for strong horizontal lines (ship deck)
edges = cv2.Canny(gray, 50, 150)
# Find horizontal lines using HoughLines
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=50, minLineLength=w/4, maxLineGap=20)
horizontal_lines = 0
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Horizontal lines have angles close to 0 or 180 degrees
if angle < 20 or angle > 160:
horizontal_lines += 1
# Check for metal/ship hull colors
# Ships often have white, gray, black, or blue colors
white_mask = cv2.inRange(roi_hsv, np.array([0, 0, 150]), np.array([180, 30, 255]))
gray_mask = cv2.inRange(roi_hsv, np.array([0, 0, 50]), np.array([180, 30, 150]))
blue_mask = cv2.inRange(roi_hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
white_ratio = np.sum(white_mask > 0) / (h * w)
gray_ratio = np.sum(gray_mask > 0) / (h * w)
blue_ratio = np.sum(blue_mask > 0) / (h * w)
ship_color_present = (white_ratio + gray_ratio + blue_ratio) > 0.3
# Combine all criteria - need horizontal lines and ship colors
return horizontal_lines >= 2 and ship_color_present
def detect_general_waste(roi, roi_hsv=None):
"""
General-purpose waste detection for beach and water scenes.
Detects various types of waste including plastics, metal, glass, etc.
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Tuple of (is_waste, waste_type, confidence)
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False, None, 0.0
# Convert to grayscale for texture analysis
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Calculate texture metrics
std_dev = np.std(gray)
# Detect plastic waste
if check_for_plastic_waste(roi, roi_hsv):
return True, "plastic waste", 0.7
# Detect plastic bottles specifically
if check_for_plastic_bottle(roi, roi_hsv):
return True, "plastic bottle", 0.85
# Check for other common waste colors and textures
# Bright unnatural colors
bright_mask = cv2.inRange(roi_hsv, np.array([0, 100, 150]), np.array([180, 255, 255]))
bright_ratio = np.sum(bright_mask > 0) / (h * w)
# Metallic/reflective surfaces
metal_mask = cv2.inRange(roi_hsv, np.array([0, 0, 150]), np.array([180, 40, 220]))
metal_ratio = np.sum(metal_mask > 0) / (h * w)
# Detect regular shape with unnatural color (likely man-made)
edges = cv2.Canny(gray, 50, 150)
edge_ratio = np.sum(edges > 0) / (h * w)
has_straight_edges = False
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=50, minLineLength=20, maxLineGap=10)
if lines is not None and len(lines) > 2:
has_straight_edges = True
# If it has bright unnatural colors and straight edges, likely waste
if bright_ratio > 0.3 and has_straight_edges:
return True, "colored waste", 0.65
# If it has metallic appearance and straight edges, likely metal waste
if metal_ratio > 0.3 and has_straight_edges:
return True, "metal waste", 0.6
# If it has uniform texture and straight edges, could be general waste
if std_dev < 35 and has_straight_edges:
return True, "general waste", 0.5
# Not waste
return False, None, 0.0
# Initialize logger first
logger = logging.getLogger(__name__)
# Apply the torchvision circular import fix BEFORE any other imports
# This is critical to prevent the "torchvision::nms does not exist" error
try:
# Pre-emptively patch the _meta_registrations module to avoid the circular import
import types
sys.modules['torchvision._meta_registrations'] = types.ModuleType('torchvision._meta_registrations')
sys.modules['torchvision._meta_registrations'].__dict__['register_meta'] = lambda x: lambda y: y
# Now safely import torchvision
import torchvision
import torchvision.ops
logger.info(f"Successfully pre-patched torchvision")
except Exception as e:
logger.warning(f"Failed to pre-patch torchvision: {e}")
# Import our fallback detection module
try:
from . import fallback_detection
HAS_FALLBACK = True
logger.info("Fallback detection module loaded successfully")
except ImportError:
HAS_FALLBACK = False
logger.warning("Fallback detection module not available")
# Initialize logger first
logger = logging.getLogger(__name__)
# Configure environment variables before importing torch
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
# Only import cv2 if available - it might not be in all environments
try:
import cv2
HAS_CV2 = True
except ImportError:
HAS_CV2 = False
logger.warning("OpenCV (cv2) not available - image processing will be limited")
# First try to import torch to check compatibility
try:
import torch
HAS_TORCH = True
# Force CPU mode if needed
if not torch.cuda.is_available():
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
logger.info("CUDA not available, using CPU for inference")
# Check torch version
torch_version = torch.__version__
logger.info(f"PyTorch version: {torch_version}")
# We already imported torchvision at the top of the file
# Just log the version if available
if 'torchvision' in sys.modules:
logger.info(f"TorchVision version: {torchvision.__version__}")
except ImportError:
HAS_TORCH = False
logger.warning("PyTorch not available - YOLO detection will not work")
# Now try to import YOLO
try:
from ultralytics import YOLO
HAS_YOLO = True
logger.info("Ultralytics YOLO loaded successfully")
except ImportError:
HAS_YOLO = False
logger.warning("Ultralytics YOLO not available - object detection disabled")
# The YOLO model - will be loaded on first use
yolo_model = None
# Custom confidence thresholds
PLASTIC_BOTTLE_CONF_THRESHOLD = 0.01 # Very low threshold to catch all potential bottles
GENERAL_CONF_THRESHOLD = 0.25 # Regular threshold for other objects
# Marine pollution related classes in COCO dataset (for standard YOLOv8)
# These are the indexes we'll filter for when using the standard YOLO model
POLLUTION_RELATED_CLASSES = {
# Primary target - plastic bottles (highest priority)
39: "plastic bottle", # COCO bottle class - primary target
40: "glass bottle", # wine glass - also bottles
41: "plastic cup", # cup - similar to bottles
44: "plastic bottle", # spoon - often misclassified bottles
# Objects commonly misclassified as bottles or vice versa (high priority)
1: "possible plastic bottle", # bicycle (sometimes confused with bottles on beaches)
2: "possible plastic bottle", # car (frequently misclassified bottles on beaches)
3: "possible plastic waste", # motorcycle (can be confused with debris)
4: "possible plastic bottle", # airplane (often misidentified with debris/bottles)
5: "possible plastic bottle", # bus (large plastic items)
9: "possible plastic bottle", # traffic light (frequently misclassified bottles)
10: "possible plastic bottle", # fire hydrant (often confused with bottles)
11: "possible plastic bottle", # stop sign (confused with bottles)
13: "possible plastic bottle", # bench (often confused with beach debris)
# Vessels and maritime objects (medium-high priority)
8: "ship", # boat/ship
9: "ship", # traffic light (sometimes confused with boats)
90: "ship", # boat
37: "ship", # sports ball (confused with buoys/small boats)
# General waste and pollution categories (medium priority)
0: "general waste", # person (can be mistaken for debris at a distance)
6: "general waste", # train
7: "general waste", # truck
15: "marine animal", # bird (can be affected by pollution)
16: "marine animal", # cat
17: "marine animal", # dog
18: "marine animal", # horse
19: "marine animal", # sheep
20: "marine animal", # cow
21: "marine animal", # elephant
22: "marine animal", # bear
23: "marine animal", # zebra
24: "marine animal", # giraffe
25: "general waste", # backpack
26: "general waste", # umbrella
27: "marine debris", # backpack (often washed up on beaches)
28: "plastic waste", # umbrella (can be beach debris)
31: "plastic waste", # handbag
32: "plastic waste", # tie
33: "plastic waste", # suitcase
# Other plastic/trash items (medium-low priority)
42: "plastic waste", # fork
43: "plastic waste", # knife
45: "plastic waste", # bowl
46: "plastic waste", # banana (misidentified waste)
47: "plastic waste", # apple (misidentified waste)
48: "plastic waste", # sandwich (often packaging)
49: "plastic waste", # orange (misidentified waste)
50: "plastic waste", # broccoli
51: "plastic waste", # carrot
67: "plastic bag", # plastic bag
73: "electronic waste",# laptop
74: "electronic waste",# mouse
75: "electronic waste",# remote
76: "electronic waste",# keyboard
77: "electronic waste",# cell phone
84: "trash bin", # trash bin
86: "paper waste" # paper
}
def custom_nms(boxes, scores, iou_threshold=0.5):
"""
Custom implementation of Non-Maximum Suppression.
This is a fallback for when torchvision's NMS operator fails.
Args:
boxes: Bounding boxes in format [x1, y1, x2, y2]
scores: Confidence scores for each box
iou_threshold: IoU threshold for considering boxes as duplicates
Returns:
List of indices of boxes to keep
"""
if len(boxes) == 0:
return []
# Convert to numpy if they're torch tensors
if HAS_TORCH and isinstance(boxes, torch.Tensor):
boxes = boxes.cpu().numpy()
if HAS_TORCH and isinstance(scores, torch.Tensor):
scores = scores.cpu().numpy()
# Get coordinates and areas
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1) * (y2 - y1)
# Sort by confidence score
indices = np.argsort(scores)[::-1]
keep = []
while indices.size > 0:
# Pick the box with highest score
i = indices[0]
keep.append(i)
if indices.size == 1:
break
# Calculate IoU of the picked box with the rest
xx1 = np.maximum(x1[i], x1[indices[1:]])
yy1 = np.maximum(y1[i], y1[indices[1:]])
xx2 = np.minimum(x2[i], x2[indices[1:]])
yy2 = np.minimum(y2[i], y2[indices[1:]])
w = np.maximum(0.0, xx2 - xx1)
h = np.maximum(0.0, yy2 - yy1)
intersection = w * h
# Calculate IoU
iou = intersection / (area[i] + area[indices[1:]] - intersection)
# Keep boxes with IoU less than threshold
indices = indices[1:][iou < iou_threshold]
return keep
def initialize_yolo_model(force_cpu=False):
"""
Initialize YOLO model with appropriate settings based on environment.
Returns the model or None if initialization fails.
Args:
force_cpu: If True, will force CPU inference regardless of CUDA availability
"""
if not HAS_YOLO or not HAS_CV2:
logger.warning("Cannot initialize YOLO: dependencies missing")
return None
try:
# Set environment variables for compatibility
if force_cpu or not torch.cuda.is_available():
logger.info("Setting YOLO to use CPU mode")
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# We've already patched torchvision at the module level,
# but let's double check that the patch is still in place
if 'torchvision._meta_registrations' not in sys.modules:
logger.warning("Torchvision patch not found, reapplying...")
try:
import types
sys.modules['torchvision._meta_registrations'] = types.ModuleType('torchvision._meta_registrations')
sys.modules['torchvision._meta_registrations'].__dict__['register_meta'] = lambda x: lambda y: y
except Exception as import_err:
logger.warning(f"Failed to reapply torchvision patch: {import_err}")
# Configure PyTorch for specific versions
if HAS_TORCH and hasattr(torch, '__version__'):
torch_version = torch.__version__
# Apply fixes for known version issues
if torch_version.startswith(('1.13', '2.0', '2.1')):
logger.info(f"Applying compatibility fixes for PyTorch {torch_version}")
# Patch for torchvision::nms issue in some versions
if "PYTHONPATH" not in os.environ:
os.environ["PYTHONPATH"] = ""
# Check if custom model exists
if os.path.exists("models/marine_pollution_yolov8.pt"):
# Load with very low confidence threshold to catch all potential bottles
model = YOLO("models/marine_pollution_yolov8.pt")
logger.info("Loaded custom marine pollution YOLO model")
else:
# ALWAYS use YOLOv8x model for deployment - no fallbacks
logger.info("Using YOLOv8x (largest/most accurate model) for production deployment...")
# Only use YOLOv8x for deployment - no fallbacks to smaller models
model_name = "yolov8x.pt"
model_size = "extra large"
# Force the model to be loaded using ultralytics' auto-download
model = None
model_loaded = False
# Only try to load YOLOv8x - this is simpler and ensures we're always using the best model
try:
# Attempt to load the model if it exists or download it if not
logger.info(f"Attempting to load {model_name} ({model_size})...")
# Import YOLO
from ultralytics import YOLO
# Check if model already exists, no need to re-download
model_exists = os.path.exists(model_name)
if model_exists:
logger.info(f"Found existing {model_name}, using it without redownloading")
else:
logger.info(f"Model {model_name} not found, will download it automatically")
# Load the model - this will trigger the download only if the file doesn't exist
model = YOLO(model_name)
# Verify that the model was loaded successfully
if hasattr(model, 'model') and model.model is not None:
logger.info(f"SUCCESS! Loaded {model_name} ({model_size} model)")
model_loaded = True
else:
logger.warning(f"Model {model_name} loaded but verification failed")
except Exception as e:
logger.error(f"Failed to load YOLOv8x model: {str(e)}")
logger.error("This is critical for proper detection. Please check your internet connection and retry.")
# If model failed to load, raise an exception - we need YOLOv8x for proper detection
if not model_loaded:
error_message = "Failed to load YOLOv8x model. This is critical for proper marine pollution detection."
logger.error(error_message)
raise RuntimeError(error_message + " Please check your internet connection and try again.")
# Configure model parameters for marine pollution detection
# Optimize settings based on model size
try:
# Get model info to adjust parameters based on model size
model_type = ""
if hasattr(model, 'info'):
model_info = model.info()
# Handle different return types from model.info()
if isinstance(model_info, dict):
model_type = model_info.get('model_type', '')
elif isinstance(model_info, tuple):
# For newer versions of Ultralytics that return tuples
model_type = str(model_info[0]) if model_info and len(model_info) > 0 else ""
elif hasattr(model_info, 'model_type'):
# For object-based returns
model_type = model_info.model_type
logger.info(f"Configuring model (type: {model_type}) with optimal settings for marine pollution detection")
# Adjust confidence threshold based on model size
# Larger models are more accurate so can use lower confidence threshold
# Safely determine model type from any identifier string
model_type_str = str(model_type).lower()
if 'x' in model_type_str: # YOLOv8x (extra large)
# For the largest model, we can use very low confidence
# as it's much more accurate with fewer false positives
model.conf = 0.15
model.iou = 0.30
logger.info("Using optimized parameters for extra large model")
elif 'l' in model_type_str: # YOLOv8l (large)
model.conf = 0.18
model.iou = 0.32
logger.info("Using optimized parameters for large model")
elif 'm' in model_type: # YOLOv8m (medium)
model.conf = 0.20
model.iou = 0.35
logger.info("Using optimized parameters for medium model")
else: # YOLOv8s or YOLOv8n (small/nano)
model.conf = 0.25
model.iou = 0.40
logger.info("Using optimized parameters for small model")
# Common settings for all model sizes
model.verbose = True # Enable detailed logging
model.agnostic_nms = True # Apply class-agnostic NMS for better multi-class detection
model.max_det = 150 # Increase max detections to catch more small objects
# Set fuse=True to optimize model speed without sacrificing accuracy
if hasattr(model, 'fuse'):
model.fuse = True
# Configure for classes that might be plastic debris or marine pollution
# These are COCO classes that could be marine pollution:
# 39: bottle, 41: cup, 44: spoon, 73: laptop, etc.
logger.info(f"YOLO {model_type} model configured successfully with optimized parameters")
# Print model properties to verify configuration
logger.info(f"Model configuration - confidence: {model.conf}, iou threshold: {model.iou}, max detections: {model.max_det}")
except Exception as config_err:
logger.warning(f"Could not configure YOLO parameters: {config_err} - using default settings")
# Fallback to basic configuration
try:
model.conf = 0.25
model.iou = 0.45
except:
pass
# Ensure model is in evaluation mode
try:
model.model.eval()
except Exception as e:
logger.warning(f"Could not explicitly set model to eval mode: {e}")
# Test model by running a simple inference to check for NMS errors
try:
# Create a small test image
test_img = np.zeros((100, 100, 3), dtype=np.uint8)
temp_path = tempfile.mktemp(suffix='.jpg')
cv2.imwrite(temp_path, test_img)
# Test inference
logger.info("Testing model with dummy image")
_ = model(temp_path)
os.unlink(temp_path)
logger.info("Model test successful")
except RuntimeError as e:
error_msg = str(e)
if "torchvision::nms" in error_msg:
# NMS operator error detected
logger.warning("NMS operator error detected during test. Will apply fallback solution.")
# If this was already in CPU mode and still failed, we need a different approach
if force_cpu:
logger.error("Model failed even in CPU mode. Manual implementation will be used.")
# We'll continue but use the custom NMS function instead when needed
else:
# Try again with CPU mode forced
logger.info("Retrying with CPU mode forced")
os.unlink(temp_path)
return initialize_yolo_model(force_cpu=True)
elif "Couldn't load custom C++ ops" in error_msg:
# Version incompatibility detected
logger.warning(f"PyTorch/Torchvision version incompatibility detected: {error_msg}")
os.unlink(temp_path)
logger.info("Will use fallback detection methods due to incompatible versions")
return None
else:
raise
except AttributeError as e:
# Handle torchvision circular import errors
if "has no attribute 'extension'" in str(e):
logger.warning(f"Torchvision circular import detected: {e}")
os.unlink(temp_path)
logger.info("Will use fallback detection methods")
return None
else:
raise
except Exception as e:
logger.warning(f"Model test threw exception: {e}")
os.unlink(temp_path)
return model
except Exception as e:
logger.error(f"Failed to initialize YOLO model: {str(e)}")
return None
async def detect_objects_in_image(image_url: str) -> Optional[Dict]:
"""
Detect objects in an image using YOLO model and return detection results.
If successful, returns a dictionary with detection results and annotated image URL.
If failed, returns None or falls back to color-based detection.
"""
if not HAS_CV2:
logger.warning("Object detection disabled: OpenCV not available")
return None
global yolo_model
temp_path = None
try:
# Download the image
image_data = await download_image(image_url)
if not image_data:
logger.error("Failed to download image for object detection")
return None
# Create a temporary file for the image
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
temp_path = temp_file.name
temp_file.write(image_data)
# First check if YOLO and PyTorch are available
if not HAS_YOLO or not HAS_TORCH:
logger.warning("YOLO or PyTorch not available - using fallback detection")
if HAS_FALLBACK:
logger.info("Using color-based fallback detection method")
return await run_fallback_detection(temp_path)
return None
# Load YOLO model if not already loaded
if yolo_model is None:
logger.info("Initializing YOLO model for object detection")
yolo_model = initialize_yolo_model()
if yolo_model is None:
logger.warning("Failed to initialize YOLO model - using fallback")
if HAS_FALLBACK:
logger.info("Using color-based fallback detection method")
return await run_fallback_detection(temp_path)
return None
# Run inference with error handling and potential retry
logger.info(f"Running YOLO inference on image: {temp_path}")
try:
# Try with default settings
results = yolo_model(temp_path)
except (RuntimeError, AttributeError) as e:
# Handle both NMS operator errors and torchvision circular import errors
error_msg = str(e)
logger.warning(f"YOLO inference error detected: {error_msg}")
# Check for torchvision circular import issue
if "has no attribute 'extension'" in error_msg:
logger.warning("Torchvision circular import detected - using fallback detection")
return await run_fallback_detection(temp_path)
# Check for custom C++ ops loading error (version incompatibility)
if "Couldn't load custom C++ ops" in error_msg:
logger.warning("PyTorch/Torchvision version incompatibility detected - using fallback detection")
return await run_fallback_detection(temp_path)
# Check for NMS operator error
if "torchvision::nms does not exist" in error_msg:
logger.warning("NMS operator error detected - trying workarounds")
# Try to fix circular import issues with torchvision
try:
# First try direct import to fix circular import
import torchvision.ops
import torchvision.models
try:
import torchvision.extension
except ImportError:
# Mock the extension module to avoid circular import
logger.info("Creating mock extension module for torchvision")
sys.modules['torchvision.extension'] = type('', (), {})()
except Exception as import_err:
logger.warning(f"Couldn't resolve torchvision imports: {import_err}")
# Try to reload model with forced CPU mode
try:
# Force CPU mode
# We can access yolo_model directly since it's already declared global at module level
yolo_model = None # Force model reload
yolo_model = initialize_yolo_model(force_cpu=True)
if yolo_model is None:
logger.warning("Failed to reinitialize YOLO model - using fallback detection")
return await run_fallback_detection(temp_path)
# Try inference with reloaded model
logger.info("Retrying with reloaded model in CPU mode")
results = yolo_model(temp_path)
except Exception as e2:
logger.warning(f"CPU mode fallback failed: {str(e2)} - using fallback detection")
return await run_fallback_detection(temp_path)
else:
# For any other error, use the fallback
logger.error(f"Unknown YOLO error: {error_msg} - using fallback detection")
return await run_fallback_detection(temp_path)
# Process results
detections = []
if results and len(results) > 0:
result = results[0] # Get the first result
# Convert the image to BGR (OpenCV format)
img = cv2.imread(temp_path)
if img is None:
logger.error(f"Failed to read image at {temp_path}")
return None
# Convert to HSV for additional checks
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_height, img_width = img.shape[:2]
# Check if this is a beach/water scene
is_beach_scene = detect_beach_scene(img, hsv)
is_water_scene = detect_water_scene(img, hsv)
if is_beach_scene:
logger.info("Beach scene detected - optimizing for beach plastic detection")
if is_water_scene:
logger.info("Water scene detected - optimizing for marine pollution detection")
# STEP 1: Run specialized detection routines first
specialized_detections = []
# Custom plastic bottle detection
plastic_bottle_regions = []
if is_beach_scene:
# More aggressive bottle detection for beach scenes
plastic_bottle_regions = detect_plastic_bottles_in_beach(img, hsv)
else:
# Standard bottle detection for all scenes
plastic_bottle_regions = detect_plastic_bottles(img, hsv)
# Add plastic bottle detections
if plastic_bottle_regions:
logger.info(f"Specialized detector found {len(plastic_bottle_regions)} potential plastic bottles")
# Add these detections with high confidence
for region in plastic_bottle_regions:
specialized_detections.append({
"class": "plastic bottle",
"confidence": region.get("confidence", 0.9),
"bbox": region["bbox"],
"method": "specialized_bottle_detector"
})
# Ship detection for water scenes
if is_water_scene:
ship_regions = detect_ships(img, hsv)
if ship_regions:
logger.info(f"Specialized detector found {len(ship_regions)} potential ships")
# Add these detections with high confidence
for region in ship_regions:
specialized_detections.append({
"class": "ship",
"confidence": region.get("confidence", 0.85),
"bbox": region["bbox"],
"method": "specialized_ship_detector"
})
# Add specialized detections to our main detections list
detections.extend(specialized_detections)
# STEP 2: Process YOLO detections with enhanced classification
# List of problematic classes that are often confused with plastic waste
problematic_classes = ["airplane", "car", "boat", "traffic light", "truck", "bus", "person", "bench",
"backpack", "handbag", "bottle", "cup", "bowl", "chair", "sofa", "box"]
marine_waste_classes = ["bottle", "cup", "plastic", "waste", "debris", "bag", "trash", "container",
"box", "package", "carton", "wrapper"]
ship_classes = ["boat", "ship", "yacht", "vessel", "speedboat", "sailboat", "barge", "tanker"]
# Potentially pollution-related classes from COCO dataset
pollution_coco_ids = [39, 41, 43, 44, 65, 67, 72, 73, 76] # bottle, cup, knife, spoon, remote, cellphone, etc.
# Use extremely low confidence threshold for beach/water scenes
min_confidence = 0.01 if (is_beach_scene or is_water_scene) else GENERAL_CONF_THRESHOLD
# Get all boxes from the results
logger.info(f"Processing {len(result.boxes)} YOLO detections")
# Create a list to track suspicious ROIs for detailed analysis
suspicious_regions = []
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
confidence = float(box.conf[0])
class_id = int(box.cls[0])
# Use even lower confidence threshold for bigger models
# Larger models are more accurate so we can trust lower confidence predictions
try:
if yolo_model is not None and hasattr(yolo_model, 'model') and hasattr(yolo_model.model, 'yaml'):
# Try to get model size from the model name
model_name = str(yolo_model.model.yaml.get('yaml_file', ''))
if 'yolov8x' in model_name.lower():
min_confidence = 0.003 # Accept even lower confidence detections
elif 'yolov8l' in model_name.lower():
min_confidence = 0.004
except Exception:
pass # Use default min_confidence if we can't determine model size
# Skip only extremely low confidence detections
if confidence < min_confidence:
continue
# Add location and size-based confidence boost
# Objects in certain regions are more likely to be relevant
# Calculate relative position and size
img_height, img_width = img.shape[:2]
rel_width = (x2 - x1) / img_width
rel_height = (y2 - y1) / img_height
rel_area = rel_width * rel_height
rel_y_pos = (y1 + y2) / 2 / img_height # Vertical center position
# Boost confidence for objects of appropriate size in water scenes
# Small to medium objects in the water are more likely to be floating debris
if is_water_scene and 0.01 < rel_area < 0.2:
confidence = min(0.99, confidence * 1.25) # 25% boost
# Get class name
if hasattr(result, 'names') and class_id in result.names:
class_name = result.names[class_id]
elif class_id in POLLUTION_RELATED_CLASSES:
class_name = POLLUTION_RELATED_CLASSES[class_id]
else:
class_name = f"class_{class_id}"
# Boost confidence for ships and boats in water scenes
if is_water_scene and any(ship_class in class_name.lower() for ship_class in ship_classes):
confidence = min(0.95, confidence * 1.5) # Boost confidence by 50%
# Boost confidence for waste in beach scenes
if is_beach_scene and any(waste_class in class_name.lower() for waste_class in marine_waste_classes):
confidence = min(0.95, confidence * 1.5) # Boost confidence by 50%
# MAJOR CHANGE: Extremely aggressive reclassification in beach/water scenes
# For beach/water scenes, any object detection might actually be a plastic bottle
if is_beach_scene or is_water_scene:
# Extract ROI for analysis
roi = img[max(0, y1):min(img_height, y2), max(0, x1):min(img_width, x2)]
if roi.size == 0:
continue
# Convert ROI to HSV for plastic detection
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# First check if this might be a ship in water scenes
is_ship = is_water_scene and check_for_ship(roi, roi_hsv)
# Check for plastic bottle characteristics regardless of class
is_plastic_bottle = check_for_plastic_bottle(roi, roi_hsv)
# Check object shape
object_shape = analyze_object_shape(roi)
# Check for general waste
is_waste, waste_type, waste_confidence = detect_general_waste(roi, roi_hsv)
# Hierarchical classification
if is_ship and is_water_scene:
# Reclassify to ship with high confidence
class_name = "ship"
confidence = 0.9
logger.info(f"Reclassified {class_id} as ship")
elif class_name.lower() == "airplane" or is_plastic_bottle or object_shape == "bottle-like":
# Reclassify to plastic bottle with high confidence
class_name = "plastic bottle"
confidence = 0.95
logger.info(f"Reclassified {class_id} as plastic bottle")
elif check_for_plastic_waste(roi, roi_hsv):
# Reclassify to general plastic waste
class_name = "plastic waste"
confidence = 0.85
logger.info(f"Reclassified {class_id} as general plastic waste")
elif is_waste and waste_confidence > confidence:
# Use the general waste detector result
class_name = waste_type
confidence = waste_confidence
logger.info(f"Reclassified {class_id} as {waste_type}")
# Handle class 39 (bottle) -> always plastic bottle in beach scene
if class_id == 39 or "bottle" in class_name.lower():
class_name = "plastic bottle"
confidence = 0.98 # Very high confidence
# Context-specific confidence boost for beach scenes
if "plastic" in class_name.lower():
confidence = min(0.99, confidence * 1.5) # Big confidence boost
# For non-beach scenes, still do smart processing
else:
# Extract ROI for analysis
roi = img[max(0, y1):min(img_height, y2), max(0, x1):min(img_width, x2)]
if roi.size > 0:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# Check specifically for problematic classes
if class_name.lower() in problematic_classes:
if check_for_plastic_bottle(roi, roi_hsv):
class_name = "plastic bottle"
confidence = 0.8
elif check_for_plastic_waste(roi, roi_hsv):
class_name = "plastic waste"
confidence = 0.7
# Skip if not a pollution-related class after all the checks
if not (class_name.lower() in ["plastic bottle", "plastic waste", "bottle"] or
"plastic" in class_name.lower() or
"bottle" in class_name.lower()):
continue
# Add to detections list
detections.append({
"class": class_name,
"confidence": round(confidence, 3),
"bbox": [x1, y1, x2, y2]
})
# STEP 3: Merge overlapping detections and remove duplicates
if len(detections) > 1:
detections = merge_overlapping_detections(detections)
# STEP 4: Draw all detections on the image with enhanced visualization
# Add scene information at the top of the image (much smaller text)
scene_info = []
if is_beach_scene:
scene_info.append("Beach")
if is_water_scene:
scene_info.append("Water")
# Simplified header - just scene and object count, with smaller text
scene_type = ' + '.join(scene_info) if scene_info else 'Unknown'
header_text = f"Scene: {scene_type} | Objects: {len(detections)}"
# Use a semi-transparent overlay instead of solid black
overlay = img.copy()
cv2.rectangle(overlay, (5, 5), (5 + len(header_text) * 4 + 10, 20), (0, 0, 0), -1)
cv2.addWeighted(overlay, 0.6, img, 0.4, 0, img)
# Much smaller text with thinner font
cv2.putText(img, header_text, (10, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
# Use a color mapping for different object types
color_map = {
"plastic bottle": (0, 0, 255), # Red for bottles
"plastic waste": (0, 165, 255), # Orange for general waste
"ship": (255, 0, 0), # Blue for ships
"bottle": (0, 0, 255), # Red for bottles
"waste": (0, 165, 255), # Orange for waste
"debris": (0, 165, 255) # Orange for debris
}
# Define default color and get the model type if available
default_color = (0, 255, 0) # Default green
for det in detections:
x1, y1, x2, y2 = det["bbox"]
class_name = det["class"]
confidence = det["confidence"]
method = det.get("method", "yolo")
# Get color for this detection type
color = color_map.get(class_name.lower(), default_color)
# Adjust thickness based on confidence and detection method
base_thickness = 2
if confidence > 0.7:
base_thickness += 1
if method == "specialized_bottle_detector" or method == "specialized_ship_detector":
base_thickness += 1
# Draw a semi-transparent filled rectangle for the detection area
overlay = img.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), color, -1) # Filled rectangle
cv2.addWeighted(overlay, 0.2, img, 0.8, 0, img) # 20% opacity
# Draw the border with appropriate thickness
cv2.rectangle(img, (x1, y1), (x2, y2), color, base_thickness)
# Create background for text
label = f"{class_name}: {confidence:.2f}"
(text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
cv2.rectangle(img, (x1, y1 - 25), (x1 + text_width, y1), color, -1)
# Add label with confidence and detection method
cv2.putText(img, label, (x1, y1 - 8), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# Remove duplicate detections (if plastic bottle is detected multiple ways)
if len(detections) > 1:
filtered_detections = []
boxes = []
for det in detections:
bbox = det["bbox"]
boxes.append([bbox[0], bbox[1], bbox[2], bbox[3]])
# Convert to numpy arrays for NMS
boxes = np.array(boxes).astype(np.float32)
scores = np.array([det["confidence"] for det in detections]).astype(np.float32)
try:
# Try to use torchvision NMS if available
if HAS_TORCH and hasattr(torchvision, "ops"):
try:
import torch
boxes_tensor = torch.from_numpy(boxes)
scores_tensor = torch.from_numpy(scores)
keep_indices = torchvision.ops.nms(boxes_tensor, scores_tensor, iou_threshold=0.4).cpu().numpy()
except Exception:
# Fall back to custom NMS
keep_indices = custom_nms(boxes, scores, iou_threshold=0.4)
else:
# Use custom NMS implementation
keep_indices = custom_nms(boxes, scores, iou_threshold=0.4)
# Keep only non-overlapping detections
filtered_detections = [detections[i] for i in keep_indices]
detections = filtered_detections
except Exception as e:
logger.warning(f"NMS failed: {e} - using all detections")
# Save the annotated image
annotated_image_path = f"{temp_path}_annotated.jpg"
cv2.imwrite(annotated_image_path, img)
# Upload the annotated image to Cloudinary
annotated_image_url = await upload_to_cloudinary(annotated_image_path)
# Clean up
try:
os.unlink(annotated_image_path)
except Exception as e:
logger.error(f"Failed to delete temporary annotated image: {e}")
# Record scene type in the response
scene_type = None
if is_beach_scene and is_water_scene:
scene_type = "coastal"
elif is_beach_scene:
scene_type = "beach"
elif is_water_scene:
scene_type = "water"
# Add method information to each detection
for det in detections:
if "method" not in det:
det["method"] = "yolo"
# Get model information for the response
model_info = {}
if yolo_model is not None:
try:
# Handle different return types from model.info()
info_result = yolo_model.info() if hasattr(yolo_model, 'info') else None
# Determine model type
model_type = "unknown"
if isinstance(info_result, dict):
model_type = info_result.get('model_type', 'unknown')
elif isinstance(info_result, tuple) and len(info_result) > 0:
# New versions return tuple: try to extract model info from tuple
model_type = str(info_result[0]) if info_result else 'unknown'
# Try to get model name from file path or model itself
model_name = "YOLOv8"
if hasattr(yolo_model, 'model') and hasattr(yolo_model.model, 'yaml'):
yaml_file = yolo_model.model.yaml.get('yaml_file', '')
if 'yolov8x' in str(yaml_file).lower():
model_name = "YOLOv8x"
elif 'yolov8l' in str(yaml_file).lower():
model_name = "YOLOv8l"
elif 'yolov8m' in str(yaml_file).lower():
model_name = "YOLOv8m"
model_info = {
"model_type": model_type,
"model_name": model_name,
"framework": "YOLOv8",
}
logger.info(f"Using {model_name} model for detection")
except Exception as e:
logger.warning(f"Could not get model info: {e}")
model_info = {"model_type": "unknown", "model_name": "YOLO", "framework": "YOLOv8"}
# Return the results with model information
return {
"detections": detections,
"annotated_image_url": annotated_image_url,
"detection_count": len(detections),
"scene_type": scene_type,
"model_info": model_info # Include model information in the response
}
return {"detections": [], "detection_count": 0, "annotated_image_url": None}
except Exception as e:
logger.error(f"Object detection failed: {e}", exc_info=True)
return None
finally:
# Clean up the temporary file
if temp_path and os.path.exists(temp_path):
try:
os.unlink(temp_path)
logger.info(f"Deleted temporary file: {temp_path}")
except Exception as e:
logger.error(f"Failed to delete temporary file: {e}")
async def download_image(url: str) -> Optional[bytes]:
"""Download an image from a URL and return its bytes"""
try:
# Use requests to download the image
response = requests.get(url, timeout=10)
response.raise_for_status()
return response.content
except Exception as e:
logger.error(f"Failed to download image: {e}")
return None
async def run_fallback_detection(image_path: str) -> Dict:
"""
Run the fallback detection when YOLO is not available or fails.
Args:
image_path: Path to the image file
Returns:
Dictionary with detection results
"""
try:
# Use the fallback detection module
if not HAS_FALLBACK:
logger.error("Fallback detection module not available")
return {"detections": [], "detection_count": 0, "annotated_image_url": None}
# Run fallback detection
results = fallback_detection.fallback_detect_objects(image_path)
# If we have a path to an annotated image, upload it
if "annotated_image_path" in results and results["annotated_image_path"]:
try:
annotated_image_url = await upload_to_cloudinary(results["annotated_image_path"])
results["annotated_image_url"] = annotated_image_url
# Clean up the temporary annotated file
os.unlink(results["annotated_image_path"])
except Exception as e:
logger.error(f"Failed to upload fallback annotated image: {str(e)}")
logger.info(f"Fallback detection found {results.get('detection_count', 0)} possible objects")
return results
except Exception as e:
logger.error(f"Fallback detection failed: {str(e)}", exc_info=True)
return {"detections": [], "detection_count": 0, "annotated_image_url": None}
def is_beach_scene(img):
"""
Detect if an image shows a beach scene (sand, water, horizon line)
Args:
img: OpenCV image in BGR format
Returns:
Boolean indicating if the image is likely a beach scene
"""
try:
# Convert to HSV for better color segmentation
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, w = img.shape[:2]
# Define color ranges for sand/beach
sand_lower = np.array([15, 20, 100])
sand_upper = np.array([35, 180, 255])
# Define color ranges for water (blue/green tones)
water_lower = np.array([80, 30, 30])
water_upper = np.array([140, 255, 255])
# Create masks for sand and water
sand_mask = cv2.inRange(hsv, sand_lower, sand_upper)
water_mask = cv2.inRange(hsv, water_lower, water_upper)
# Calculate the percentage of sand and water pixels
sand_ratio = np.sum(sand_mask > 0) / (h * w)
water_ratio = np.sum(water_mask > 0) / (h * w)
# Check for horizon line using edge detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Apply Hough Line Transform to detect straight horizontal lines
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=w//3, maxLineGap=20)
has_horizon = False
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Look for horizontal lines (+/- 10 degrees)
if angle < 10 or angle > 170:
# Check if it's in the middle third of the image (typical horizon position)
y_pos = (y1 + y2) / 2
if h/4 < y_pos < 3*h/4:
has_horizon = True
break
# Consider it a beach if we have significant sand or water AND
# either have both elements OR have a horizon line
return ((sand_ratio > 0.15 or water_ratio > 0.2) and
(sand_ratio + water_ratio > 0.3 or has_horizon))
except Exception as e:
logger.error(f"Error in beach scene detection: {e}")
return False
def is_water_scene(img):
"""
Detect if an image shows a water scene (ocean, lake, river)
Args:
img: OpenCV image in BGR format
Returns:
Boolean indicating if the image is likely a water scene
"""
try:
# Convert to HSV for better color segmentation
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, w = img.shape[:2]
# Define color ranges for water (blue/green tones)
blue_water_lower = np.array([80, 30, 30])
blue_water_upper = np.array([140, 255, 255])
# Define color ranges for darker water
dark_water_lower = np.array([80, 10, 10])
dark_water_upper = np.array([140, 180, 180])
# Define color ranges for greenish water
green_water_lower = np.array([40, 30, 30])
green_water_upper = np.array([90, 180, 200])
# Create masks for different water colors
blue_water_mask = cv2.inRange(hsv, blue_water_lower, blue_water_upper)
dark_water_mask = cv2.inRange(hsv, dark_water_lower, dark_water_upper)
green_water_mask = cv2.inRange(hsv, green_water_lower, green_water_upper)
# Combine masks
water_mask = cv2.bitwise_or(blue_water_mask, dark_water_mask)
water_mask = cv2.bitwise_or(water_mask, green_water_mask)
# Calculate the percentage of water pixels
water_ratio = np.sum(water_mask > 0) / (h * w)
# Check for horizon line using edge detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Apply Hough Line Transform to detect straight horizontal lines
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=w//3, maxLineGap=20)
has_horizon = False
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Look for horizontal lines (+/- 10 degrees)
if angle < 10 or angle > 170:
# Check if it's in the middle third of the image (typical horizon position)
y_pos = (y1 + y2) / 2
if h/4 < y_pos < 3*h/4:
has_horizon = True
break
# It's a water scene if significant portion is water-colored or has horizon with some water
return water_ratio > 0.3 or (water_ratio > 0.15 and has_horizon)
except Exception as e:
logger.error(f"Error in water scene detection: {e}")
return False
def analyze_object_shape(roi):
"""
Analyze the shape of an object to determine if it looks like a bottle, ship, etc.
Args:
roi: Region of interest (cropped image) in BGR format
Returns:
String indicating the likely shape category
"""
try:
# Convert to grayscale
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Apply threshold to get binary image
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# No contours found
if not contours:
return "unknown"
# Use the largest contour
contour = max(contours, key=cv2.contourArea)
# Calculate shape metrics
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
x, y, w, h = cv2.boundingRect(contour)
# Skip if area is too small
if area < 100:
return "unknown"
# Calculate aspect ratio
aspect_ratio = float(w) / h if h > 0 else 0
# Calculate circularity
circularity = 4 * np.pi * area / (perimeter * perimeter) if perimeter > 0 else 0
# Calculate extent (ratio of contour area to bounding rectangle area)
extent = float(area) / (w * h) if w * h > 0 else 0
# Identify shape based on metrics
if 0.2 < aspect_ratio < 0.7 and circularity < 0.8 and extent > 0.4:
return "bottle-like"
elif aspect_ratio > 3 and circularity < 0.3:
return "elongated" # could be floating debris
elif aspect_ratio < 0.3 and circularity < 0.3:
return "tall-thin" # could be standing bottle
elif 0.85 < circularity and extent > 0.7:
return "circular" # could be bottle cap or small debris
elif aspect_ratio > 2 and extent > 0.6:
return "ship-like" # horizontally elongated with high fill ratio
else:
return "irregular"
except Exception as e:
logger.error(f"Error in shape analysis: {e}")
return "unknown"
def check_for_plastic_bottle(roi, roi_hsv=None):
"""
Check if a region of interest contains a plastic bottle based on color and texture
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if a plastic bottle was detected
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Look for clear/translucent plastic colors (broader range)
clear_plastic_mask = cv2.inRange(
roi_hsv,
np.array([0, 0, 120]), # Lower threshold to catch more plastic
np.array([180, 80, 255]) # Higher saturation tolerance
)
clear_ratio = np.sum(clear_plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Look for blue plastic colors (common in water bottles)
blue_plastic_mask = cv2.inRange(
roi_hsv,
np.array([85, 40, 40]), # Wider blue range
np.array([135, 255, 255])
)
blue_ratio = np.sum(blue_plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Look for colored plastic (expanded colors)
colored_plastic_mask = cv2.inRange(
roi_hsv,
np.array([0, 50, 100]), # Catch any colored plastics
np.array([180, 255, 255])
)
colored_ratio = np.sum(colored_plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Look for blue plastic cap colors
blue_cap_mask = cv2.inRange(
roi_hsv,
np.array([90, 80, 80]),
np.array([140, 255, 255])
)
blue_cap_ratio = np.sum(blue_cap_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Check object shape
bottle_shape = analyze_object_shape(roi)
# Calculate aspect ratio directly (bottles are typically taller than wide)
aspect_ratio = w / h if h > 0 else 0
direct_bottle_shape = 0.1 < aspect_ratio < 0.9 # Very permissive aspect ratio
# Check for uniform texture (plastic bottles tend to have uniform regions)
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
std_dev = np.std(gray)
uniform_texture = std_dev < 60 # More permissive texture threshold
# Combination of factors to determine if it's a bottle - MUCH more permissive now
is_bottle_shape = bottle_shape in ["bottle-like", "tall-thin"] or direct_bottle_shape
has_plastic_colors = clear_ratio > 0.2 or blue_ratio > 0.2 or colored_ratio > 0.3
has_bottle_cap = blue_cap_ratio > 0.03
# More permissive combination
return (is_bottle_shape and has_plastic_colors) or \
(has_plastic_colors and has_bottle_cap) or \
(is_bottle_shape and uniform_texture and (clear_ratio > 0.1 or blue_ratio > 0.1))
def check_for_plastic_waste(roi, roi_hsv=None):
"""
Check if a region of interest contains plastic waste based on color and texture
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if plastic waste was detected
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Look for plastic-like colors - much broader range
plastic_colors_mask = cv2.inRange(
roi_hsv,
np.array([0, 0, 80]), # Lower threshold to catch more varied plastics
np.array([180, 120, 255]) # Higher saturation tolerance
)
plastic_ratio = np.sum(plastic_colors_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Look for bright colored plastics (packaging, etc.)
bright_plastic_mask = cv2.inRange(
roi_hsv,
np.array([0, 80, 120]), # More permissive for colored plastics
np.array([180, 255, 255])
)
bright_ratio = np.sum(bright_plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Check for white/gray plastic specifically
white_plastic_mask = cv2.inRange(
roi_hsv,
np.array([0, 0, 120]),
np.array([180, 50, 255])
)
white_ratio = np.sum(white_plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Get standard deviation of hue and saturation (plastics often have uniform color)
h_std = np.std(roi_hsv[:,:,0])
s_std = np.std(roi_hsv[:,:,1])
v_std = np.std(roi_hsv[:,:,2])
# Look for unnatural colors (not common in natural scenes)
# For synthetic materials like plastic waste
unnatural_mask = np.zeros_like(roi_hsv[:,:,0])
# Neon colors
neon_mask = cv2.inRange(roi_hsv, np.array([0, 150, 150]), np.array([180, 255, 255]))
unnatural_mask = cv2.bitwise_or(unnatural_mask, neon_mask)
# Light blue (uncommon in nature)
light_blue_mask = cv2.inRange(roi_hsv, np.array([90, 50, 200]), np.array([110, 150, 255]))
unnatural_mask = cv2.bitwise_or(unnatural_mask, light_blue_mask)
# Bright red/orange (uncommon in nature)
bright_red_mask = cv2.inRange(roi_hsv, np.array([0, 150, 150]), np.array([20, 255, 255]))
unnatural_mask = cv2.bitwise_or(unnatural_mask, bright_red_mask)
unnatural_ratio = np.sum(unnatural_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Convert to grayscale for edge detection
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
edge_ratio = np.sum(edges > 0) / (roi.shape[0] * roi.shape[1])
# Check if it has plastic-like colors and uniform appearance - more permissive
has_plastic_colors = plastic_ratio > 0.25 or bright_ratio > 0.2 or white_ratio > 0.3 or unnatural_ratio > 0.1
has_uniform_appearance = h_std < 45 and s_std < 70
# Additional check for man-made objects: uniform regions with defined edges
has_defined_edges = 0.01 < edge_ratio < 0.3 and v_std < 50
# More permissive criteria - any of these combinations could indicate plastic waste
return (has_plastic_colors and has_uniform_appearance) or \
(has_plastic_colors and has_defined_edges) or \
(unnatural_ratio > 0.15) or \
(white_ratio > 0.4 and edge_ratio > 0.01)
def check_for_ship(roi, roi_hsv=None):
"""
Check if a region of interest contains a ship based on shape and color
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Boolean indicating if a ship was detected
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False
# Ship needs to have enough size
if h < 20 or w < 20:
return False
# Check aspect ratio first - ships are typically wider than tall
aspect_ratio = w / h
if aspect_ratio < 1.2: # Ship must be wider than tall
return False
# Convert to grayscale for line detection
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Get edges
edges = cv2.Canny(gray, 50, 150)
# Look for horizontal lines (characteristic of ships)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=40, minLineLength=w//3, maxLineGap=10)
horizontal_lines = 0
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Count horizontal lines (stricter: +/- 5 degrees)
if angle < 5 or angle > 175:
# Only count lines with significant length
if abs(x2 - x1) > w//3:
horizontal_lines += 1
# Require more horizontal lines
if horizontal_lines < 3:
return False
# Look for ship colors (white, gray, dark)
white_mask = cv2.inRange(roi_hsv, np.array([0, 0, 180]), np.array([180, 30, 255]))
gray_mask = cv2.inRange(roi_hsv, np.array([0, 0, 80]), np.array([180, 30, 150]))
blue_mask = cv2.inRange(roi_hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
white_ratio = np.sum(white_mask > 0) / (h * w)
gray_ratio = np.sum(gray_mask > 0) / (h * w)
blue_ratio = np.sum(blue_mask > 0) / (h * w)
# Require higher color presence
ship_color_present = (white_ratio + gray_ratio + blue_ratio) > 0.4
# Check object shape
shape = analyze_object_shape(roi)
ship_shape = shape == "ship-like" # Only use ship-like, not elongated which is too broad
# Check for presence of water at the bottom of the region (ships are on water)
if h > 30:
bottom_roi = roi[int(h*2/3):h, :]
if bottom_roi.size > 0:
bottom_hsv = cv2.cvtColor(bottom_roi, cv2.COLOR_BGR2HSV)
water_mask = cv2.inRange(bottom_hsv, np.array([80, 30, 30]), np.array([150, 255, 255]))
water_ratio = np.sum(water_mask > 0) / (bottom_roi.shape[0] * bottom_roi.shape[1])
has_water = water_ratio > 0.3
else:
has_water = False
else:
has_water = False
# Combine all criteria - much more strict now
return (horizontal_lines >= 3 and ship_color_present and aspect_ratio > 1.5) or (ship_shape and ship_color_present and has_water)
async def upload_to_cloudinary(image_path: str) -> Optional[str]:
"""Upload an image to Cloudinary and return its URL"""
try:
# Check if Cloudinary is configured
from ..config import get_settings
settings = get_settings()
if not settings.cloudinary_cloud_name or not settings.cloudinary_api_key or not settings.cloudinary_api_secret:
logger.warning("Cloudinary not configured - using local storage for annotated image")
# Save to local uploads folder instead
from pathlib import Path
upload_dir = Path("app/uploads")
upload_dir.mkdir(exist_ok=True)
filename = f"{uuid.uuid4().hex}.jpg"
local_path = upload_dir / filename
import shutil
shutil.copy(image_path, local_path)
# Return a local file URL
return f"/uploads/{filename}"
# Cloudinary is configured, proceed with upload
upload_result = cloudinary.uploader.upload(
image_path,
folder="marine_guard_annotated",
resource_type="auto"
)
return upload_result["secure_url"]
except Exception as e:
logger.error(f"Failed to upload annotated image to Cloudinary: {e}")
try:
# Fallback to local storage
from pathlib import Path
upload_dir = Path("app/uploads")
upload_dir.mkdir(exist_ok=True)
filename = f"{uuid.uuid4().hex}_fallback.jpg"
local_path = upload_dir / filename
import shutil
shutil.copy(image_path, local_path)
logger.info(f"Saved annotated image locally as fallback: {local_path}")
return f"/uploads/{filename}"
except Exception as e2:
logger.error(f"Local storage fallback also failed: {e2}")
return None
# -------------------- Helper Functions for Marine Pollution Detection --------------------
def detect_beach_scene(img, hsv):
"""
Detect if the image shows a beach scene
Args:
img: OpenCV image in BGR format
hsv: HSV format of the same image
Returns:
True if beach scene detected, False otherwise
"""
# Detect sand/beach colors
sand_mask = cv2.inRange(
hsv,
np.array([15, 0, 150]), # Light sand colors - broader range
np.array([40, 80, 255])
)
# Check for presence of blue sky
sky_mask = cv2.inRange(
hsv,
np.array([90, 50, 180]), # Blue sky
np.array([130, 255, 255])
)
# Calculate ratio of sand and sky pixels
sand_ratio = np.sum(sand_mask) / (hsv.shape[0] * hsv.shape[1] * 255)
sky_ratio = np.sum(sky_mask) / (hsv.shape[0] * hsv.shape[1] * 255)
# Return True if significant sand is detected (suggesting beach)
return sand_ratio > 0.15 or (sand_ratio > 0.1 and sky_ratio > 0.2)
def detect_water_scene(img, hsv):
"""
Detect if the image shows a water body (sea, ocean, lake)
Args:
img: OpenCV image in BGR format
hsv: HSV format of the same image
Returns:
True if water scene detected, False otherwise
"""
# Detect water colors (blue/green tones)
blue_water_mask = cv2.inRange(
hsv,
np.array([80, 30, 30]), # Broader range for water colors
np.array([150, 255, 255])
)
# Define color ranges for darker water
dark_water_mask = cv2.inRange(hsv, np.array([80, 10, 10]), np.array([140, 180, 180]))
# Define color ranges for greenish water
green_water_mask = cv2.inRange(hsv, np.array([40, 30, 30]), np.array([90, 180, 200]))
# Combine masks
water_mask = cv2.bitwise_or(blue_water_mask, dark_water_mask)
water_mask = cv2.bitwise_or(water_mask, green_water_mask)
# Calculate ratio of water pixels
water_ratio = np.sum(water_mask) / (hsv.shape[0] * hsv.shape[1] * 255)
# Check for horizon line using edge detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Apply Hough Line Transform to detect straight horizontal lines
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100,
minLineLength=img.shape[1]//3, maxLineGap=20)
has_horizon = False
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Look for horizontal lines (+/- 10 degrees)
if angle < 10 or angle > 170:
# Check if it's in the middle third of the image (typical horizon position)
y_pos = (y1 + y2) / 2
if img.shape[0]/4 < y_pos < 3*img.shape[0]/4:
has_horizon = True
break
# Return True if significant water is detected or has horizon with some water
return water_ratio > 0.25 or (water_ratio > 0.15 and has_horizon)
def check_for_plastic_bottle(roi, roi_hsv=None):
"""
Check if an image region contains a plastic bottle
Args:
roi: Image region to analyze
roi_hsv: HSV version of the roi (optional)
Returns:
True if likely plastic bottle, False otherwise
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
if h == 0 or w == 0:
return False
# Check bottle aspect ratio (usually taller than wide)
aspect_ratio = w / h
# Check for transparent/translucent plastic
clear_mask = cv2.inRange(roi_hsv, np.array([0, 0, 150]), np.array([180, 60, 255]))
clear_ratio = np.sum(clear_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Check for blue plastic (common for bottles)
blue_mask = cv2.inRange(roi_hsv, np.array([90, 40, 100]), np.array([130, 255, 255]))
blue_ratio = np.sum(blue_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Check for white plastic cap or label
white_mask = cv2.inRange(roi_hsv, np.array([0, 0, 200]), np.array([180, 30, 255]))
white_ratio = np.sum(white_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Bottle-like if it has right shape and color characteristics
return ((0.2 < aspect_ratio < 0.8) and # Bottle shape
(clear_ratio > 0.3 or blue_ratio > 0.3 or white_ratio > 0.2)) # Bottle colors
def check_for_plastic_waste(roi, roi_hsv=None):
"""
Check if an image region contains plastic waste (broader than just bottles)
Args:
roi: Image region to analyze
roi_hsv: HSV version of the roi (optional)
Returns:
True if likely plastic waste, False otherwise
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# Check for common plastic colors
plastic_mask = np.zeros_like(roi_hsv[:,:,0])
# Clear/white plastic
clear_mask = cv2.inRange(roi_hsv, np.array([0, 0, 150]), np.array([180, 60, 255]))
plastic_mask = cv2.bitwise_or(plastic_mask, clear_mask)
# Blue plastic
blue_mask = cv2.inRange(roi_hsv, np.array([90, 40, 100]), np.array([130, 255, 255]))
plastic_mask = cv2.bitwise_or(plastic_mask, blue_mask)
# Green plastic
green_mask = cv2.inRange(roi_hsv, np.array([40, 40, 100]), np.array([80, 255, 255]))
plastic_mask = cv2.bitwise_or(plastic_mask, green_mask)
# Calculate ratio of plastic-like pixels
plastic_ratio = np.sum(plastic_mask) / (roi_hsv.shape[0] * roi_hsv.shape[1] * 255)
# Check if region has uniform texture (common for plastic)
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
texture_uniformity = np.std(gray)
# Return True if significant plastic-like colors and texture
return plastic_ratio > 0.4 or (plastic_ratio > 0.25 and texture_uniformity < 50)
def detect_plastic_bottles(img, hsv=None):
"""
Specialized detector for plastic bottles using color and shape analysis
Args:
img: OpenCV image in BGR format
hsv: HSV format of the same image (optional)
Returns:
List of detected plastic bottle regions with bounding boxes and confidence
"""
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Create a combined mask for common bottle colors
bottle_mask = np.zeros_like(hsv[:,:,0])
# Clear/translucent plastic
clear_mask = cv2.inRange(hsv, np.array([0, 0, 140]), np.array([180, 60, 255]))
bottle_mask = cv2.bitwise_or(bottle_mask, clear_mask)
# Blue plastic
blue_mask = cv2.inRange(hsv, np.array([90, 40, 100]), np.array([130, 255, 255]))
bottle_mask = cv2.bitwise_or(bottle_mask, blue_mask)
# Green plastic
green_mask = cv2.inRange(hsv, np.array([40, 40, 100]), np.array([80, 255, 255]))
bottle_mask = cv2.bitwise_or(bottle_mask, green_mask)
# Apply morphological operations to clean up the mask
kernel = np.ones((5, 5), np.uint8)
bottle_mask = cv2.morphologyEx(bottle_mask, cv2.MORPH_CLOSE, kernel)
bottle_mask = cv2.morphologyEx(bottle_mask, cv2.MORPH_OPEN, kernel)
# Find contours in the bottle mask
contours, _ = cv2.findContours(bottle_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter contours to find bottle-shaped objects
detections = []
for contour in contours:
area = cv2.contourArea(contour)
if area < 200: # Skip small contours
continue
# Get bounding rectangle
x, y, w, h = cv2.boundingRect(contour)
# Skip if too small
if w < 20 or h < 30:
continue
# Calculate aspect ratio
aspect_ratio = float(w) / h if h > 0 else 0
# Check if shape matches bottle profile (usually taller than wide)
if 0.2 < aspect_ratio < 0.8:
# Extract ROI for additional checks
roi = img[y:y+h, x:x+w]
roi_hsv = hsv[y:y+h, x:x+w]
# Check for bottle characteristics
if check_for_plastic_bottle(roi, roi_hsv):
detections.append({
"bbox": [x, y, x+w, y+h],
"confidence": 0.85,
"class": "plastic bottle"
})
return detections
def box_overlap(box1, box2):
"""
Calculate IoU (Intersection over Union) between two boxes
Args:
box1, box2: Boxes in format [x1, y1, x2, y2]
Returns:
IoU value between 0 and 1
"""
# Calculate intersection
x_left = max(box1[0], box2[0])
y_top = max(box1[1], box2[1])
x_right = min(box1[2], box2[2])
y_bottom = min(box1[3], box2[3])
if x_right < x_left or y_bottom < y_top:
return 0.0 # No intersection
intersection = (x_right - x_left) * (y_bottom - y_top)
# Calculate areas
box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
# Calculate IoU
union = box1_area + box2_area - intersection
return intersection / union if union > 0 else 0
def merge_overlapping_detections(detections, iou_threshold=0.5):
"""
Merge overlapping detections, keeping the one with higher confidence
Args:
detections: List of detection dictionaries
iou_threshold: Threshold for overlap detection
Returns:
List of merged detections
"""
if not detections:
return []
# Sort by confidence (descending)
sorted_detections = sorted(detections, key=lambda x: x["confidence"], reverse=True)
merged = []
for det in sorted_detections:
should_add = True
# Check if it overlaps with any detection already in merged list
for m in merged:
overlap = box_overlap(det["bbox"], m["bbox"])
# If significant overlap and same/similar class, don't add
if overlap > iou_threshold:
if ("bottle" in det["class"].lower() and "bottle" in m["class"].lower()) or \
("plastic" in det["class"].lower() and "plastic" in m["class"].lower()):
should_add = False
break
if should_add:
merged.append(det)
return merged
def analyze_object_shape(roi):
"""
Analyze the shape of an object to determine if it resembles a bottle
Args:
roi: Region of interest (image crop)
Returns:
String indicating the shape type
"""
if roi is None or roi.size == 0:
return "unknown"
# Convert to grayscale
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Apply threshold
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# If no contours found, return unknown
if not contours:
return "unknown"
# Get largest contour
largest_contour = max(contours, key=cv2.contourArea)
# Calculate aspect ratio
x, y, w, h = cv2.boundingRect(largest_contour)
aspect_ratio = w / h if h > 0 else 0
# Calculate circularity
area = cv2.contourArea(largest_contour)
perimeter = cv2.arcLength(largest_contour, True)
circularity = 4 * np.pi * area / (perimeter * perimeter) if perimeter > 0 else 0
# Bottle characteristics: typically taller than wide and not very circular
if 0.2 < aspect_ratio < 0.7 and 0.4 < circularity < 0.75:
return "bottle-like"
# Irregular plastic waste
elif circularity < 0.6:
return "irregular"
# Round objects
elif circularity > 0.8:
return "circular"
else:
return "unknown"
# Special detection functions for different object types
def detect_plastic_bottles(img, hsv=None):
"""
Specialized function to detect plastic bottles based on color and shape
Args:
img: OpenCV image in BGR format
hsv: Optional pre-computed HSV image
Returns:
List of dictionaries with bbox and confidence for detected plastic bottles
"""
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, w = img.shape[:2]
detections = []
# Apply color thresholding for typical plastic bottle colors
# 1. Clear/transparent plastic
clear_plastic_mask = cv2.inRange(hsv, np.array([0, 0, 140]), np.array([180, 70, 255]))
# 2. Blue bottle caps
blue_cap_mask = cv2.inRange(hsv, np.array([100, 100, 100]), np.array([130, 255, 255]))
# 3. Blue plastic bottles
blue_bottle_mask = cv2.inRange(hsv, np.array([90, 50, 50]), np.array([130, 255, 255]))
# Combine masks
combined_mask = cv2.bitwise_or(clear_plastic_mask, blue_cap_mask)
combined_mask = cv2.bitwise_or(combined_mask, blue_bottle_mask)
# Apply morphological operations to clean up the mask
kernel = np.ones((5, 5), np.uint8)
mask_cleaned = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, kernel)
mask_cleaned = cv2.morphologyEx(mask_cleaned, cv2.MORPH_CLOSE, kernel)
# Find contours
contours, _ = cv2.findContours(mask_cleaned, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Process contours
for contour in contours:
# Filter by size
area = cv2.contourArea(contour)
if area < (h * w * 0.005): # Skip very small objects (less than 0.5% of image)
continue
# Get bounding box
x, y, w_box, h_box = cv2.boundingRect(contour)
# Calculate aspect ratio - bottles are usually taller than wide
aspect_ratio = float(w_box) / h_box if h_box > 0 else 0
# Bottle shape criteria
is_bottle_shape = 0.2 < aspect_ratio < 0.8
# Calculate confidence based on multiple factors
confidence = 0.6 # Base confidence
# Extract ROI for more detailed analysis
roi = img[y:y+h_box, x:x+w_box]
if roi.size > 0:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# Check if ROI has bottle characteristics
if check_for_plastic_bottle(roi, roi_hsv):
confidence += 0.25
# Check for blue cap at the top of the potential bottle
top_region = roi[:max(1, h_box//4), :]
if top_region.size > 0:
top_hsv = cv2.cvtColor(top_region, cv2.COLOR_BGR2HSV)
blue_cap_mask = cv2.inRange(top_hsv, np.array([100, 100, 100]), np.array([130, 255, 255]))
blue_cap_ratio = np.sum(blue_cap_mask > 0) / (top_region.shape[0] * top_region.shape[1])
if blue_cap_ratio > 0.1:
confidence += 0.15
# Add to detections if confidence is high enough
if is_bottle_shape and confidence > 0.65:
detections.append({
"bbox": [x, y, x + w_box, y + h_box],
"confidence": min(0.98, confidence)
})
return detections
def detect_plastic_bottles_in_beach(img, hsv=None):
"""
Specialized function to detect plastic bottles in beach scenes - more aggressive
Args:
img: OpenCV image in BGR format
hsv: Optional pre-computed HSV image
Returns:
List of dictionaries with bbox and confidence for detected plastic bottles
"""
# Start with standard bottle detection
detections = detect_plastic_bottles(img, hsv)
# Use more aggressive detection for beach scenes
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, w = img.shape[:2]
# For beach scenes, we'll be extremely aggressive and look for any potential plastic
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Use adaptive thresholding to better detect plastic in variable lighting
adaptive_thresh = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
)
# Use multiple Canny edge detection settings to catch different kinds of plastic edges
edges1 = cv2.Canny(gray, 20, 100) # More sensitive
edges2 = cv2.Canny(gray, 50, 150) # Standard
edges = cv2.bitwise_or(edges1, edges2)
# Dilate edges to connect boundaries
kernel = np.ones((5, 5), np.uint8)
dilated_edges = cv2.dilate(edges, kernel, iterations=1)
# Find contours
contours, _ = cv2.findContours(dilated_edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Process contours
for contour in contours:
# Filter by size - much more permissive
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
# Only skip extremely small or extremely large objects
if area < (h * w * 0.002) or area > (h * w * 0.7):
continue
# Calculate shape metrics
if perimeter > 0:
circularity = 4 * np.pi * area / (perimeter * perimeter)
# Get bounding box
x, y, w_box, h_box = cv2.boundingRect(contour)
# Calculate aspect ratio
aspect_ratio = float(w_box) / h_box if h_box > 0 else 0
# Much more permissive bottle shape criteria
is_bottle_shape = h_box > 20 and (
# Traditional bottle shape
((0.1 < aspect_ratio < 1.2) and (circularity < 1.0)) or
# Flattened/crushed bottle
((0.5 < aspect_ratio < 2.0) and (circularity < 0.8))
)
# Continue processing even if shape doesn't match bottle - for plastic waste detection
if is_bottle_shape or (area > (h * w * 0.005)): # Process larger objects even if shape doesn't match
# Extract ROI for detailed analysis
roi = img[max(0, y-5):min(h, y+h_box+5), max(0, x-5):min(w, x+w_box+5)]
if roi.size == 0:
continue
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# Expanded color range for plastic detection
plastic_colors = [
# Clear plastic
(np.array([0, 0, 80]), np.array([180, 70, 255])),
# White/gray plastic
(np.array([0, 0, 150]), np.array([180, 40, 255])),
# Colored plastic (common in bottles)
(np.array([0, 40, 100]), np.array([180, 255, 255])),
# Blue plastic specifically (common in bottles)
(np.array([90, 50, 100]), np.array([130, 255, 255])),
]
# Check all plastic color ranges
has_plastic_colors = False
for low, high in plastic_colors:
plastic_mask = cv2.inRange(roi_hsv, low, high)
plastic_ratio = np.sum(plastic_mask > 0) / (roi.shape[0] * roi.shape[1])
if plastic_ratio > 0.15: # Lower threshold for plastic detection
has_plastic_colors = True
break
# Calculate texture metrics
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray_roi, (5, 5), 0)
std_dev = np.std(blur)
# Look for colored caps - not just blue but any bright color
has_bottle_cap = False
if h_box > 15:
# Check both top and bottom for caps (for bottles lying on their sides)
top_roi = roi[:max(1, roi.shape[0]//4), :]
bottom_roi = roi[min(roi.shape[0], roi.shape[0]*3//4):, :]
# Check both regions for bright colors that could be caps
for cap_roi in [top_roi, bottom_roi]:
if cap_roi.size > 0:
cap_hsv = cv2.cvtColor(cap_roi, cv2.COLOR_BGR2HSV)
# Check for various cap colors - blue, red, green, white
cap_masks = [
cv2.inRange(cap_hsv, np.array([90, 80, 80]), np.array([140, 255, 255])), # Blue
cv2.inRange(cap_hsv, np.array([0, 80, 80]), np.array([20, 255, 255])), # Red
cv2.inRange(cap_hsv, np.array([35, 80, 80]), np.array([85, 255, 255])), # Green
cv2.inRange(cap_hsv, np.array([0, 0, 180]), np.array([180, 40, 255])) # White
]
for cap_mask in cap_masks:
cap_ratio = np.sum(cap_mask > 0) / (cap_roi.shape[0] * cap_roi.shape[1])
if cap_ratio > 0.08: # Lower threshold for cap detection
has_bottle_cap = True
break
if has_bottle_cap:
break
# Look for plastic waste specifically
is_plastic_waste = check_for_plastic_waste(roi, roi_hsv)
# Check with our specialized bottle detector
is_bottle = check_for_plastic_bottle(roi, roi_hsv)
# Calculate confidence - much more permissive criteria
base_confidence = 0.4 # Start with a lower base confidence
if has_plastic_colors:
base_confidence += 0.15
if has_bottle_cap:
base_confidence += 0.15
if is_bottle_shape:
base_confidence += 0.15
if is_bottle:
base_confidence += 0.2
if is_plastic_waste:
base_confidence += 0.15
if std_dev < 50: # Uniform texture is common in plastic
base_confidence += 0.1
# For beach scenes, be much more aggressive with detection confidence threshold
if base_confidence > 0.5: # Lower threshold for beach scenes
# Check if this detection overlaps with existing ones
bbox = [x, y, x + w_box, y + h_box]
is_duplicate = False
for det in detections:
existing_bbox = det["bbox"]
# Calculate IoU
x1 = max(bbox[0], existing_bbox[0])
y1 = max(bbox[1], existing_bbox[1])
x2 = min(bbox[2], existing_bbox[2])
y2 = min(bbox[3], existing_bbox[3])
if x2 > x1 and y2 > y1:
intersection = (x2 - x1) * (y2 - y1)
area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
area2 = (existing_bbox[2] - existing_bbox[0]) * (existing_bbox[3] - existing_bbox[1])
union = area1 + area2 - intersection
iou = intersection / union if union > 0 else 0
if iou > 0.3: # If overlapping significantly
is_duplicate = True
# Update the existing detection if this one has higher confidence
if base_confidence > det["confidence"]:
det["confidence"] = base_confidence
break
if not is_duplicate:
detections.append({
"bbox": bbox,
"confidence": base_confidence
})
return detections
def detect_ships(img, hsv=None):
"""
Specialized function to detect ships based on color, shape and context.
Now with extremely conservative criteria to avoid false positives.
Args:
img: OpenCV image in BGR format
hsv: Optional pre-computed HSV image
Returns:
List of dictionaries with bbox and confidence for detected ships
"""
if hsv is None:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, w = img.shape[:2]
detections = []
# Return empty if the image is too small - can't reliably detect ships
if h < 100 or w < 100:
return []
# Convert to grayscale for edge detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply edge detection - more conservative parameters
edges = cv2.Canny(gray, 80, 200) # Higher thresholds
# Apply Hough Line Transform with stricter parameters
# Require longer lines (1/4 of image width) and higher threshold
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100,
minLineLength=w//4, maxLineGap=15)
# No lines found, definitely no ships
if lines is None or len(lines) < 3: # Require at least 3 lines
return []
# Count horizontal lines and their positions - be more strict about horizontality
horizontal_lines = []
for line in lines:
x1, y1, x2, y2 = line[0]
angle = abs(np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi)
# Horizontal lines (+/- 5 degrees) - stricter angle
if angle < 5 or angle > 175:
# Calculate line length
length = np.sqrt((x2 - x1)**2 + (y2 - y1)**2)
# Only include lines that are significant in length (at least 1/4 of width)
if length > w / 4:
horizontal_lines.append((x1, y1, x2, y2))
# Require more horizontal lines
if len(horizontal_lines) < 3:
# Not enough significant horizontal lines for ship detection
return []
# Find clusters of horizontal lines that might represent ships - more conservative
ship_candidates = []
for i, (x1, y1, x2, y2) in enumerate(horizontal_lines):
# Start a new candidate with this line
y_min = min(y1, y2)
y_max = max(y1, y2)
x_min = min(x1, x2)
x_max = max(x1, x2)
# Look for nearby horizontal lines
related_lines = [i]
for j, (x1_other, y1_other, x2_other, y2_other) in enumerate(horizontal_lines):
if i == j:
continue
y_min_other = min(y1_other, y2_other)
y_max_other = max(y1_other, y2_other)
# Check if this line is near our candidate (vertically)
# Use a more conservative distance threshold
vertical_distance = min(abs(y_min - y_max_other), abs(y_max - y_min_other))
if vertical_distance < h * 0.1: # Within 10% of image height
# Update bounding box
y_min = min(y_min, y_min_other)
y_max = max(y_max, y_max_other)
x_min = min(x_min, min(x1_other, x2_other))
x_max = max(x_max, max(x1_other, x2_other))
related_lines.append(j)
# Calculate bounding box aspect ratio (ships are typically wider than tall)
width = x_max - x_min
height = y_max - y_min
aspect_ratio = width / height if height > 0 else 0
# Skip if aspect ratio is not appropriate for ships
if aspect_ratio < 1.5: # More conservative
continue
# Check if there's water present at the bottom of the candidate
# Ships should be on water
if y_max < h:
water_region = img[y_max:min(h, y_max + 20), x_min:x_max]
if water_region.size > 0:
water_hsv = cv2.cvtColor(water_region, cv2.COLOR_BGR2HSV)
water_mask = cv2.inRange(water_hsv, np.array([90, 40, 40]), np.array([140, 255, 255]))
water_ratio = np.sum(water_mask > 0) / (water_region.shape[0] * water_region.shape[1])
# Skip if no water detected below the object
if water_ratio < 0.3:
continue
# Add some padding to the bounding box
y_padding = int(h * 0.03)
x_padding = int(w * 0.03)
y_min = max(0, y_min - y_padding)
y_max = min(h, y_max + y_padding)
x_min = max(0, x_min - x_padding)
x_max = min(w, x_max + x_padding)
# Only add if we have multiple related lines AND they span a significant width
if len(related_lines) >= 3 and (x_max - x_min) > w / 4:
ship_candidates.append({
"bbox": [x_min, y_min, x_max, y_max],
"related_lines": related_lines,
"aspect_ratio": aspect_ratio
})
# Further verify ship candidates - much stricter criteria
for candidate in ship_candidates:
bbox = candidate["bbox"]
x_min, y_min, x_max, y_max = bbox
# Extract ROI
roi = img[y_min:y_max, x_min:x_max]
if roi.size == 0:
continue
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# Only accept candidates with good aspect ratio
if candidate["aspect_ratio"] < 1.5:
continue
# Check if this is a large region - ships are usually significant
region_size_ratio = ((y_max - y_min) * (x_max - x_min)) / (h * w)
if region_size_ratio < 0.05: # Skip very small regions
continue
# Check for plastic bottles or waste - if found, this is likely NOT a ship
if check_for_plastic_bottle(roi, roi_hsv) or check_for_plastic_waste(roi, roi_hsv):
continue
# Finally, check if it meets stricter ship criteria
if check_for_ship(roi, roi_hsv):
# More conservative confidence scoring
confidence = 0.6 + (0.05 * min(3, len(candidate["related_lines"])))
confidence += 0.1 if candidate["aspect_ratio"] > 2 else 0 # Bonus for wide ships
# If we pass all these strict checks, it's very likely a ship
detections.append({
"bbox": bbox,
"confidence": min(0.9, confidence) # Cap confidence slightly lower
})
# Apply non-max suppression to remove overlapping detections
if len(detections) > 1:
# Extract boxes and confidences
boxes = np.array([d["bbox"] for d in detections])
confidences = np.array([d["confidence"] for d in detections])
# Convert boxes from [x1, y1, x2, y2] to [x, y, w, h]
boxes_nms = np.zeros((len(boxes), 4))
boxes_nms[:, 0] = boxes[:, 0]
boxes_nms[:, 1] = boxes[:, 1]
boxes_nms[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes_nms[:, 3] = boxes[:, 3] - boxes[:, 1]
# Apply NMS with low IoU threshold to keep distinct ships
indices = cv2.dnn.NMSBoxes(boxes_nms.tolist(), confidences.tolist(), 0.6, 0.4)
if isinstance(indices, list) and len(indices) > 0:
filtered_detections = [detections[i] for i in indices]
elif len(indices) > 0:
# OpenCV 4.x returns a 2D array
try:
filtered_detections = [detections[i[0]] for i in indices]
except:
filtered_detections = [detections[i] for i in indices.flatten()]
else:
filtered_detections = []
# Limit to a maximum of 3 ship detections per image to further reduce false positives
return filtered_detections[:3]
return detections
def detect_general_waste(roi, roi_hsv=None):
"""
General-purpose waste detection for beach and water scenes.
Detects various types of waste including plastics, metal, glass, etc.
Args:
roi: Region of interest (cropped image) in BGR format
roi_hsv: Pre-computed HSV region (optional)
Returns:
Tuple of (is_waste, waste_type, confidence)
"""
if roi_hsv is None:
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
h, w = roi.shape[:2]
# Skip invalid ROIs
if h == 0 or w == 0:
return False, None, 0.0
# Convert to grayscale for texture analysis
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Calculate texture metrics
std_dev = np.std(gray)
# Detect plastic waste
if check_for_plastic_waste(roi, roi_hsv):
return True, "plastic waste", 0.7
# Detect plastic bottles specifically
if check_for_plastic_bottle(roi, roi_hsv):
return True, "plastic bottle", 0.85
# Check for other common waste colors and textures
# Bright unnatural colors
bright_mask = cv2.inRange(roi_hsv, np.array([0, 100, 150]), np.array([180, 255, 255]))
bright_ratio = np.sum(bright_mask > 0) / (h * w)
# Metallic/reflective surfaces
metal_mask = cv2.inRange(roi_hsv, np.array([0, 0, 150]), np.array([180, 40, 220]))
metal_ratio = np.sum(metal_mask > 0) / (h * w)
# Detect regular shape with unnatural color (likely man-made)
edges = cv2.Canny(gray, 50, 150)
edge_ratio = np.sum(edges > 0) / (h * w)
has_straight_edges = False
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=50, minLineLength=20, maxLineGap=10)
if lines is not None and len(lines) > 2:
has_straight_edges = True
# If it has bright unnatural colors and straight edges, likely waste
if bright_ratio > 0.3 and has_straight_edges:
return True, "colored waste", 0.65
# If it has metallic appearance and straight edges, likely metal waste
if metal_ratio > 0.3 and has_straight_edges:
return True, "metal waste", 0.6
# If it has uniform texture and straight edges, could be general waste
if std_dev < 35 and has_straight_edges:
return True, "general waste", 0.5
# Not waste
return False, None, 0.0
# Apply one final torchvision patch to ensure we avoid the circular import issue
# This will run when the module is imported and ensure the patch is applied
try:
# Make sure torchvision._meta_registrations is properly patched
if 'torchvision._meta_registrations' not in sys.modules or not hasattr(sys.modules['torchvision._meta_registrations'], 'register_meta'):
import types
sys.modules['torchvision._meta_registrations'] = types.ModuleType('torchvision._meta_registrations')
sys.modules['torchvision._meta_registrations'].__dict__['register_meta'] = lambda x: lambda y: y
logger.info("Applied final torchvision patch")
# Apply specific patch for torchvision::nms operator issue
if HAS_TORCH:
# Check if we need to mock torch._C._dispatch_has_kernel_for_dispatch_key
if hasattr(torch, '_C') and hasattr(torch._C, '_dispatch_has_kernel_for_dispatch_key'):
original_func = torch._C._dispatch_has_kernel_for_dispatch_key
# Patch the function to handle the problematic case
def patched_dispatch_check(qualname, key):
if qualname == "torchvision::nms" and key == "Meta":
logger.info("Intercepted check for torchvision::nms Meta dispatcher")
return True
return original_func(qualname, key)
torch._C._dispatch_has_kernel_for_dispatch_key = patched_dispatch_check
logger.info("Applied torch dispatch check patch")
except Exception as e:
logger.warning(f"Final torchvision patching failed (non-critical): {e}") |