Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ public static function validate(array $data): void

public function getLabel(array $categories): Label
{
$categoryIndex = array_search($this->category_id, array_column($categories, 'id'));
$category = $categories[$categoryIndex];
$category = $categories[$this->category_id];

return new Label(id: $category->id, name: $category->name);
}

public function getLabelAndUsers(array $categories): array
{
$cocoUser = Coco::getCocoUser();
$label = $this->getLabel($categories);

return [new LabelAndUser(label: $label, user: $cocoUser)];
}

Expand All @@ -85,6 +86,11 @@ public function getPoints(): array
if ($this->getShape()->id === Shape::circleId()) {
return $this->getCirclePoints();
}

if ($this->getShape()->id === Shape::rectangleId()) {
return array_slice($this->segmentation, 0, 8);
}

return $this->segmentation;
}

Expand Down Expand Up @@ -204,27 +210,22 @@ function euclidean_distance($x1, $y1, $x2, $y2)

public function isRectangleShape(): bool
{
if (count($this->segmentation) !== 8) {
// Some rectangles have an additional last point equalling the first point,
// similar to polygons.
$segmentationCount = count($this->segmentation);
if ($segmentationCount !== 8 && $segmentationCount !== 10) {
return false;
}

// Toleranz für Gleitkomma-Vergleiche
// Tolerance for float comparison.
$tolerance = 0.01;

// Punkte (x1, y1), (x2, y2), (x3, y3), (x4, y4)
list($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = $this->segmentation;

// Berechne die Seitenlängen
$d1 = $this->euclidean_distance($x1, $y1, $x2, $y2); // Distanz zwischen P1 und P2
$d2 = $this->euclidean_distance($x2, $y2, $x3, $y3); // Distanz zwischen P2 und P3
$d3 = $this->euclidean_distance($x3, $y3, $x4, $y4); // Distanz zwischen P3 und P4
$d4 = $this->euclidean_distance($x4, $y4, $x1, $y1); // Distanz zwischen P4 und P1

// Berechne die Diagonalen
$diag1 = $this->euclidean_distance($x1, $y1, $x3, $y3); // Diagonale P1 -> P3
$diag2 = $this->euclidean_distance($x2, $y2, $x4, $y4); // Diagonale P2 -> P4
$diag1 = $this->euclidean_distance($x1, $y1, $x3, $y3);
$diag2 = $this->euclidean_distance($x2, $y2, $x4, $y4);

// Prüfen, ob gegenüberliegende Seiten gleich lang sind und Diagonalen gleich lang sind
return abs($d1 - $d3) < $tolerance && abs($d2 - $d4) < $tolerance && abs($diag1 - $diag2) < $tolerance;
// If diagonals are equal we have a rectangle.
return abs($diag1 - $diag2) < $tolerance;
}
}
4 changes: 3 additions & 1 deletion src/Coco.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public static function create(array $data): self
}, $data['annotations']);

// Create the Category objects
$instance->categories = array_map(function ($categoryData) {
$categories = array_map(function ($categoryData) {
return Category::create($categoryData);
}, $data['categories']);

$instance->categories = array_combine(array_map(fn ($c) => $c->id, $categories), $categories);

// validate the data consistency
$instance->validateCategoriesInData();
$instance->validateImagesInData();
Expand Down
31 changes: 16 additions & 15 deletions src/CocoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Biigle\Modules\MetadataCoco;

use Biigle\MediaType;
use Biigle\Services\MetadataParsing\MetadataParser;
use Biigle\Services\MetadataParsing\VolumeMetadata;
use Biigle\Services\MetadataParsing\ImageAnnotation;
use Biigle\Services\MetadataParsing\ImageMetadata;
use Biigle\Modules\MetadataCoco\Coco;
use Biigle\Modules\MetadataCoco\Image;
use Biigle\Services\MetadataParsing\ImageAnnotation;
use Biigle\Services\MetadataParsing\ImageMetadata;
use Biigle\Services\MetadataParsing\MetadataParser;
use Biigle\Services\MetadataParsing\VolumeMetadata;
use Illuminate\Support\Collection;

class CocoParser extends MetadataParser
{
Expand Down Expand Up @@ -74,32 +75,32 @@ public function getMetadata(): VolumeMetadata
handle: null,
);

$annotations = collect($coco->annotations)->groupBy('image_id');
$categories = $coco->categories;

foreach ($coco->images as $image) {
$imageMetaData = new ImageMetadata(
name: $image->file_name
);

$this->processImageAnnotations($image, $imageMetaData);
if ($annotations->has($image->id)) {
$imageMetaData->annotations = $this->processImageAnnotations($annotations->get($image->id), $categories);
}

$metadata->addFile($imageMetaData);
}

return $metadata;
}

private function processImageAnnotations(Image $image, ImageMetadata $imageMetaData)
private function processImageAnnotations(Collection $annotations, array $categories)
{
$annotations = array_filter($this->getCoco()->annotations, function ($annotation) use ($image) {
return $annotation->image_id === $image->id;
});

foreach ($annotations as $annotation) {
$metaDataAnnotation = new ImageAnnotation(
return $annotations->map(function ($annotation) use ($categories) {
return new ImageAnnotation(
shape: $annotation->getShape(),
points: $annotation->getPoints(),
labels: $annotation->getLabelAndUsers($this->getCoco()->categories),
labels: $annotation->getLabelAndUsers($categories),
);
$imageMetaData->addAnnotation($metaDataAnnotation);
}
})->toArray();
}
}
33 changes: 32 additions & 1 deletion tests/CocoParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,37 @@ public function testIsRectangleShape()
$this->assertSame($rectangleAnnotation->getPoints(), $rectangleAnnotation->segmentation);
}

public function testIsRectangleShapeWithMorePoints()
{
$rectangleAnnotation = Annotation::create([
'id' => 1,
'image_id' => 1,
'category_id' => 1,
'bbox' => null,
'segmentation' => [
[
1853.22,
596.22,

1776.16,
799.04,

1597.21,
731.04,

1674.27,
528.23,

1853.22,
596.22,
]
],
]);
$this->assertTrue($rectangleAnnotation->isRectangleShape());
$this->assertSame($rectangleAnnotation->getShape(), Shape::rectangle());
$this->assertSame($rectangleAnnotation->getPoints(), array_slice($rectangleAnnotation->segmentation, 0, 8));
}

public function testIsCircleShape()
{
$circleAnnotation = Annotation::create([
Expand Down Expand Up @@ -328,7 +359,7 @@ public function testIsPolygonShape()
'image_id' => 1,
'category_id' => 1,
'bbox' => null,
'segmentation' => [[1, 1, 2, 2, 3, 3, 4, 4, 1, 1]]
'segmentation' => [[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1]]
]);
$this->assertSame($polygonAnnotation->getShape(), Shape::polygon());
}
Expand Down