Skip to content

Commit 8a5bfd7

Browse files
authored
✨ Add inference options, fix polygons (#139)
1 parent 7c4a2a3 commit 8a5bfd7

14 files changed

Lines changed: 367 additions & 99 deletions

File tree

.github/workflows/_test-code-samples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
matrix:
1414
php-version:
1515
- "7.4"
16-
- "8.2"
16+
- "8.3"
1717
runs-on: ubuntu-latest
1818
steps:
1919
- uses: actions/checkout@v4

.github/workflows/_test-units.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: shivammathur/setup-php@v2
2525
with:
2626
php-version: ${{ matrix.php-version }}
27-
- uses: ramsey/composer-install@v2
27+
- uses: ramsey/composer-install@v3
2828
- name: Unit testing with phpunit
2929
env:
3030
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
@@ -55,7 +55,7 @@ jobs:
5555
uses: shivammathur/setup-php@v2
5656
with:
5757
php-version: ${{ matrix.php-version }}
58-
- uses: ramsey/composer-install@v2
58+
- uses: ramsey/composer-install@v3
5959
- name: Change ImageMagick security policy on Ubuntu
6060
run: |
6161
DQT='"'
@@ -90,7 +90,7 @@ jobs:
9090
phpts: zts
9191
with:
9292
php-version: ${{ matrix.php-version }}
93-
- uses: ramsey/composer-install@v2
93+
- uses: ramsey/composer-install@v3
9494
- name: Unit testing with phpunit
9595
env:
9696
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
@@ -133,7 +133,7 @@ jobs:
133133
# php-version: ${{ matrix.php-version }}
134134
# - name: Install Imagick
135135
# run: pecl install imagick
136-
# - uses: ramsey/composer-install@v2
136+
# - uses: ramsey/composer-install@v3
137137
# - name: Unit testing with phpunit
138138
# env:
139139
# MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
@@ -163,7 +163,7 @@ jobs:
163163
with:
164164
php-version: ${{ matrix.php-version }}
165165
extensions: curl, fileinfo, json
166-
- uses: ramsey/composer-install@v2
166+
- uses: ramsey/composer-install@v3
167167
- name: Unit testing with phpunit
168168
env:
169169
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
@@ -200,7 +200,7 @@ jobs:
200200
with:
201201
php-version: ${{ matrix.php-version }}
202202
extensions: curl, fileinfo, json, imagick
203-
- uses: ramsey/composer-install@v2
203+
- uses: ramsey/composer-install@v3
204204
- name: Unit testing with phpunit
205205
env:
206206
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}

docs/code_samples/default_v2.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ $mindeeClient = new ClientV2($apiKey);
1414
// Set inference parameters
1515
// Note: modelId is mandatory.
1616
$inferenceParams = new InferenceParameters(
17+
// ID of the model, required.
1718
$modelId,
18-
// If set to `true`, will enable Retrieval-Augmented Generation.
19+
20+
// Options: set to `true` or `false` to override defaults
21+
22+
// Enhance extraction accuracy with Retrieval-Augmented Generation.
23+
false,
24+
// Extract the full text content from the document as strings.
25+
false,
26+
// Calculate bounding box polygons for all fields.
27+
false,
28+
// Boost the precision and accuracy of all extractions.
29+
// Calculate confidence scores for all fields.
1930
false
2031
);
2132

src/Geometry/Point.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Mindee\Geometry;
44

5+
use ArrayAccess;
6+
use InvalidArgumentException;
7+
58
/**
69
* Representation of the coordinates of a point.
710
*/
8-
class Point
11+
class Point implements ArrayAccess
912
{
1013
/**
1114
* @var float X coordinate.
@@ -45,4 +48,68 @@ public function getY(): float
4548
{
4649
return $this->y;
4750
}
51+
52+
/**
53+
* Whether an offset exists.
54+
* @param integer|string $offset Use 0 or 1.
55+
* @return boolean
56+
*/
57+
public function offsetExists($offset): bool
58+
{
59+
if ($offset === 0 || $offset === 1) {
60+
return true;
61+
}
62+
return false;
63+
}
64+
65+
/**
66+
* Get an offset value.
67+
* @param integer|string $offset Use 0 or 1.
68+
* @return float
69+
* @throws InvalidArgumentException If the offset is not 0 or 1.
70+
*/
71+
public function offsetGet($offset): float
72+
{
73+
if ($offset === 0) {
74+
return $this->x;
75+
} elseif ($offset === 1) {
76+
return $this->y;
77+
}
78+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
79+
}
80+
81+
/**
82+
* Set an offset value.
83+
* @param integer|string $offset Use 0 or 1.
84+
* @param float|integer|string $value Coordinate value to set.
85+
* @return void
86+
* @throws InvalidArgumentException If the offset is not 0 or 1.
87+
*/
88+
public function offsetSet($offset, $value): void
89+
{
90+
if ($offset === 0) {
91+
$this->x = $value;
92+
} elseif ($offset === 1) {
93+
$this->y = $value;
94+
} else {
95+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
96+
}
97+
}
98+
99+
/**
100+
* Get an offset value.
101+
* @param integer|string $offset Use 0 or 1.
102+
* @return void
103+
* @throws InvalidArgumentException If the offset is not 0 or 1.
104+
*/
105+
public function offsetUnset($offset): void
106+
{
107+
if ($offset === 0) {
108+
unset($this->x);
109+
} elseif ($offset === 1) {
110+
unset($this->y);
111+
} else {
112+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
113+
}
114+
}
48115
}

