Skip to content

Commit e477cde

Browse files
committed
Fix validation and robustness of annotation segmentation property
1 parent e6dc304 commit e477cde

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/Annotation.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Annotation
1212
public int $id;
1313
public int $image_id;
1414
public int $category_id;
15-
public ?array $bbox;
1615
public array $segmentation;
16+
public ?array $bbox;
1717

1818
private ?Shape $shape = null;
1919
private ?array $points = null;
@@ -26,16 +26,16 @@ public static function create(array $data): self
2626
$instance->id = $data['id'];
2727
$instance->image_id = $data['image_id'];
2828
$instance->category_id = $data['category_id'];
29+
$instance->segmentation = is_array($data['segmentation'][0]) ? $data['segmentation'][0] : $data['segmentation'];
2930
$instance->bbox = $data['bbox'] ?? null;
30-
$instance->segmentation = $data['segmentation'][0] ?? null;
3131

3232
return $instance;
3333
}
3434

3535
// Validate the structure
3636
public static function validate(array $data): void
3737
{
38-
$requiredKeys = ['id', 'image_id', 'category_id'];
38+
$requiredKeys = ['id', 'image_id', 'category_id', 'segmentation'];
3939
foreach ($requiredKeys as $key) {
4040
if (!array_key_exists($key, $data)) {
4141
throw new \Exception("Missing key '$key' in Annotation");

tests/CocoParserTest.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Biigle\Tests\Modules\MetadataCoco;
44

5-
use Symfony\Component\HttpFoundation\File\File;
65
use Biigle\MediaType;
7-
use Biigle\Shape;
86
use Biigle\Modules\MetadataCoco\Annotation;
97
use Biigle\Modules\MetadataCoco\Coco;
108
use Biigle\Modules\MetadataCoco\CocoParser;
11-
9+
use Biigle\Shape;
10+
use Exception;
11+
use Symfony\Component\HttpFoundation\File\File;
1212
use TestCase;
1313

1414
class CocoParserTest extends TestCase
@@ -98,6 +98,29 @@ public function testGetMetadata()
9898
$this->assertSame("Animal", $annotations[5]->labels[0]->label->name);
9999
}
100100

101+
public function testValidateSegmentation()
102+
{
103+
$this->expectException(Exception::class);
104+
Annotation::validate([
105+
'id' => 1,
106+
'image_id' => 1,
107+
'category_id' => 1,
108+
'bbox' => null,
109+
]);
110+
}
111+
112+
public function testUseSegmentationSingleArray()
113+
{
114+
$annotation = Annotation::create([
115+
'id' => 1,
116+
'image_id' => 1,
117+
'category_id' => 1,
118+
'bbox' => null,
119+
'segmentation' => [1, 1]
120+
]);
121+
$this->assertSame([1, 1], $annotation->getPoints());
122+
}
123+
101124
public function testIsPointShape()
102125
{
103126
$pointAnnotation = Annotation::create([

0 commit comments

Comments
 (0)