Skip to content

Commit 6f10715

Browse files
authored
🐛 fix polygon handling (#207)
1 parent ecea861 commit 6f10715

File tree

9 files changed

+31
-33
lines changed

9 files changed

+31
-33
lines changed

lib/mindee/geometry/polygon.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ module Mindee
55
module Geometry
66
# Contains any number of vertex coordinates (Points).
77
class Polygon < Array
8+
# @param server_response [Hash] Raw server response hash.
9+
def initialize(server_response)
10+
points = []
11+
server_response.map do |point|
12+
points << Point.new(point[0], point[1])
13+
end
14+
super(points)
15+
end
16+
817
# Get the central point (centroid) of the polygon.
918
# @return [Mindee::Geometry::Point]
1019
def centroid

lib/mindee/geometry/utils.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ def self.quadrilateral_from_prediction(prediction)
1717
)
1818
end
1919

20-
# Transform a prediction into a Polygon.
21-
# @param prediction [Hash]
22-
# @return [Mindee::Geometry::Polygon]
23-
def self.polygon_from_prediction(prediction)
24-
polygon = Polygon.new
25-
return polygon if prediction.nil?
26-
27-
prediction.each do |point|
28-
polygon << Point.new(point[0], point[1])
29-
end
30-
polygon
31-
end
32-
3320
# Gets the points of a bounding box for a given set of points
3421
# @param vertices [Array<Mindee::Geometry::Point>]
3522
# @return [Array<Float>]
@@ -39,7 +26,7 @@ def self.get_bbox(vertices)
3926
[x_coords.min, y_coords.min, x_coords.max, y_coords.max]
4027
end
4128

42-
# Creates the bounding bounding box for a given set of points
29+
# Creates the bounding box for a given set of points
4330
# @param vertices [Array<Mindee::Geometry::Point>]
4431
# @return [Mindee::Geometry::Quadrilateral]
4532
def self.get_bounding_box(vertices)

lib/mindee/parsing/common/ocr/ocr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class OCRWord
2323
def initialize(prediction)
2424
@text = prediction['text']
2525
@confidence = prediction['confidence']
26-
@polygon = Geometry.polygon_from_prediction(prediction['polygon'])
26+
@polygon = Mindee::Geometry::Polygon.new(prediction['polygon'])
2727
@bounding_box = Geometry.get_bounding_box(@polygon) unless @polygon.nil? || @polygon.empty?
2828
end
2929

lib/mindee/parsing/standard/abstract_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AbstractField
2121
# @param page_id [Integer, nil]
2222
def initialize(prediction, page_id)
2323
@confidence = prediction['confidence'] if prediction.key?('confidence')
24-
@polygon = Geometry.polygon_from_prediction(prediction['polygon']) if prediction.key?('polygon')
24+
@polygon = Mindee::Geometry::Polygon.new(prediction['polygon']) if prediction.key?('polygon')
2525
@bounding_box = Geometry.get_bounding_box(@polygon) unless @polygon.nil? || @polygon.empty?
2626
@page_id = page_id || prediction['page_id']
2727
end

lib/mindee/parsing/standard/position_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PositionField
2020
# @param page_id [Integer, nil]
2121
def initialize(prediction, page_id)
2222
unless prediction['polygon'].nil? || prediction['polygon'].empty?
23-
@polygon = Geometry.polygon_from_prediction(prediction['polygon'])
23+
@polygon = Mindee::Geometry::Polygon.new(prediction['polygon'])
2424
end
2525
@quadrangle = to_quadrilateral(prediction, 'quadrangle')
2626
@rectangle = to_quadrilateral(prediction, 'rectangle')

lib/mindee/parsing/v2/field/field_location.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ class FieldLocation
1616

1717
# @param server_response [Hash] Raw server response hash.
1818
def initialize(server_response)
19-
polygon_data = server_response['polygon'] || server_response[:polygon]
20-
@polygon = polygon_data ? Mindee::Geometry::Polygon.new(polygon_data) : nil
21-
22-
page_id = server_response['page'] || server_response[:page]
23-
@page = page_id.is_a?(Float) || page_id.is_a?(Integer) ? page_id.to_i : nil
19+
@polygon = Mindee::Geometry::Polygon.new(server_response['polygon'])
20+
@page = server_response['page']
2421
end
2522

2623
# String representation of the polygon (empty string when none).

sig/mindee/geometry/polygon.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module Mindee
33
module Geometry
44
class Polygon < Array[Point]
5+
def initialize: (Array[::Mindee::Geometry::Point | Array[Float]]) -> void
56
def centroid: -> Point
67
def point_in_y?: (Point) -> bool
78
end

sig/mindee/parsing/v2/field/field_location.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Mindee
44
module V2
55
module Field
66
class FieldLocation
7-
attr_reader page: Integer?
8-
attr_reader polygon: Geometry::Polygon?
7+
attr_reader page: Integer
8+
attr_reader polygon: Geometry::Polygon
99

1010
def initialize: (Hash[String | Symbol, untyped]) -> void
1111
def to_s: -> String

spec/parsing/v2/inference_spec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,23 @@ def load_standard_fields
308308
expect(date_field.locations[0].page).to eq(0)
309309

310310
polygon = date_field.locations[0].polygon
311-
expect(polygon[0].length).to eq(2)
311+
expect(polygon[0]).to be_a(Mindee::Geometry::Point)
312312

313-
expect(polygon[0][0]).to be_within(1e-12).of(0.948979073166918)
314-
expect(polygon[0][1]).to be_within(1e-12).of(0.23097924535067715)
313+
expect(polygon[0].x).to eq(0.948979073166918)
314+
expect(polygon[0].y).to eq(0.23097924535067715)
315315

316-
expect(polygon[1][0]).to be_within(1e-12).of(0.85422)
317-
expect(polygon[1][1]).to be_within(1e-12).of(0.230072)
316+
expect(polygon[1][0]).to eq(0.85422)
317+
expect(polygon[1][1]).to eq(0.230072)
318318

319-
expect(polygon[2][0]).to be_within(1e-12).of(0.8540899268330819)
320-
expect(polygon[2][1]).to be_within(1e-12).of(0.24365775464932288)
319+
expect(polygon[2][0]).to eq(0.8540899268330819)
320+
expect(polygon[2][1]).to eq(0.24365775464932288)
321321

322-
expect(polygon[3][0]).to be_within(1e-12).of(0.948849)
323-
expect(polygon[3][1]).to be_within(1e-12).of(0.244565)
322+
expect(polygon[3][0]).to eq(0.948849)
323+
expect(polygon[3][1]).to eq(0.244565)
324+
325+
centroid = polygon.centroid
326+
expect(centroid.x).to eq(0.9015345)
327+
expect(centroid.y).to eq(0.23731850000000002)
324328

325329
confidence = date_field.confidence
326330
expect(confidence).to be_a(field_confidence)

0 commit comments

Comments
 (0)