src/Geometry/Polygon.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Polygon
99
{
1010
/**
11-
* @var array|null Vertices of the polygon.
11+
* @var Point[]|null Vertices of the polygon.
1212
*/
1313
public ?array $coordinates;
1414

@@ -17,7 +17,14 @@ class Polygon
1717
*/
1818
public function __construct(?array $coordinates = null)
1919
{
20-
$this->coordinates = $coordinates;
20+
if (!is_null($coordinates)) {
21+
$this->coordinates = [];
22+
foreach ($coordinates as $point) {
23+
$this->coordinates[] = new Point($point[0], $point[1]);
24+
}
25+
} else {
26+
$this->coordinates = null;
27+
}
2128
}
2229

2330
/**
@@ -30,6 +37,50 @@ public function getCentroid(): Point
3037
return PolygonUtils::getCentroid($this->coordinates);
3138
}
3239

40+
/**
41+
* Retrieves the upper and lower bounds of the y-axis.
42+
*
43+
* @return MinMax
44+
*/
45+
public function getMinMaxY(): MinMax
46+
{
47+
return MinMaxUtils::getMinMaxY($this->coordinates);
48+
}
49+
50+
/**
51+
* Retrieves the upper and lower bounds of the x-axis.
52+
*
53+
* @return MinMax
54+
*/
55+
public function getMinMaxX(): MinMax
56+
{
57+
return MinMaxUtils::getMinMaxX($this->coordinates);
58+
}
59+
60+
/**
61+
* Checks whether a point is located within the polygon's y-axis.
62+
*
63+
* @param Point $point Point to check.
64+
* @return boolean
65+
*/
66+
public function isPointInY(Point $point): bool
67+
{
68+
$minMax = $this->getMinMaxY();
69+
return PolygonUtils::isPointInY($point, $minMax->getMin(), $minMax->getMax());
70+
}
71+
72+
/**
73+
* Checks whether a point is located within the polygon's x-axis.
74+
*
75+
* @param Point $point Point to check.
76+
* @return boolean
77+
*/
78+
public function isPointInX(Point $point): bool
79+
{
80+
$minMax = $this->getMinMaxX();
81+
return PolygonUtils::isPointInX($point, $minMax->getMin(), $minMax->getMax());
82+
}
83+
3384
/**
3485
* Checks whether the Polygon has coordinates.
3586
*
@@ -58,7 +109,6 @@ public function __toString()
58109
if (!$this->isEmpty()) {
59110
return 'Polygon with ' . count($this->getCoordinates()) . ' points.';
60111
}
61-
62112
return '';
63113
}
64114
}

src/Geometry/PolygonUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public static function quadrilateralFromPrediction(array $prediction): Polygon
219219
/**
220220
* Generates a Polygon from a given prediction.
221221
*
222+
* @deprecated construct a new Polygon() instead.
222223
* @param array $prediction Raw prediction array.
223224
* @return Polygon
224225
*/
@@ -228,7 +229,6 @@ public static function polygonFromPrediction(array $prediction): Polygon
228229
foreach ($prediction as $point) {
229230
$points[] = new Point($point[0], $point[1]);
230231
}
231-
232232
return new Polygon($points);
233233
}
234234

src/Http/MindeeApiV2.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,17 @@ private function documentEnqueuePost(
311311
$postFields['file'] = $inputSource->fileObject;
312312
}
313313

