samgeo2 module¶
SamGeo2
¶
The main class for segmenting geospatial data with the Segment Anything Model 2 (SAM2). See https://github.com/facebookresearch/segment-anything-2 for details.
Source code in samgeo/samgeo2.py
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 |
|
__init__(model_id='sam2-hiera-large', device=None, empty_cache=True, automatic=True, video=False, mode='eval', hydra_overrides_extra=None, apply_postprocessing=False, points_per_side=32, points_per_batch=64, pred_iou_thresh=0.8, stability_score_thresh=0.95, stability_score_offset=1.0, mask_threshold=0.0, box_nms_thresh=0.7, crop_n_layers=0, crop_nms_thresh=0.7, crop_overlap_ratio=512 / 1500, crop_n_points_downscale_factor=1, point_grids=None, min_mask_region_area=0, output_mode='binary_mask', use_m2m=False, multimask_output=False, max_hole_area=0.0, max_sprinkle_area=0.0, **kwargs)
¶
Initializes the SamGeo2 class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
str
|
The model ID to use. Can be one of the following: "sam2-hiera-tiny", "sam2-hiera-small", "sam2-hiera-base-plus", "sam2-hiera-large". Defaults to "sam2-hiera-large". |
'sam2-hiera-large'
|
device
|
Optional[str]
|
The device to use (e.g., "cpu", "cuda", "mps"). Defaults to None. |
None
|
empty_cache
|
bool
|
Whether to empty the cache. Defaults to True. |
True
|
automatic
|
bool
|
Whether to use automatic mask generation. Defaults to True. |
True
|
video
|
bool
|
Whether to use video prediction. Defaults to False. |
False
|
mode
|
str
|
The mode to use. Defaults to "eval". |
'eval'
|
hydra_overrides_extra
|
Optional[List[str]]
|
Additional Hydra overrides. Defaults to None. |
None
|
apply_postprocessing
|
bool
|
Whether to apply postprocessing. Defaults to False. |
False
|
points_per_side
|
int or None
|
The number of points to be sampled along one side of the image. The total number of points is points_per_side**2. If None, 'point_grids' must provide explicit point sampling. |
32
|
points_per_batch
|
int
|
Sets the number of points run simultaneously by the model. Higher numbers may be faster but use more GPU memory. |
64
|
pred_iou_thresh
|
float
|
A filtering threshold in [0,1], using the model's predicted mask quality. |
0.8
|
stability_score_thresh
|
float
|
A filtering threshold in [0,1], using the stability of the mask under changes to the cutoff used to binarize the model's mask predictions. |
0.95
|
stability_score_offset
|
float
|
The amount to shift the cutoff when calculated the stability score. |
1.0
|
mask_threshold
|
float
|
Threshold for binarizing the mask logits |
0.0
|
box_nms_thresh
|
float
|
The box IoU cutoff used by non-maximal suppression to filter duplicate masks. |
0.7
|
crop_n_layers
|
int
|
If >0, mask prediction will be run again on crops of the image. Sets the number of layers to run, where each layer has 2**i_layer number of image crops. |
0
|
crop_nms_thresh
|
float
|
The box IoU cutoff used by non-maximal suppression to filter duplicate masks between different crops. |
0.7
|
crop_overlap_ratio
|
float
|
Sets the degree to which crops overlap. In the first crop layer, crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. |
512 / 1500
|
crop_n_points_downscale_factor
|
int
|
The number of points-per-side sampled in layer n is scaled down by crop_n_points_downscale_factor**n. |
1
|
point_grids
|
list(ndarray) or None
|
A list over explicit grids of points used for sampling, normalized to [0,1]. The nth grid in the list is used in the nth crop layer. Exclusive with points_per_side. |
None
|
min_mask_region_area
|
int
|
If >0, postprocessing will be applied to remove disconnected regions and holes in masks with area smaller than min_mask_region_area. Requires opencv. |
0
|
output_mode
|
str
|
The form masks are returned in. Can be 'binary_mask', 'uncompressed_rle', or 'coco_rle'. 'coco_rle' requires pycocotools. For large resolutions, 'binary_mask' may consume large amounts of memory. |
'binary_mask'
|
use_m2m
|
bool
|
Whether to add a one step refinement using previous mask predictions. |
False
|
multimask_output
|
bool
|
Whether to output multimask at each point of the grid. Defaults to False. |
False
|
max_hole_area
|
int
|
If max_hole_area > 0, we fill small holes in up to the maximum area of max_hole_area in low_res_masks. |
0.0
|
max_sprinkle_area
|
int
|
If max_sprinkle_area > 0, we remove small sprinkles up to the maximum area of max_sprinkle_area in low_res_masks. |
0.0
|
**kwargs
|
Any
|
Additional keyword arguments to pass to SAM2AutomaticMaskGenerator.from_pretrained() or SAM2ImagePredictor.from_pretrained(). |
{}
|
Source code in samgeo/samgeo2.py
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 |
|
add_new_mask(inference_state, frame_idx, obj_id, mask)
¶
Add a new mask to the inference state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_state
|
Any
|
The current inference state. |
required |
frame_idx
|
int
|
The frame index. |
required |
obj_id
|
int
|
The object ID. |
required |
mask
|
ndarray
|
The mask to add. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The updated inference state. |
Source code in samgeo/samgeo2.py
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 |
|
add_new_points_or_box(inference_state, frame_idx, obj_id, points=None, labels=None, clear_old_points=True, normalize_coords=True, box=None)
¶
Add new points or a box to the inference state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_state
|
Any
|
The current inference state. |
required |
frame_idx
|
int
|
The frame index. |
required |
obj_id
|
int
|
The object ID. |
required |
points
|
Optional[ndarray]
|
The points to add. Defaults to None. |
None
|
labels
|
Optional[ndarray]
|
The labels for the points. Defaults to None. |
None
|
clear_old_points
|
bool
|
Whether to clear old points. Defaults to True. |
True
|
normalize_coords
|
bool
|
Whether to normalize the coordinates. Defaults to True. |
True
|
box
|
Optional[ndarray]
|
The bounding box to add. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The updated inference state. |
Source code in samgeo/samgeo2.py
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 |
|
generate(source, output=None, foreground=True, erosion_kernel=None, mask_multiplier=255, unique=True, min_size=0, max_size=None, **kwargs)
¶
Generate masks for the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[str, ndarray]
|
The path to the input image or the input image as a numpy array. |
required |
output
|
Optional[str]
|
The path to the output image. Defaults to None. |
None
|
foreground
|
bool
|
Whether to generate the foreground mask. Defaults to True. |
True
|
erosion_kernel
|
Optional[Tuple[int, int]]
|
The erosion kernel for filtering object masks and extract borders. Such as (3, 3) or (5, 5). Set to None to disable it. Defaults to None. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. You can use this parameter to scale the mask to a larger range, for example [0, 255]. Defaults to 255. The parameter is ignored if unique is True. |
255
|
unique
|
bool
|
Whether to assign a unique value to each object. Defaults to True. The unique value increases from 1 to the number of objects. The larger the number, the larger the object area. |
True
|
min_size
|
int
|
The minimum size of the object. Defaults to 0. |
0
|
max_size
|
int
|
The maximum size of the object. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of dictionaries containing the generated masks. |
Source code in samgeo/samgeo2.py
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 |
|
init_state(video_path, offload_video_to_cpu=False, offload_state_to_cpu=False, async_loading_frames=False)
¶
Initialize an inference state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_path
|
str
|
The path to the video file. |
required |
offload_video_to_cpu
|
bool
|
Whether to offload the video to CPU. Defaults to False. |
False
|
offload_state_to_cpu
|
bool
|
Whether to offload the state to CPU. Defaults to False. |
False
|
async_loading_frames
|
bool
|
Whether to load frames asynchronously. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The initialized inference state. |
Source code in samgeo/samgeo2.py
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 |
|
predict(point_coords=None, point_labels=None, boxes=None, mask_input=None, multimask_output=False, return_logits=False, normalize_coords=True, point_crs=None, output=None, index=None, mask_multiplier=255, dtype='float32', return_results=False, **kwargs)
¶
Predict the mask for the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point_coords
|
ndarray
|
The point coordinates. Defaults to None. |
None
|
point_labels
|
ndarray
|
The point labels. Defaults to None. |
None
|
boxes
|
list | ndarray
|
A length 4 array given a box prompt to the model, in XYXY format. |
None
|
mask_input
|
ndarray
|
A low resolution mask input to the model, typically coming from a previous prediction iteration. Has form 1xHxW, where for SAM, H=W=256. multimask_output (bool, optional): If true, the model will return three masks. For ambiguous input prompts (such as a single click), this will often produce better masks than a single prediction. If only a single mask is needed, the model's predicted quality score can be used to select the best mask. For non-ambiguous prompts, such as multiple input prompts, multimask_output=False can give better results. |
None
|
multimask_output
|
bool
|
Whether to output multimask at each point of the grid. Defaults to False. |
False
|
return_logits
|
bool
|
If true, returns un-thresholded masks logits instead of a binary mask. |
False
|
normalize_coords
|
bool
|
Whether to normalize the coordinates. Defaults to True. |
True
|
point_crs
|
str
|
The coordinate reference system (CRS) of the point prompts. |
None
|
output
|
str
|
The path to the output image. Defaults to None. |
None
|
index
|
index
|
The index of the mask to save. Defaults to None, which will save the mask with the highest score. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. |
255
|
dtype
|
dtype
|
The data type of the output image. Defaults to np.float32. |
'float32'
|
return_results
|
bool
|
Whether to return the predicted masks, scores, and logits. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray, np.ndarray]: The mask, the multimask, and the logits. |
Source code in samgeo/samgeo2.py
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 |
|
predict_batch(point_coords_batch=None, point_labels_batch=None, box_batch=None, mask_input_batch=None, multimask_output=False, return_logits=False, normalize_coords=True)
¶
Predict masks for a batch of images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point_coords_batch
|
Optional[List[ndarray]]
|
A batch of point coordinates. Defaults to None. |
None
|
point_labels_batch
|
Optional[List[ndarray]]
|
A batch of point labels. Defaults to None. |
None
|
box_batch
|
Optional[List[ndarray]]
|
A batch of bounding boxes. Defaults to None. |
None
|
mask_input_batch
|
Optional[List[ndarray]]
|
A batch of mask inputs. Defaults to None. |
None
|
multimask_output
|
bool
|
Whether to output multimask at each point of the grid. Defaults to False. |
False
|
return_logits
|
bool
|
Whether to return the logits. Defaults to False. |
False
|
normalize_coords
|
bool
|
Whether to normalize the coordinates. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Tuple[List[ndarray], List[ndarray], List[ndarray]]
|
Tuple[List[np.ndarray], List[np.ndarray], List[np.ndarray]]: Lists of masks, multimasks, and logits. |
Source code in samgeo/samgeo2.py
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 |
|
predict_by_points(point_coords_batch=None, point_labels_batch=None, box_batch=None, mask_input_batch=None, multimask_output=False, return_logits=False, normalize_coords=True, point_crs=None, output=None, index=None, unique=True, mask_multiplier=255, dtype='int32', return_results=False, **kwargs)
¶
Predict the mask for the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point_coords
|
ndarray
|
The point coordinates. Defaults to None. |
required |
point_labels
|
ndarray
|
The point labels. Defaults to None. |
required |
boxes
|
list | ndarray
|
A length 4 array given a box prompt to the model, in XYXY format. |
required |
mask_input
|
ndarray
|
A low resolution mask input to the model, typically coming from a previous prediction iteration. Has form 1xHxW, where for SAM, H=W=256. multimask_output (bool, optional): If true, the model will return three masks. For ambiguous input prompts (such as a single click), this will often produce better masks than a single prediction. If only a single mask is needed, the model's predicted quality score can be used to select the best mask. For non-ambiguous prompts, such as multiple input prompts, multimask_output=False can give better results. |
required |
multimask_output
|
bool
|
Whether to output multimask at each point of the grid. Defaults to True. |
False
|
return_logits
|
bool
|
If true, returns un-thresholded masks logits instead of a binary mask. |
False
|
normalize_coords
|
bool
|
Whether to normalize the coordinates. Defaults to True. |
True
|
point_crs
|
str
|
The coordinate reference system (CRS) of the point prompts. |
None
|
output
|
str
|
The path to the output image. Defaults to None. |
None
|
index
|
index
|
The index of the mask to save. Defaults to None, which will save the mask with the highest score. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. |
255
|
dtype
|
dtype
|
The data type of the output image. Defaults to np.int32. |
'int32'
|
return_results
|
bool
|
Whether to return the predicted masks, scores, and logits. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray, np.ndarray]: The mask, the multimask, and the logits. |
Source code in samgeo/samgeo2.py
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 |
|
predict_video(prompts=None, point_crs=None, output_dir=None, img_ext='png')
¶
Predict masks for the video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompts
|
Dict[int, Any]
|
A dictionary containing the prompts with points and labels. |
None
|
point_crs
|
Optional[str]
|
The coordinate reference system (CRS) of the point prompts. |
None
|
output_dir
|
Optional[str]
|
The directory to save the output images. Defaults to None. |
None
|
img_ext
|
str
|
The file extension for the output images. Defaults to "png". |
'png'
|
Source code in samgeo/samgeo2.py
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 |
|
propagate_in_video(inference_state, start_frame_idx=None, max_frame_num_to_track=None, reverse=False)
¶
Propagate the inference state in video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_state
|
Any
|
The current inference state. |
required |
start_frame_idx
|
Optional[int]
|
The starting frame index. Defaults to None. |
None
|
max_frame_num_to_track
|
Optional[int]
|
The maximum number of frames to track. Defaults to None. |
None
|
reverse
|
bool
|
Whether to propagate in reverse. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The propagated inference state. |
Source code in samgeo/samgeo2.py
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 |
|
propagate_in_video_preflight(inference_state)
¶
Propagate the inference state in video preflight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_state
|
Any
|
The current inference state. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The propagated inference state. |
Source code in samgeo/samgeo2.py
992 993 994 995 996 997 998 999 1000 1001 1002 |
|
raster_to_vector(raster, vector, simplify_tolerance=None, **kwargs)
¶
Convert a raster image file to a vector dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster
|
str
|
The path to the raster image. |
required |
output
|
str
|
The path to the vector file. |
required |
simplify_tolerance
|
float
|
The maximum allowed geometry displacement. The higher this value, the smaller the number of vertices in the resulting geometry. |
None
|
Source code in samgeo/samgeo2.py
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 |
|
region_groups(image, connectivity=1, min_size=10, max_size=None, threshold=None, properties=None, intensity_image=None, out_csv=None, out_vector=None, out_image=None, **kwargs)
¶
Segment regions in an image and filter them based on size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Union[str, DataArray, ndarray]
|
Input image, can be a file path, xarray DataArray, or numpy array. |
required |
connectivity
|
int
|
Connectivity for labeling. Defaults to 1 for 4-connectivity. Use 2 for 8-connectivity. |
1
|
min_size
|
int
|
Minimum size of regions to keep. Defaults to 10. |
10
|
max_size
|
Optional[int]
|
Maximum size of regions to keep. Defaults to None. |
None
|
threshold
|
Optional[int]
|
Threshold for filling holes. Defaults to None, which is equal to min_size. |
None
|
properties
|
Optional[List[str]]
|
List of properties to measure. See https://scikit-image.org/docs/stable/api/skimage.measure.html#skimage.measure.regionprops Defaults to None. |
None
|
intensity_image
|
Optional[Union[str, DataArray, ndarray]]
|
Intensity image to use for properties. Defaults to None. |
None
|
out_csv
|
Optional[str]
|
Path to save the properties as a CSV file. Defaults to None. |
None
|
out_vector
|
Optional[str]
|
Path to save the vector file. Defaults to None. |
None
|
out_image
|
Optional[str]
|
Path to save the output image. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Union[Tuple[ndarray, DataFrame], Tuple[DataArray, DataFrame]]
|
Union[Tuple[np.ndarray, pd.DataFrame], Tuple[xr.DataArray, pd.DataFrame]]: Labeled image and properties DataFrame. |
Source code in samgeo/samgeo2.py
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 |
|
reset_state(inference_state)
¶
Remove all input points or masks in all frames throughout the video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_state
|
Any
|
The current inference state. |
required |
Source code in samgeo/samgeo2.py
924 925 926 927 928 929 930 931 |
|
save_masks(output=None, foreground=True, unique=True, erosion_kernel=None, mask_multiplier=255, min_size=0, max_size=None, **kwargs)
¶
Save the masks to the output path. The output is either a binary mask or a mask of objects with unique values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
str
|
The path to the output image. Defaults to None, saving the masks to SamGeo.objects. |
None
|
foreground
|
bool
|
Whether to generate the foreground mask. Defaults to True. |
True
|
unique
|
bool
|
Whether to assign a unique value to each object. Defaults to True. |
True
|
erosion_kernel
|
tuple
|
The erosion kernel for filtering object masks and extract borders. Such as (3, 3) or (5, 5). Set to None to disable it. Defaults to None. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. You can use this parameter to scale the mask to a larger range, for example [0, 255]. Defaults to 255. |
255
|
min_size
|
int
|
The minimum size of the object. Defaults to 0. |
0
|
max_size
|
int
|
The maximum size of the object. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for common.array_to_image(). |
{}
|
Source code in samgeo/samgeo2.py
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 |
|
save_prediction(output, index=None, mask_multiplier=255, dtype='float32', vector=None, simplify_tolerance=None, **kwargs)
¶
Save the predicted mask to the output path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
str
|
The path to the output image. |
required |
index
|
Optional[int]
|
The index of the mask to save. Defaults to None, which will save the mask with the highest score. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. |
255
|
dtype
|
str
|
The data type of the output image. Defaults to "float32". |
'float32'
|
vector
|
Optional[str]
|
The path to the output vector file. Defaults to None. |
None
|
simplify_tolerance
|
Optional[float]
|
The maximum allowed geometry displacement. The higher this value, the smaller the number of vertices in the resulting geometry. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Source code in samgeo/samgeo2.py
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 |
|
save_video_segments(output_dir, img_ext='png')
¶
Save the video segments to the output directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_dir
|
str
|
The path to the output directory. |
required |
img_ext
|
str
|
The file extension for the output images. Defaults to "png". |
'png'
|
Source code in samgeo/samgeo2.py
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 |
|
save_video_segments_blended(output_dir, img_ext='png', alpha=0.6, dpi=200, frame_stride=1, output_video=None, fps=30)
¶
Save blended video segments to the output directory and optionally create a video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_dir
|
str
|
The directory to save the output images. |
required |
img_ext
|
str
|
The file extension for the output images. Defaults to "png". |
'png'
|
alpha
|
float
|
The alpha value for the blended masks. Defaults to 0.6. |
0.6
|
dpi
|
int
|
The DPI (dots per inch) for the output images. Defaults to 200. |
200
|
frame_stride
|
int
|
The stride for selecting frames to save. Defaults to 1. |
1
|
output_video
|
Optional[str]
|
The path to the output video file. Defaults to None. |
None
|
fps
|
int
|
The frames per second for the output video. Defaults to 30. |
30
|
Source code in samgeo/samgeo2.py
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 |
|
set_image(image)
¶
Set the input image as a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Union[str, ndarray, Image]
|
The input image as a path, a numpy array, or an Image. |
required |
Source code in samgeo/samgeo2.py
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 |
|
set_image_batch(image_list)
¶
Set a batch of images for prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_list
|
List[Union[ndarray, str, Image]]
|
A list of images, |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If an input image path does not exist or if the input image type is not supported. |
Source code in samgeo/samgeo2.py
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 |
|
set_video(video_path, output_dir=None, frame_rate=None, prefix='')
¶
Set the video path and parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_path
|
str
|
The path to the video file. |
required |
start_frame
|
int
|
The starting frame index. Defaults to 0. |
required |
end_frame
|
Optional[int]
|
The ending frame index. Defaults to None. |
required |
step
|
int
|
The step size. Defaults to 1. |
required |
frame_rate
|
Optional[int]
|
The frame rate. Defaults to None. |
None
|
Source code in samgeo/samgeo2.py
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 |
|
show_anns(figsize=(12, 10), axis='off', alpha=0.35, output=None, blend=True, **kwargs)
¶
Show the annotations (objects with random color) on the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
figsize
|
tuple
|
The figure size. Defaults to (12, 10). |
(12, 10)
|
axis
|
str
|
Whether to show the axis. Defaults to "off". |
'off'
|
alpha
|
float
|
The alpha value for the annotations. Defaults to 0.35. |
0.35
|
output
|
str
|
The path to the output image. Defaults to None. |
None
|
blend
|
bool
|
Whether to show the input image. Defaults to True. |
True
|
Source code in samgeo/samgeo2.py
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 |
|
show_canvas(fg_color=(0, 255, 0), bg_color=(0, 0, 255), radius=5)
¶
Show a canvas to collect foreground and background points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fg_color
|
Tuple[int, int, int]
|
The color for the foreground points. Defaults to (0, 255, 0). |
(0, 255, 0)
|
bg_color
|
Tuple[int, int, int]
|
The color for the background points. Defaults to (0, 0, 255). |
(0, 0, 255)
|
radius
|
int
|
The radius of the points. Defaults to 5. |
5
|
Returns:
Type | Description |
---|---|
Tuple[list, list]
|
Tuple[list, list]: A tuple of two lists of foreground and background points. |
Source code in samgeo/samgeo2.py
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 |
|
show_images(path=None)
¶
Show the images in the video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the images. Defaults to None. |
None
|
Source code in samgeo/samgeo2.py
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 |
|
show_map(basemap='SATELLITE', repeat_mode=True, out_dir=None, **kwargs)
¶
Show the interactive map.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
basemap
|
str
|
The basemap. It can be one of the following: SATELLITE, ROADMAP, TERRAIN, HYBRID. |
'SATELLITE'
|
repeat_mode
|
bool
|
Whether to use the repeat mode for draw control. Defaults to True. |
True
|
out_dir
|
Optional[str]
|
The path to the output directory. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The map object. |
Source code in samgeo/samgeo2.py
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
|
show_masks(figsize=(12, 10), cmap='binary_r', axis='off', foreground=True, **kwargs)
¶
Show the binary mask or the mask of objects with unique values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
figsize
|
tuple
|
The figure size. Defaults to (12, 10). |
(12, 10)
|
cmap
|
str
|
The colormap. Defaults to "binary_r". |
'binary_r'
|
axis
|
str
|
Whether to show the axis. Defaults to "off". |
'off'
|
foreground
|
bool
|
Whether to show the foreground mask only. Defaults to True. |
True
|
**kwargs
|
Any
|
Other arguments for save_masks(). |
{}
|
Source code in samgeo/samgeo2.py
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 |
|
show_prompts(prompts, frame_idx=0, mask=None, random_color=False, point_crs=None, figsize=(9, 6))
¶
Show the prompts on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompts
|
Dict[int, Any]
|
A dictionary containing the prompts with points and labels. |
required |
frame_idx
|
int
|
The frame index. Defaults to 0. |
0
|
mask
|
Any
|
The mask. Defaults to None. |
None
|
random_color
|
bool
|
Whether to use random colors for the masks. Defaults to False. |
False
|
point_crs
|
Optional[str]
|
The coordinate reference system |
None
|
figsize
|
Tuple[int, int]
|
The figure size. Defaults to (9, 6). |
(9, 6)
|
Source code in samgeo/samgeo2.py
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 |
|
tensor_to_numpy(index=None, output=None, mask_multiplier=255, dtype='uint8', save_args=None)
¶
Convert the predicted masks from tensors to numpy arrays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
Optional[int]
|
The index of the mask to save. Defaults to None, which will save the mask with the highest score. |
None
|
output
|
Optional[str]
|
The path to the output image. Defaults to None. |
None
|
mask_multiplier
|
int
|
The mask multiplier for the output mask, which is usually a binary mask [0, 1]. |
255
|
dtype
|
str
|
The data type of the output image. Defaults to "uint8". |
'uint8'
|
save_args
|
Optional[Dict[str, Any]]
|
Optional arguments for saving the output image. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Optional[ndarray]
|
Optional[np.ndarray]: The predicted mask as a numpy array, or None if output is specified. |
Source code in samgeo/samgeo2.py
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 |
|