|
| 1 | +import math |
| 2 | +import time |
| 3 | +from threading import Thread |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from aperturedb import Status |
| 9 | +from aperturedb import ParallelLoader |
| 10 | +from aperturedb import CSVParser |
| 11 | + |
| 12 | +HEADER_X_POS = "x_pos" |
| 13 | +HEADER_Y_POS = "y_pos" |
| 14 | +HEADER_WIDTH = "width" |
| 15 | +HEADER_HEIGHT = "height" |
| 16 | +IMG_KEY_PROP = "img_key_prop" |
| 17 | +IMG_KEY_VAL = "img_key_value" |
| 18 | + |
| 19 | +class BBoxGeneratorCSV(CSVParser.CSVParser): |
| 20 | + |
| 21 | + ''' |
| 22 | + ApertureDB BBox Data loader. |
| 23 | + Expects a csv file with the following columns: |
| 24 | +
|
| 25 | + IMG_KEY,x_pos,y_pos,width,height,BBOX_PROP_NAME_1, ... BBOX_PROP_NAME_N |
| 26 | +
|
| 27 | + IMG_KEY column has the property name of the image property that |
| 28 | + the bounding box will be connected to, and each row has the value |
| 29 | + that will be used for finding the image. |
| 30 | +
|
| 31 | + x_pos,y_pos,width,height are the coordinates of the bounding boxes, |
| 32 | + as integers (unit is in pixels) |
| 33 | +
|
| 34 | + BBOX_PROP_NAME_N is an arbitrary name of the property of the bounding |
| 35 | + box, and each row has the value for that property. |
| 36 | +
|
| 37 | + Example csv file: |
| 38 | + img_unique_id,x_pos,y_pos,width,height,type |
| 39 | + d5b25253-9c1e,257,154,84,125,manual |
| 40 | + d5b25253-9c1e,7,537,522,282,manual |
| 41 | + ... |
| 42 | + ''' |
| 43 | + |
| 44 | + def __init__(self, filename): |
| 45 | + |
| 46 | + super().__init__(filename) |
| 47 | + |
| 48 | + self.props_keys = [x for x in self.header[5:] if not x.startswith(CSVParser.CONTRAINTS_PREFIX) ] |
| 49 | + self.constraints_keys = [x for x in self.header[5:] if x.startswith(CSVParser.CONTRAINTS_PREFIX) ] |
| 50 | + |
| 51 | + self.img_key = self.header[0] |
| 52 | + |
| 53 | + def __getitem__(self, idx): |
| 54 | + |
| 55 | + data = { |
| 56 | + "x": int(self.df.loc[idx, HEADER_X_POS]), |
| 57 | + "y": int(self.df.loc[idx, HEADER_Y_POS]), |
| 58 | + "width": int(self.df.loc[idx, HEADER_WIDTH]), |
| 59 | + "height": int(self.df.loc[idx, HEADER_HEIGHT]), |
| 60 | + } |
| 61 | + |
| 62 | + val = self.df.loc[idx, self.img_key] |
| 63 | + |
| 64 | + if val != "": |
| 65 | + data[IMG_KEY_PROP] = self.img_key |
| 66 | + data[IMG_KEY_VAL] = val |
| 67 | + |
| 68 | + properties = self.parse_properties(self.df, idx) |
| 69 | + constraints = self.parse_constraints(self.df, idx) |
| 70 | + |
| 71 | + if properties: |
| 72 | + data[CSVParser.PROPERTIES] = properties |
| 73 | + |
| 74 | + if constraints: |
| 75 | + data[CSVParser.CONSTRAINTS] = constraints |
| 76 | + |
| 77 | + return data |
| 78 | + |
| 79 | + def validate(self): |
| 80 | + |
| 81 | + self.header = list(self.df.columns.values) |
| 82 | + |
| 83 | + if self.header[1] != HEADER_X_POS: |
| 84 | + raise Exception("Error with CSV file field: " + HEADER_X_POS) |
| 85 | + if self.header[2] != HEADER_Y_POS: |
| 86 | + raise Exception("Error with CSV file field: " + HEADER_Y_POS) |
| 87 | + if self.header[3] != HEADER_WIDTH: |
| 88 | + raise Exception("Error with CSV file field: " + HEADER_WIDTH) |
| 89 | + if self.header[4] != HEADER_HEIGHT: |
| 90 | + raise Exception("Error with CSV file field: " + HEADER_HEIGHT) |
| 91 | + |
| 92 | +class BBoxLoader(ParallelLoader.ParallelLoader): |
| 93 | + |
| 94 | + def __init__(self, db, dry_run=False): |
| 95 | + |
| 96 | + super().__init__(db, dry_run=dry_run) |
| 97 | + |
| 98 | + self.type = "bbox" |
| 99 | + |
| 100 | + def generate_batch(self, bbox_data): |
| 101 | + |
| 102 | + q = [] |
| 103 | + |
| 104 | + ref_counter = 1 |
| 105 | + for data in bbox_data: |
| 106 | + |
| 107 | + # TODO we could reuse image references within the batch |
| 108 | + # instead of creating a new find for every image. |
| 109 | + img_ref = ref_counter |
| 110 | + ref_counter += 1 |
| 111 | + fi = { |
| 112 | + "FindImage": { |
| 113 | + "_ref": img_ref, |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if IMG_KEY_PROP in data: |
| 118 | + key = data[IMG_KEY_PROP] |
| 119 | + val = data[IMG_KEY_VAL] |
| 120 | + constraints = {} |
| 121 | + constraints[key] = ["==", val] |
| 122 | + fi["FindImage"]["constraints"] = constraints |
| 123 | + |
| 124 | + q.append(fi) |
| 125 | + |
| 126 | + ai = { |
| 127 | + "AddBoundingBox": { |
| 128 | + "image": img_ref, |
| 129 | + "rectangle": { |
| 130 | + "x": data["x"], |
| 131 | + "y": data["y"], |
| 132 | + "width": data["width"], |
| 133 | + "height": data["height"], |
| 134 | + }, |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if "properties" in data: |
| 139 | + ai["AddBoundingBox"]["properties"] = data[CSVParser.PROPERTIES] |
| 140 | + |
| 141 | + q.append(ai) |
| 142 | + |
| 143 | + if self.dry_run: |
| 144 | + print(q) |
| 145 | + |
| 146 | + return q, [] |
0 commit comments