314-
if ($params->rag) {
315-
$postFields['rag'] = 'true';
314+
if (isset($params->rawText)) {
315+
$postFields['raw_text'] = $params->rawText ? 'true' : 'false';
316+
}
317+
if (isset($params->polygon)) {
318+
$postFields['polygon'] = $params->polygon ? 'true' : 'false';
319+
}
320+
if (isset($params->confidence)) {
321+
$postFields['confidence'] = $params->confidence ? 'true' : 'false';
322+
}
323+
if (isset($params->rag)) {
324+
$postFields['rag'] = $params->rag ? 'true' : 'false';
316325
}
317326

318327
$url = $this->baseUrl . '/inferences/enqueue';

src/Input/InferenceParameters.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ class InferenceParameters
1313
public string $modelId;
1414

1515
/**
16-
* @var boolean Whether to enable Retrieval-Augmented Generation.
16+
* @var boolean|null Enhance extraction accuracy with Retrieval-Augmented Generation..
1717
*/
18-
public bool $rag;
18+
public ?bool $rag;
19+
20+
/**
21+
* @var boolean|null Extract the full text content from the document as strings.
22+
*/
23+
public ?bool $rawText;
24+
25+
/**
26+
* @var boolean|null Calculate bounding box polygons for all fields.
27+
*/
28+
public ?bool $polygon;
29+
30+
/**
31+
* @var boolean|null Boost the precision and accuracy of all extractions.
32+
* Calculate confidence scores for all fields.
33+
*/
34+
public ?bool $confidence;
1935

2036
/**
2137
* @var string|null Optional file alias.
@@ -40,6 +56,9 @@ class InferenceParameters
4056
/**
4157
* @param string $modelId ID of the model.
4258
* @param boolean|null $rag Whether to enable Retrieval-Augmented Generation.
59+
* @param boolean|null $rawText Whether to extract the full text content from the document as strings.
60+
* @param boolean|null $polygon Whether to calculate bounding box polygons for all fields.
61+
* @param boolean|null $confidence Whether to calculate confidence scores for all fields.
4362
* @param string|null $alias Optional file alias.
4463
* @param array<string>|null $webhooksIds List of webhook IDs.
4564
* @param PollingOptions|null $pollingOptions Polling options.
@@ -48,6 +67,9 @@ class InferenceParameters
4867
public function __construct(
4968
string $modelId,
5069
?bool $rag = null,
70+
?bool $rawText = null,
71+
?bool $polygon = null,
72+
?bool $confidence = null,
5173
?string $alias = null,
5274
?array $webhooksIds = null,
5375
?PollingOptions $pollingOptions = null,
@@ -58,7 +80,11 @@ public function __construct(
5880
$pollingOptions = new PollingOptions();
5981
}
6082
$this->pollingOptions = $pollingOptions;
61-
$this->rag = (bool) $rag;
83+
$this->rag = $rag;
84+
$this->rawText = $rawText;
85+
$this->polygon = $polygon;
86+
$this->confidence = $confidence;
87+
6288
$this->closeFile = (bool) $closeFile;
6389
if (isset($alias)) {
6490
$this->alias = $alias;

src/Parsing/Common/Extras/Extras.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@
1212
class Extras
1313
{
1414
/**
15-
* @var \Mindee\Parsing\Common\Extras\CropperExtra|null Cropper extra.
15+
* @var CropperExtra|null Cropper extra.
1616
*/
1717
public ?CropperExtra $cropper;
1818
/**
19-
* @var \Mindee\Parsing\Common\Extras\CropperExtra|null Full text OCR extra.
19+
* @var FullTextOcrExtra|null Full text OCR extra.
2020
*/
2121
public ?FullTextOcrExtra $fullTextOcr;
2222
/**
23-
* @var \Mindee\Parsing\Common\Extras\RagExtra|null Rag Extra.
23+
* @var RagExtra|null Rag Extra.
2424
*/
2525
public ?RagExtra $rag;
2626
/**
2727
* @var array Other extras.
2828
*/
2929
private array $data;
3030

31-
3231
/**
3332
* Sets a field.
3433
*
35-
* @param mixed $varName Name of the field to set.
36-
* @param mixed $value Value to set the field with.
34+
* @param string $varName Name of the field to set.
35+
* @param mixed $value Value to set the field with.
3736
* @return void
3837
*/
39-
public function __set($varName, $value)
38+
public function __set(string $varName, $value)
4039
{
4140
$this->data[$varName] = $value;
4241
}

src/Parsing/Standard/PositionField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static function getQuadrilateral(array $rawPrediction, string $key): ?Po
5757
private static function getPolygon(array $rawPrediction, string $key): ?Polygon
5858
{
5959
if (array_key_exists($key, $rawPrediction)) {
60-
return PolygonUtils::polygonFromPrediction($rawPrediction[$key]);
60+
return new Polygon($rawPrediction[$key]);
6161
}
6262

6363
return null;

0 commit comments

Comments
 (0)