def test_crop_in_batch(): uri = "http://farm2.staticflickr.com/1129/4726871278_4dd241a03a_z.jpg" img = Image(uri) data = img.to_numpy() patches = img.crop( [Box2d(10, 10, 30, 30), Box2d(15, 15, 35, 35), Box2d(20, 20, 40, 40)]) assert len(patches) == 3 assert np.array_equal(patches[0].to_numpy(), data[10:30, 10:30, :]) assert np.array_equal(patches[1].to_numpy(), data[15:35, 15:35, :]) assert np.array_equal(patches[2].to_numpy(), data[20:40, 20:40, :])
def deserialize(self, datum: Row) -> "rikai.types.geometry.Box2d": from rikai.types.geometry import Box2d if len(datum) < 4: logger.error(f"Deserialize box2d: not sufficient data: {datum}") return Box2d(*datum[:4])
def box2d_from_center(coords) -> Box2d: """Build a Box2d from a center-point based coordinate array: ``[center_x, center_y, width, height]``. See Also -------- :py:meth:`rikai.types.geometry.Box2d.from_center` """ return Box2d.from_center(coords[0], coords[1], coords[2], coords[3])
def test_dataframe_with_udt(): data = [{"id": i, "box": Box2d(i, i + 1, i + 2, i + 3)} for i in range(10)] df = pd.DataFrame(data) dataset = PandasDataset(df) assert len(dataset) == 10 expected = [ {"id": i, "box": np.array([i, i + 1, i + 2, i + 3])} for i in range(10) ] for i in range(10): row = dataset[i] assert row["id"] == expected[i]["id"] assert np.array_equal(row["box"], expected[i]["box"])
def box2d_from_top_left(coords) -> Box2d: """Build :py:class:`Box2d` from a top-left based coordinate array: ``[x0, y0, width, height]`` Example ------- `Coco dataset <https://cocodataset.org/>`_ is one public dataset that use top-left coordinates >>> #! pyspark --packages ai.eto:rikai_2.12:0.0.1 >>> import json >>> from rikai.spark.functions.geometry import box2d_from_top_left >>> >>> with open("coco_sample/annotations/train_sample.json") as fobj: ... coco = json.load(fobj) >>> anno_df = ( ... spark ... .createDataFrame(coco["annotations"][:5]) ... .withColumn("box2d", box2d_from_top_left("bbox")) ... ) >>> anno_df.show() +--------------------+-----------+--------+--------------------+ | bbox|category_id|image_id| box2d| +--------------------+-----------+--------+--------------------+ |[505.24, 0.0, 47....| 72| 318219|Box2d(x=505.24, y...| |[470.68, 0.0, 45....| 72| 318219|Box2d(x=470.68, y...| |[442.51, 0.0, 43....| 72| 318219|Box2d(x=442.51, y...| |[380.74, 112.85, ...| 72| 554625|Box2d(x=380.74, y...| |[339.13, 32.99, 3...| 72| 554625|Box2d(x=339.13, y...| +--------------------+-----------+--------+--------------------+ >>> anno_df.printSchema() root |-- bbox: array (nullable = true) | |-- element: double (containsNull = true) |-- category_id: long (nullable = true) |-- image_id: long (nullable = true) |-- box2d: box2d (nullable = true) See Also -------- :py:meth:`rikai.types.geometry.Box2d.from_top_left`. """ return Box2d.from_top_left(coords[0], coords[1], coords[2], coords[3])
def box2d(coords) -> Box2d: """Build a Box2d from ``[xmin,ymin,xmax,ymax]`` array.""" return Box2d(coords[0], coords[1], coords[2], coords[3])
def test_pandas_series_with_udt(): data = pd.Series([Box2d(i, i + 1, i + 2, i + 3) for i in range(10)]) dataset = PandasDataset(data) assert len(dataset) == 10 for i in range(10): assert np.array_equal(dataset[i], np.array([i, i + 1, i + 2, i + 3]))
def test_crop_image(): data = np.random.randint(0, 255, size=(100, 100), dtype=np.uint8) im = Image.from_array(data) patch = im.crop(Box2d(10, 10, 30, 30)) cropped_data = patch.to_numpy() assert np.array_equal(cropped_data, data[10:30, 10:30])
def test_crop_real_image(): uri = "http://farm2.staticflickr.com/1129/4726871278_4dd241a03a_z.jpg" img = Image(uri) data = img.to_numpy() patch = img.crop(Box2d(10, 10, 30, 30)) assert np.array_equal(patch.to_numpy(), data[10:30, 10:30, :])