diff --git a/src/Annotation.php b/src/Annotation.php index ce083ce..143f2d8 100644 --- a/src/Annotation.php +++ b/src/Annotation.php @@ -12,8 +12,8 @@ class Annotation public int $id; public int $image_id; public int $category_id; - public ?array $bbox; public array $segmentation; + public ?array $bbox; private ?Shape $shape = null; private ?array $points = null; @@ -26,8 +26,8 @@ public static function create(array $data): self $instance->id = $data['id']; $instance->image_id = $data['image_id']; $instance->category_id = $data['category_id']; + $instance->segmentation = is_array($data['segmentation'][0]) ? $data['segmentation'][0] : $data['segmentation']; $instance->bbox = $data['bbox'] ?? null; - $instance->segmentation = $data['segmentation'][0] ?? null; return $instance; } @@ -35,7 +35,7 @@ public static function create(array $data): self // Validate the structure public static function validate(array $data): void { - $requiredKeys = ['id', 'image_id', 'category_id']; + $requiredKeys = ['id', 'image_id', 'category_id', 'segmentation']; foreach ($requiredKeys as $key) { if (!array_key_exists($key, $data)) { throw new \Exception("Missing key '$key' in Annotation"); diff --git a/tests/CocoParserTest.php b/tests/CocoParserTest.php index bb49dc2..5af7a9f 100644 --- a/tests/CocoParserTest.php +++ b/tests/CocoParserTest.php @@ -2,13 +2,13 @@ namespace Biigle\Tests\Modules\MetadataCoco; -use Symfony\Component\HttpFoundation\File\File; use Biigle\MediaType; -use Biigle\Shape; use Biigle\Modules\MetadataCoco\Annotation; use Biigle\Modules\MetadataCoco\Coco; use Biigle\Modules\MetadataCoco\CocoParser; - +use Biigle\Shape; +use Exception; +use Symfony\Component\HttpFoundation\File\File; use TestCase; class CocoParserTest extends TestCase @@ -98,6 +98,29 @@ public function testGetMetadata() $this->assertSame("Animal", $annotations[5]->labels[0]->label->name); } + public function testValidateSegmentation() + { + $this->expectException(Exception::class); + Annotation::validate([ + 'id' => 1, + 'image_id' => 1, + 'category_id' => 1, + 'bbox' => null, + ]); + } + + public function testUseSegmentationSingleArray() + { + $annotation = Annotation::create([ + 'id' => 1, + 'image_id' => 1, + 'category_id' => 1, + 'bbox' => null, + 'segmentation' => [1, 1] + ]); + $this->assertSame([1, 1], $annotation->getPoints()); + } + public function testIsPointShape() { $pointAnnotation = Annotation::create([