diff --git a/examples.py b/examples.py index a61dc43..cccef33 100644 --- a/examples.py +++ b/examples.py @@ -199,10 +199,10 @@ def show_detection_example(): # Bboxes creation gt_bbox_1 = BBox(10, 10, 100, 100, 0) - pred_bbox_1 = BBox(10, 10, 100, 100, 0, .8) + pred_bbox_1 = BBox(10, 10, 100, 100, 0, .8212158464) gt_bbox_2 = BBox(110, 110, 320, 280, 1) - pred_bbox_2 = BBox(70, 50, 240, 220, 1, .3) + pred_bbox_2 = BBox(70, 50, 240, 220, 1, .34844545) gt_bbox_3 = BBox(300, 300, 100, 100, 2) diff --git a/lapixdl/convert/labelbox.py b/lapixdl/convert/labelbox.py index 218465a..66b0cad 100644 --- a/lapixdl/convert/labelbox.py +++ b/lapixdl/convert/labelbox.py @@ -33,6 +33,7 @@ def __generate_coco_annotations(img_labels: List[dict], img_id: int, annotation_ 'category_id': classes.index(class_name), 'id': annotation_first_id, 'bbox': bbox, + 'area': __calculate_area(segmentation) }) annotation_first_id += 1 @@ -40,6 +41,12 @@ def __generate_coco_annotations(img_labels: List[dict], img_id: int, annotation_ return annotations +def __calculate_area(segmentation: List[float]) -> float: + x = segmentation[0:-1:2] + y = segmentation[1::2] + return 0.5*np.abs(np.dot(x, np.roll(y, 1))-np.dot(y, np.roll(x, 1))) #Shoelace + + def __generate_coco_file(lblbox_annotations: dict, img_names_to_include: Optional[List[str]] = None) -> dict: annotation_id = 1 image_id = 1 diff --git a/setup.py b/setup.py index a665631..4f168c6 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='lapixdl', packages=find_packages(exclude=['tests']), - version='0.7.8', + version='0.7.9', description='Utils for Computer Vision Deep Learning research', long_description=long_description, diff --git a/tests/convert/coco_expect.json b/tests/convert/coco_expect.json index 9799082..dfc8a81 100644 --- a/tests/convert/coco_expect.json +++ b/tests/convert/coco_expect.json @@ -194,7 +194,8 @@ 894, 81, 99 - ] + ], + "area": 6040.0 }, { "segmentation": [ @@ -218,7 +219,8 @@ 132.046, 48.648999999999994, 48.649 - ] + ], + "area": 1167.2629254999993 }, { "segmentation": [ @@ -250,7 +252,8 @@ 135.125, 50.66699999999999, 31.334000000000003 - ] + ], + "area": 1190.0186665000074 }, { "segmentation": [ @@ -286,7 +289,8 @@ 188.459, 45.333, 48.666 - ] + ], + "area": 1696.6463334999862 }, { "segmentation": [ @@ -322,7 +326,8 @@ 448.651, 53.499999999999986, 47.0 - ] + ], + "area": 2000.75 }, { "segmentation": [ @@ -358,7 +363,8 @@ 407.651, 36.50000000000001, 50.5 - ] + ], + "area": 1286.0 }, { "segmentation": [ @@ -392,7 +398,8 @@ 710.651, 58.00000000000001, 63.0 - ] + ], + "area": 2466.5 }, { "segmentation": [ @@ -424,7 +431,8 @@ 617.469, 52.0, 63.0 - ] + ], + "area": 2398.500000000233 }, { "segmentation": [ @@ -456,7 +464,8 @@ 905.469, 63.0, 63.0 - ] + ], + "area": 2525.0 }, { "segmentation": [ @@ -520,7 +529,8 @@ 820, 111, 132 - ] + ], + "area": 10005.0 }, { "segmentation": [ @@ -550,7 +560,8 @@ 721.961, 61.33299999999997, 66.66700000000003 - ] + ], + "area": 2790.1944445000263 }, { "segmentation": [ @@ -584,7 +595,8 @@ 692.628, 69.333, 89.33299999999997 - ] + ], + "area": 3528.867111000116 }, { "segmentation": [ @@ -620,7 +632,8 @@ 878.545, 48.249999999999986, 47.75 - ] + ], + "area": 1633.5625000001164 }, { "segmentation": [ @@ -726,7 +739,8 @@ 364, 50, 64 - ] + ], + "area": 2462.5 }, { "segmentation": [ @@ -892,7 +906,8 @@ 692, 54, 102 - ] + ], + "area": 4055.0 }, { "segmentation": [ @@ -1028,7 +1043,8 @@ 863, 58, 63 - ] + ], + "area": 2478.0 }, { "segmentation": [ @@ -1058,7 +1074,8 @@ 809.699, 65.334, 72.0 - ] + ], + "area": 3306.7133334998507 }, { "segmentation": [ @@ -1090,7 +1107,8 @@ 969.699, 90.66700000000003, 58.66700000000003 - ] + ], + "area": 3873.794222000055 }, { "segmentation": [ @@ -1124,7 +1142,8 @@ 795.032, 65.33399999999995, 82.66699999999992 - ] + ], + "area": 3692.4755555000156 }, { "segmentation": [ @@ -1160,7 +1179,8 @@ 977.866, 45.75, 46.5 - ] + ], + "area": 1585.47275000019 }, { "segmentation": [ @@ -1196,7 +1216,8 @@ 1071.616, 54.5, 58.5 - ] + ], + "area": 2354.75 }, { "segmentation": [ @@ -1236,7 +1257,8 @@ 1016.449, 64.5, 60.500000000000114 - ] + ], + "area": 2801.4665554994717 }, { "segmentation": [ @@ -1270,7 +1292,8 @@ 425.282, 60.0, 42.5 - ] + ], + "area": 1873.3750000004657 }, { "segmentation": [ @@ -1312,7 +1335,8 @@ 477.782, 102.0, 90.00000000000006 - ] + ], + "area": 7426.75 }, { "segmentation": [ @@ -1346,7 +1370,8 @@ 430.532, 57.0, 53.25 - ] + ], + "area": 2429.125 }, { "segmentation": [ @@ -1386,7 +1411,8 @@ 453.532, 60.49999999999994, 60.25000000000006 - ] + ], + "area": 2486.000000000233 }, { "segmentation": [ @@ -1494,7 +1520,8 @@ 827, 75, 60 - ] + ], + "area": 3337.5 }, { "segmentation": [ @@ -1594,7 +1621,8 @@ 1013, 72, 68 - ] + ], + "area": 3868.5 }, { "segmentation": [ @@ -1694,7 +1722,8 @@ 442, 68, 56 - ] + ], + "area": 2910.0 }, { "segmentation": [ @@ -1734,7 +1763,8 @@ 402.055, 40.0, 40.16699999999997 - ] + ], + "area": 1122.0042499997653 }, { "segmentation": [ @@ -1770,7 +1800,8 @@ 508.722, 40.667000000000144, 41.83299999999997 - ] + ], + "area": 1307.7857220000587 }, { "segmentation": [ @@ -1812,7 +1843,8 @@ 564.555, 44.666999999999916, 66.66700000000003 - ] + ], + "area": 2200.7718885000795 }, { "segmentation": [ @@ -1852,7 +1884,8 @@ 444.049, 68.66599999999994, 60.0 - ] + ], + "area": 3266.422888999805 }, { "segmentation": [ @@ -1902,7 +1935,8 @@ 637, 116, 152 - ] + ], + "area": 13921.0 }, { "segmentation": [ @@ -1942,7 +1976,8 @@ 386.288, 61.0, 94.0 - ] + ], + "area": 4437.249999999767 }, { "segmentation": [ @@ -1982,7 +2017,8 @@ 548.038, 69.0, 99.0 - ] + ], + "area": 4515.124999999534 }, { "segmentation": [ @@ -2018,7 +2054,8 @@ 667.871, 59.0, 43.0 - ] + ], + "area": 2164.6249999998836 }, { "segmentation": [ @@ -2070,7 +2107,8 @@ 746.56, 91.666, 70.0 - ] + ], + "area": 4596.920388999861 }, { "segmentation": [ @@ -2140,7 +2178,8 @@ 1049, 103, 107 - ] + ], + "area": 7536.5 }, { "segmentation": [ @@ -2174,7 +2213,8 @@ 248.703, 40.0, 25.99999999999997 - ] + ], + "area": 775.724278000067 }, { "segmentation": [ @@ -2210,7 +2250,8 @@ 1072.076, 45.5, 50.0 - ] + ], + "area": 1685.75 }, { "segmentation": [ @@ -2252,7 +2293,8 @@ 1125.576, 48.0, 65.5 - ] + ], + "area": 2292.3750000009313 }, { "segmentation": [ @@ -2288,7 +2330,8 @@ 115.053, 45.0, 83.0 - ] + ], + "area": 2637.5 }, { "segmentation": [ @@ -2448,7 +2491,8 @@ 254, 99, 66 - ] + ], + "area": 5246.5 }, { "segmentation": [ @@ -2548,7 +2592,8 @@ 750, 59, 62 - ] + ], + "area": 3083.0 }, { "segmentation": [ @@ -2608,7 +2653,8 @@ 71.054, 50.75, 65.0 - ] + ], + "area": 2098.2500000001164 }, { "segmentation": [ @@ -2658,7 +2704,8 @@ 58.054, 69.75, 43.5 - ] + ], + "area": 2310.906249999942 }, { "segmentation": [ @@ -2706,7 +2753,8 @@ 104.304, 51.75, 39.75 - ] + ], + "area": 1461.21875 }, { "segmentation": [ @@ -2742,7 +2790,8 @@ 2.721, 51.334, 49.333 - ] + ], + "area": 1892.6783334999927 }, { "segmentation": [ @@ -2788,7 +2837,8 @@ 7.554, 44.5, 81.0 - ] + ], + "area": 2639.124999999942 }, { "segmentation": [ @@ -2844,7 +2894,8 @@ 239.304, 50.49999999999994, 68.74999999999997 - ] + ], + "area": 2175.5625 }, { "segmentation": [ @@ -2880,7 +2931,8 @@ 569.2, 38.333, 29.666999999999916 - ] + ], + "area": 822.8626945000142 }, { "segmentation": [ @@ -3000,7 +3052,8 @@ 833, 97, 56 - ] + ], + "area": 3534.5 }, { "segmentation": [ @@ -3066,7 +3119,8 @@ 965.613, 47.00000000000001, 55.83299999999997 - ] + ], + "area": 1658.5812499998137 }, { "segmentation": [ @@ -3108,7 +3162,8 @@ 952.196, 36.25, 46.75 - ] + ], + "area": 1358.5312500002328 }, { "segmentation": [ @@ -3152,7 +3207,8 @@ 942.196, 49.50000000000003, 56.5 - ] + ], + "area": 2125.3125 }, { "segmentation": [ @@ -3196,7 +3252,8 @@ 1032.545, 50.0, 49.5 - ] + ], + "area": 1661.9999999990687 }, { "segmentation": [ @@ -3232,7 +3289,8 @@ 1140.545, 44.5, 45.5 - ] + ], + "area": 1578.9999999990687 }, { "segmentation": [ @@ -3274,7 +3332,8 @@ 811.129, 57.0, 42.75 - ] + ], + "area": 1962.4062499990687 }, { "segmentation": [ @@ -3310,7 +3369,8 @@ 912.584, 82.0, 86.0 - ] + ], + "area": 5368.0 }, { "segmentation": [ @@ -3346,7 +3406,8 @@ 1081.584, 85.0, 113.0 - ] + ], + "area": 7611.5 }, { "segmentation": [ @@ -3438,7 +3499,8 @@ 7, 66, 56 - ] + ], + "area": 2763.5 }, { "segmentation": [ @@ -3546,7 +3608,8 @@ 76, 58, 59 - ] + ], + "area": 2389.0 }, { "segmentation": [ @@ -3654,7 +3717,8 @@ 166, 65, 78 - ] + ], + "area": 3621.5 }, { "segmentation": [ @@ -3780,7 +3844,8 @@ 299, 73, 100 - ] + ], + "area": 5403.0 }, { "segmentation": [ @@ -3944,7 +4009,8 @@ 449, 86, 71 - ] + ], + "area": 4435.5 }, { "segmentation": [ @@ -4036,7 +4102,8 @@ 563, 54, 70 - ] + ], + "area": 3187.0 }, { "segmentation": [ @@ -4112,7 +4179,8 @@ 721, 56, 72 - ] + ], + "area": 3024.0 }, { "segmentation": [ @@ -4144,7 +4212,8 @@ 630.662, 61.0, 63.0 - ] + ], + "area": 2769.5 }, { "segmentation": [ @@ -4184,7 +4253,8 @@ 483.662, 72.0, 104.00000000000006 - ] + ], + "area": 5632.249999999767 }, { "segmentation": [ @@ -4222,7 +4292,8 @@ 359.162, 51.5, 80.5 - ] + ], + "area": 2920.125 }, { "segmentation": [ @@ -4278,7 +4349,8 @@ 286.662, 65.0, 55.5 - ] + ], + "area": 2166.75 }, { "segmentation": [ @@ -4324,7 +4396,8 @@ 353.162, 141.0, 99.0 - ] + ], + "area": 9227.875 }, { "segmentation": [ @@ -4390,7 +4463,8 @@ 520.495, 63.66700000000003, 68.0 - ] + ], + "area": 2645.28955549933 }, { "segmentation": [ @@ -4428,7 +4502,8 @@ 436.391, 119.33300000000008, 122.666 - ] + ], + "area": 11211.931666499935 }, { "segmentation": [ @@ -4476,7 +4551,8 @@ 1112.557, 36.5, 55.5 - ] + ], + "area": 1559.0000000009313 }, { "segmentation": [ @@ -4518,7 +4594,8 @@ 1050.057, 76.667, 84.0 - ] + ], + "area": 4897.939055500086 }, { "segmentation": [ @@ -4580,7 +4657,8 @@ 1083.962, 79.0, 67.0 - ] + ], + "area": 2730.8125 }, { "segmentation": [ @@ -4620,7 +4698,8 @@ 260.152, 132.0, 127.0 - ] + ], + "area": 11705.375 }, { "segmentation": [ @@ -4758,7 +4837,8 @@ 2, 98, 86 - ] + ], + "area": 6842.5 }, { "segmentation": [ @@ -4878,7 +4958,8 @@ 91, 95, 100 - ] + ], + "area": 6197.5 }, { "segmentation": [ @@ -5000,7 +5081,8 @@ 557, 73, 93 - ] + ], + "area": 4877.0 }, { "segmentation": [ @@ -5042,7 +5124,8 @@ 1024.362, 55.33299999999997, 67.0 - ] + ], + "area": 2911.381444499828 }, { "segmentation": [ @@ -5082,7 +5165,8 @@ 932.695, 79.0, 94.66700000000003 - ] + ], + "area": 5398.736944500357 }, { "segmentation": [ @@ -5122,7 +5206,8 @@ 938.07, 76.0, 96.99999999999989 - ] + ], + "area": 5614.75 }, { "segmentation": [ @@ -5160,7 +5245,8 @@ 646.737, 82.667, 98.0 - ] + ], + "area": 6426.891444000066 }, { "segmentation": [ @@ -5208,7 +5294,8 @@ 534.737, 64.66700000000002, 70.66600000000005 - ] + ], + "area": 3043.533111500088 }, { "segmentation": [ @@ -5240,7 +5327,8 @@ 555.403, 45.33299999999997, 45.333999999999946 - ] + ], + "area": 1628.479888999951 }, { "segmentation": [ @@ -5286,7 +5374,8 @@ 462.737, 52.667, 69.99999999999994 - ] + ], + "area": 2741.992333500064 }, { "segmentation": [ @@ -5326,7 +5415,8 @@ 91.1, 65.33299999999997, 91.334 - ] + ], + "area": 4590.017000499996 }, { "segmentation": [ @@ -5362,7 +5452,8 @@ 165.1, 54.0, 73.334 - ] + ], + "area": 3015.563444499974 }, { "segmentation": [ @@ -5398,7 +5489,8 @@ 195.1, 33.332999999999856, 70.667 - ] + ], + "area": 1654.000999999931 }, { "segmentation": [ @@ -5452,7 +5544,8 @@ 139.934, 43.832999999999856, 52.5 - ] + ], + "area": 1681.2612775000744 }, { "segmentation": [ @@ -5496,7 +5589,8 @@ 35.416, 69.25, 70.75 - ] + ], + "area": 3878.15625 }, { "segmentation": [ @@ -5532,7 +5626,8 @@ 393.416, 42.0, 40.666 - ] + ], + "area": 1239.5629445000086 }, { "segmentation": [ @@ -5566,7 +5661,8 @@ 259.082, 59.33299999999997, 70.66700000000003 - ] + ], + "area": 3282.0029999999097 }, { "segmentation": [ @@ -5604,7 +5700,8 @@ 236.416, 52.66699999999997, 84.666 - ] + ], + "area": 3252.862444499973 }, { "segmentation": [ @@ -5650,7 +5747,8 @@ 95.264, 46.66599999999994, 82.66700000000002 - ] + ], + "area": 2654.6706664999947 }, { "segmentation": [ @@ -5686,7 +5784,8 @@ 379.264, 60.5, 52.5 - ] + ], + "area": 2367.750000000233 }, { "segmentation": [ @@ -5722,7 +5821,8 @@ 337.264, 43.0, 51.0 - ] + ], + "area": 1613.125 }, { "segmentation": [ @@ -5758,7 +5858,8 @@ 359.494, 84.0, 58.66699999999997 - ] + ], + "area": 3075.996000000043 }, { "segmentation": [ @@ -5986,7 +6087,8 @@ 413, 108, 116 - ] + ], + "area": 7463.5 }, { "segmentation": [ @@ -6102,7 +6204,8 @@ 918, 73, 69 - ] + ], + "area": 3523.5 }, { "segmentation": [ @@ -6148,7 +6251,8 @@ 102.532, 52.0, 60.667000000000016 - ] + ], + "area": 1952.9041110000107 }, { "segmentation": [ @@ -6188,7 +6292,8 @@ 285.865, 71.33399999999995, 57.334 - ] + ], + "area": 2767.802888999693 }, { "segmentation": [ @@ -6226,7 +6331,8 @@ 353.532, 49.0, 37.0 - ] + ], + "area": 1232.5135000001173 }, { "segmentation": [ @@ -6270,7 +6376,8 @@ 356.865, 79.33399999999995, 52.66699999999997 - ] + ], + "area": 2879.674666499719 }, { "segmentation": [ @@ -6308,7 +6415,8 @@ 558.199, 75.5, 45.5 - ] + ], + "area": 2657.6562500004657 }, { "segmentation": [ @@ -6350,7 +6458,8 @@ 593.699, 53.0, 48.5 - ] + ], + "area": 1684.34375 }, { "segmentation": [ @@ -6388,7 +6497,8 @@ 494.865, 71.49999999999989, 44.0 - ] + ], + "area": 2404.875 }, { "segmentation": [ @@ -6428,7 +6538,8 @@ 687.086, 71.0, 111.0 - ] + ], + "area": 5174.728610999882 }, { "segmentation": [ @@ -6464,7 +6575,8 @@ 781.753, 40.332999999999856, 35.66599999999994 - ] + ], + "area": 969.9918335005641 }, { "segmentation": [ @@ -6498,7 +6610,8 @@ 815.086, 36.0, 31.66700000000003 - ] + ], + "area": 829.3789440002292 }, { "segmentation": [ @@ -6532,7 +6645,8 @@ 803.086, 45.667000000000144, 53.33299999999997 - ] + ], + "area": 1912.8320000004023 }, { "segmentation": [ @@ -6568,7 +6682,8 @@ 866.753, 60.0, 52.66599999999994 - ] + ], + "area": 2178.750055500306 }, { "segmentation": [ @@ -6602,7 +6717,8 @@ 1075.586, 57.5, 48.0 - ] + ], + "area": 2033.7499999995343 }, { "segmentation": [ @@ -6634,7 +6750,8 @@ 969.586, 28.5, 39.5 - ] + ], + "area": 796.625 }, { "segmentation": [ @@ -6674,7 +6791,8 @@ 858.086, 61.333000000000084, 61.33299999999997 - ] + ], + "area": 2620.2131110001355 }, { "segmentation": [ @@ -6720,7 +6838,8 @@ 1126.086, 60.0, 49.333000000000084 - ] + ], + "area": 1844.4292219989002 }, { "segmentation": [ @@ -6760,7 +6879,8 @@ 1034.086, 52.66700000000003, 54.0 - ] + ], + "area": 1938.8961110003293 }, { "segmentation": [ @@ -6798,7 +6918,8 @@ 964.086, 70.66699999999992, 54.0 - ] + ], + "area": 2938.434888998978 }, { "segmentation": [ @@ -6834,7 +6955,8 @@ 871.574, 48.5, 61.5 - ] + ], + "area": 2053.75 }, { "segmentation": [ @@ -6876,7 +6998,8 @@ 710.074, 53.0, 64.5 - ] + ], + "area": 2456.625 } ] } \ No newline at end of file diff --git a/tests/convert/coco_expect_filtered.json b/tests/convert/coco_expect_filtered.json index 16eda91..6aef0c0 100644 --- a/tests/convert/coco_expect_filtered.json +++ b/tests/convert/coco_expect_filtered.json @@ -129,7 +129,8 @@ 894, 81, 99 - ] + ], + "area": 6040.0 }, { "segmentation": [ @@ -153,7 +154,8 @@ 132.046, 48.648999999999994, 48.649 - ] + ], + "area": 1167.2629254999993 }, { "segmentation": [ @@ -185,7 +187,8 @@ 135.125, 50.66699999999999, 31.334000000000003 - ] + ], + "area": 1190.0186665000074 }, { "segmentation": [ @@ -221,7 +224,8 @@ 188.459, 45.333, 48.666 - ] + ], + "area": 1696.6463334999862 }, { "segmentation": [ @@ -257,7 +261,8 @@ 448.651, 53.499999999999986, 47.0 - ] + ], + "area": 2000.75 }, { "segmentation": [ @@ -293,7 +298,8 @@ 407.651, 36.50000000000001, 50.5 - ] + ], + "area": 1286.0 }, { "segmentation": [ @@ -327,7 +333,8 @@ 710.651, 58.00000000000001, 63.0 - ] + ], + "area": 2466.5 }, { "segmentation": [ @@ -359,7 +366,8 @@ 617.469, 52.0, 63.0 - ] + ], + "area": 2398.500000000233 }, { "segmentation": [ @@ -391,7 +399,8 @@ 905.469, 63.0, 63.0 - ] + ], + "area": 2525.0 }, { "segmentation": [ @@ -455,7 +464,8 @@ 820, 111, 132 - ] + ], + "area": 10005.0 }, { "segmentation": [ @@ -485,7 +495,8 @@ 721.961, 61.33299999999997, 66.66700000000003 - ] + ], + "area": 2790.1944445000263 }, { "segmentation": [ @@ -519,7 +530,8 @@ 692.628, 69.333, 89.33299999999997 - ] + ], + "area": 3528.867111000116 }, { "segmentation": [ @@ -555,7 +567,8 @@ 878.545, 48.249999999999986, 47.75 - ] + ], + "area": 1633.5625000001164 } ] } \ No newline at end of file diff --git a/tests/convert/test_labelbox.py b/tests/convert/test_labelbox.py index 315eef7..de63f83 100644 --- a/tests/convert/test_labelbox.py +++ b/tests/convert/test_labelbox.py @@ -1,4 +1,4 @@ -from lapixdl.convert.labelbox import labelbox_to_coco +from lapixdl.convert.labelbox import labelbox_to_coco, __calculate_area import json def test_labelbox_to_coco(): @@ -26,4 +26,10 @@ def test_labelbox_to_coco_w_filter(): conversion = labelbox_to_coco(labelbox_file, imgs_to_include) - assert conversion == coco_expect \ No newline at end of file + assert conversion == coco_expect + +def test_calculate_area(): + segmentation = [0,0, 0,2, 2,2, 2,0] + area = __calculate_area(segmentation) + + assert area == 4 \ No newline at end of file