Skip to content

Commit

Permalink
Merge pull request #139 from biigle/div-by-zero
Browse files Browse the repository at this point in the history
Fix division by error
  • Loading branch information
mzur authored Feb 2, 2024
2 parents a980bc9 + ad05389 commit 7d1a72a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Jobs/ProcessAnnotatedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,15 @@ protected function getEllipseSvgAnnotation(array $tuples): SVGEllipse
protected function computeRotationAngle(array $v, array $u): float
{
// If (upper) left and (upper) right coordinate have equal y coordinate, then there is no rotation
if ($v[1] === 0) {
if (intval($v[1]) === 0) {
return 0;
}

// Compute angle
$scalarProd = 0;
$vNorm = 0;
$uNorm = 0;
for ($i = 0; $i < sizeof($v); $i++) {
for ($i = 0; $i < count($v); $i++) {
$scalarProd += $v[$i] * $u[$i];
$vNorm += pow($v[$i], 2);
$uNorm += pow($u[$i], 2);
Expand Down
25 changes: 25 additions & 0 deletions tests/Jobs/ProcessAnnotatedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,31 @@ public function testHandleOnlyAnnotations()
$this->assertEquals(1, ImageAnnotationLabelFeatureVector::count());
}

public function testHandleInvalidShape()
{
config(['thumbnails.height' => 100, 'thumbnails.width' => 100]);
$disk = Storage::fake('test');
$image = $this->getImageMock();
$annotation = ImageAnnotationTest::create([
// This is a real-world example where someone managed to create a zero-sized
// rectangle.
'points' => [844.69,1028.44,844.69,1028.44,844.69,1028.44,844.69,1028.44],
'shape_id' => Shape::rectangleId(),
]);
$job = new ProcessAnnotatedImageStub($annotation->image);
$job->mock = $image;

$image->shouldReceive('crop')
->once()
->andReturn($image);

$image->shouldReceive('writeToBuffer')->once()->andReturn('abc123');

// Assert that no exception is thrown
$job->handle();

}

public function testHandleFeatureVectorTiledImage()
{
$vipsImage = $this->getImageMock(0);
Expand Down
26 changes: 26 additions & 0 deletions tests/Jobs/ProcessAnnotatedVideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,32 @@ public function testHandleOnlyAnnotations()
$this->assertEquals(1, VideoAnnotationLabelFeatureVector::count());
}

public function testHandleInvalidShape()
{
config(['thumbnails.height' => 100, 'thumbnails.width' => 100]);
$disk = Storage::fake('test2');
$video = $this->getFrameMock();
$annotation = VideoAnnotationTest::create([
// This is a real-world example where someone managed to create a zero-sized
// rectangle.
'points' => [[844.69,1028.44,844.69,1028.44,844.69,1028.44,844.69,1028.44]],
'frames' => [0],
'shape_id' => Shape::rectangleId(),
]);
$job = new ProcessAnnotatedVideoStub($annotation->video, targetDisk: 'test2');
$job->mock = $video;

$video->shouldReceive('crop')
->once()
->andReturn($video);

$video->shouldReceive('writeToBuffer')->once()->andReturn('abc123');

// Assert that no exception is thrown
$job->handle();
}


protected function getFrameMock($times = 1)
{
$video = Mockery::mock();
Expand Down

0 comments on commit 7d1a72a

Please sign in to comment.