def _make_image_labels_dataset(img, images_dir, num_samples=4, num_objects_per_sample=3): exts = [".jpg", ".png"] samples = [] for idx in range(num_samples): filepath = os.path.join(images_dir, "%06d%s" % (idx, exts[idx % len(exts)])) etai.write(img, filepath) image_labels = etai.ImageLabels() _label = random.choice(["sun", "rain", "snow"]) image_labels.add_attribute(etad.CategoricalAttribute("label", _label)) for _ in range(num_objects_per_sample): _label = random.choice(["cat", "dog", "bird", "rabbit"]) _xtl = 0.8 * random.random() _ytl = 0.8 * random.random() _bounding_box = etag.BoundingBox.from_coords( _xtl, _ytl, _xtl + 0.2, _ytl + 0.2) image_labels.add_object( etao.DetectedObject(label=_label, bounding_box=_bounding_box)) samples.append( fo.Sample( filepath=filepath, ground_truth=fo.ImageLabels(labels=image_labels), )) dataset = fo.Dataset() dataset.add_samples(samples) return dataset
def to_detected_object(self, name=None): """Returns an ``eta.core.objects.DetectedObject`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.objects.DetectedObject`` """ label = self.label index = self.index # pylint: disable=unpacking-non-sequence tlx, tly, w, h = self.bounding_box brx = tlx + w bry = tly + h bounding_box = etag.BoundingBox.from_coords(tlx, tly, brx, bry) mask = self.mask confidence = self.confidence # pylint: disable=no-member attrs = _to_eta_attributes(self.attributes) return etao.DetectedObject( label=label, index=index, bounding_box=bounding_box, mask=mask, confidence=confidence, name=name, attrs=attrs, )
def _parse_bdd_annotation(d, frame_size): image_labels = etai.ImageLabels() # Frame attributes frame_attrs = d.get("attributes", {}) image_labels.attrs = _make_attributes(frame_attrs) # Objects objects = d.get("labels", []) for obj in objects: if "box2d" not in obj: continue label = obj["category"] bbox = obj["box2d"] bounding_box = etag.BoundingBox.from_abs_coords( bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"], frame_size=frame_size, ) obj_attrs = obj.get("attributes", {}) attrs = _make_attributes(obj_attrs) image_labels.add_object( etao.DetectedObject( label=label, bounding_box=bounding_box, attrs=attrs, )) return fol.ImageLabels(labels=image_labels)
def to_detected_object(self, name=None): """Returns an ``eta.core.objects.DetectedObject`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.objects.DetectedObject`` """ label = self.label # pylint: disable=unpacking-non-sequence tlx, tly, w, h = self.bounding_box brx = tlx + w bry = tly + h bounding_box = etag.BoundingBox.from_coords(tlx, tly, brx, bry) confidence = self.confidence # pylint: disable=no-member attrs = etad.AttributeContainer() for attr_name, attr in self.attributes.items(): attrs.add(etad.CategoricalAttribute(attr_name, attr.value)) return etao.DetectedObject( label=label, bounding_box=bounding_box, confidence=confidence, name=name, attrs=attrs, )
def _parse_bdd_object(d, frame_size): label = d["category"] box2d = d["box2d"] bounding_box = etag.BoundingBox.from_abs_coords( box2d["x1"], box2d["y1"], box2d["x2"], box2d["y2"], frame_size=frame_size, clamp=False, ) obj_attrs = d.get("attributes", {}) attrs = _make_attributes(obj_attrs) return etao.DetectedObject( label=label, bounding_box=bounding_box, attrs=attrs, )
def to_detected_object(label, det, image): ''' convert the ObjectDetector detection to an eta DetectedObject. Args: label is the label of the object det is a 5-vector with topleft-x,y bottomright-x,y confidence as values image is the original image that was use to detect this object ''' sx, sy, ex, ey, conf = det return etao.DetectedObject(label, etag.BoundingBox( etag.RelativePoint.from_abs(sx, sy, img=image), etag.RelativePoint.from_abs(ex, ey, img=image), ), confidence=conf)
def to_detected_object(self, frame_size): """Returns a ``eta.core.objects.DetectedObject`` representation of the box. Args: frame_size: the ``(width, height)`` of the image Returns: a ``eta.core.objects.DetectedObject`` """ label = self.label bounding_box = etag.BoundingBox.from_abs_coords(self.xtl, self.ytl, self.xbr, self.ybr, frame_size=frame_size) attrs = etad.AttributeContainer( attrs=[a.to_eta_attribute() for a in self.attributes]) return etao.DetectedObject(label=label, bounding_box=bounding_box, attrs=attrs)