|
| 1 | +from matplotlib.transforms import Bbox |
| 2 | +from aperturedb import ParallelLoader |
| 3 | +from aperturedb import CSVParser |
| 4 | + |
| 5 | +HEADER_X_POS = "x_pos" |
| 6 | +HEADER_Y_POS = "y_pos" |
| 7 | +HEADER_WIDTH = "width" |
| 8 | +HEADER_HEIGHT = "height" |
| 9 | +IMG_KEY_PROP = "img_key_prop" |
| 10 | +IMG_KEY_VAL = "img_key_value" |
| 11 | + |
| 12 | + |
| 13 | +class BBoxDataCSV(CSVParser.CSVParser): |
| 14 | + """ |
| 15 | + **ApertureDB BBox Data.** |
| 16 | +
|
| 17 | + This class loads the Bounding Box Data which is present in a csv file, |
| 18 | + and converts it into a series of aperturedb queries. |
| 19 | +
|
| 20 | + .. note:: |
| 21 | + Is backed by a csv file with the following columns: |
| 22 | +
|
| 23 | + ``IMG_KEY``, ``x_pos``, ``y_pos``, ``width``, ``height``, ``BBOX_PROP_NAME_1``, ... ``BBOX_PROP_NAME_N``, ``constraint_BBOX_PROP_NAME_1`` |
| 24 | +
|
| 25 | + **IMG_KEY**: column has the property name of the image property that |
| 26 | + the bounding box will be connected to, and each row has the value |
| 27 | + that will be used for finding the image. |
| 28 | +
|
| 29 | + **x_pos, y_pos**: Specify the coordinates of top left of the bounding box. |
| 30 | +
|
| 31 | + **width, height**: Specify the dimensions of the bounding box, as integers (unit is in pixels). |
| 32 | +
|
| 33 | + **BBOX_PROP_NAME_N**: is an arbitrary name of the property of the bounding |
| 34 | + box, and each row has the value for that property. |
| 35 | +
|
| 36 | + **constraint_BBOX_PROP_NAME_1**: Constraints against specific property, used for conditionally adding a Bounding Box. |
| 37 | +
|
| 38 | + Example csv file:: |
| 39 | +
|
| 40 | + img_unique_id,x_pos,y_pos,width,height,type,dataset_id,constraint_dataset_id |
| 41 | + d5b25253-9c1e,257,154,84,125,manual,12345,12345 |
| 42 | + d5b25253-9c1e,7,537,522,282,manual,12346,12346 |
| 43 | + ... |
| 44 | +
|
| 45 | + Example usage: |
| 46 | +
|
| 47 | + .. code-block:: python |
| 48 | +
|
| 49 | + data = BBoxDataCSV("/path/to/BoundingBoxesData.csv") |
| 50 | + loader = ParallelLoader(db) |
| 51 | + loader.ingest(data) |
| 52 | +
|
| 53 | +
|
| 54 | + .. important:: |
| 55 | + In the above example, the constraint_dataset_id ensures that a bounding box with the specified |
| 56 | + dataset_id would be only inserted if it does not already exist in the database. |
| 57 | +
|
| 58 | + """ |
| 59 | + |
| 60 | + def __init__(self, filename): |
| 61 | + |
| 62 | + super().__init__(filename) |
| 63 | + |
| 64 | + self.props_keys = [x for x in self.header[5:] |
| 65 | + if not x.startswith(CSVParser.CONTRAINTS_PREFIX)] |
| 66 | + self.constraints_keys = [x for x in self.header[5:] |
| 67 | + if x.startswith(CSVParser.CONTRAINTS_PREFIX)] |
| 68 | + |
| 69 | + self.img_key = self.header[0] |
| 70 | + self.command = "AddBoundingBox" |
| 71 | + |
| 72 | + def getitem(self, idx): |
| 73 | + |
| 74 | + val = self.df.loc[idx, self.img_key] |
| 75 | + box_data_headers = [HEADER_X_POS, |
| 76 | + HEADER_Y_POS, HEADER_WIDTH, HEADER_HEIGHT] |
| 77 | + box_data = [int(self.df.loc[idx, h]) for h in box_data_headers] |
| 78 | + |
| 79 | + q = [] |
| 80 | + |
| 81 | + ref_counter = idx + 1 |
| 82 | + # TODO we could reuse image references within the batch |
| 83 | + # instead of creating a new find for every image. |
| 84 | + img_ref = ref_counter |
| 85 | + fi = { |
| 86 | + "FindImage": { |
| 87 | + "_ref": img_ref, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + key = self.img_key |
| 92 | + val = val |
| 93 | + constraints = {} |
| 94 | + constraints[key] = ["==", val] |
| 95 | + fi["FindImage"]["constraints"] = constraints |
| 96 | + q.append(fi) |
| 97 | + |
| 98 | + rect_attrs = ["x", "y", "width", "height"] |
| 99 | + custom_fields = { |
| 100 | + "image": img_ref, |
| 101 | + "rectangle": { |
| 102 | + attr: val for attr, val in zip(rect_attrs, box_data) |
| 103 | + }, |
| 104 | + } |
| 105 | + abb = self._basic_command(idx, custom_fields) |
| 106 | + |
| 107 | + properties = self.parse_properties(self.df, idx) |
| 108 | + if properties: |
| 109 | + props = properties |
| 110 | + if "_label" in props: |
| 111 | + abb[self.command]["label"] = props["_label"] |
| 112 | + props.pop("_label") |
| 113 | + # Check if props is not empty after removing "_label" |
| 114 | + if props: |
| 115 | + abb[self.command]["properties"] = props |
| 116 | + q.append(abb) |
| 117 | + |
| 118 | + return q, [] |
| 119 | + |
| 120 | + def validate(self): |
| 121 | + |
| 122 | + self.header = list(self.df.columns.values) |
| 123 | + |
| 124 | + if self.header[1] != HEADER_X_POS: |
| 125 | + raise Exception("Error with CSV file field: " + HEADER_X_POS) |
| 126 | + if self.header[2] != HEADER_Y_POS: |
| 127 | + raise Exception("Error with CSV file field: " + HEADER_Y_POS) |
| 128 | + if self.header[3] != HEADER_WIDTH: |
| 129 | + raise Exception("Error with CSV file field: " + HEADER_WIDTH) |
| 130 | + if self.header[4] != HEADER_HEIGHT: |
| 131 | + raise Exception("Error with CSV file field: " + HEADER_HEIGHT) |
0 commit comments