Skip to content

Commit

Permalink
Merge pull request #3 from biigle/patch-1
Browse files Browse the repository at this point in the history
Fix validation and robustness of annotation segmentation property
  • Loading branch information
mzur authored Nov 5, 2024
2 parents e6dc304 + e477cde commit 4b7da60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,16 +26,16 @@ 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;
}

// 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");
Expand Down
29 changes: 26 additions & 3 deletions tests/CocoParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 4b7da60

Please sign in to comment.