diff --git a/lib/mindee/v2/product/classification/classification_classifier.rb b/lib/mindee/v2/product/classification/classification_classifier.rb index 288ee311..f5c37e94 100644 --- a/lib/mindee/v2/product/classification/classification_classifier.rb +++ b/lib/mindee/v2/product/classification/classification_classifier.rb @@ -8,10 +8,16 @@ module Classification class ClassificationClassifier # @return [String] The document type, as identified on given classification values. attr_reader :document_type + attr_reader :extraction_response # @param server_response [Hash] Hash representation of the JSON returned by the service. def initialize(server_response) @document_type = server_response['document_type'] + # rubocop:disable Style/GuardClause + unless server_response['extraction_response'].nil? + @extraction_response = V2::Product::Extraction::ExtractionResponse.new(server_response['extraction_response']) + end + # rubocop:enable Style/GuardClause end # @return [String] String representation. diff --git a/lib/mindee/v2/product/crop/crop_item.rb b/lib/mindee/v2/product/crop/crop_item.rb index 74f024d4..71c427dc 100644 --- a/lib/mindee/v2/product/crop/crop_item.rb +++ b/lib/mindee/v2/product/crop/crop_item.rb @@ -10,11 +10,17 @@ class CropItem attr_reader :object_type # @return [V2::Parsing::Field::FieldLocation] Coordinates of the detected object on the document. attr_reader :location + attr_reader :extraction_response # @param server_response [Hash] Hash representation of the JSON returned by the service. def initialize(server_response) @object_type = server_response['object_type'] @location = Mindee::V2::Parsing::Field::FieldLocation.new(server_response['location']) + # rubocop:disable Style/GuardClause + unless server_response['extraction_response'].nil? + @extraction_response = V2::Product::Extraction::ExtractionResponse.new(server_response['extraction_response']) + end + # rubocop:enable Style/GuardClause end # String representation. diff --git a/lib/mindee/v2/product/split/split_range.rb b/lib/mindee/v2/product/split/split_range.rb index f53d81f3..a2bc09e8 100644 --- a/lib/mindee/v2/product/split/split_range.rb +++ b/lib/mindee/v2/product/split/split_range.rb @@ -11,11 +11,17 @@ class SplitRange attr_reader :page_range # @return [String] The document type, as identified on given classification values. attr_reader :document_type + attr_reader :extraction_response # @param server_response [Hash] Hash representation of the JSON returned by the service. def initialize(server_response) @page_range = server_response['page_range'] @document_type = server_response['document_type'] + # rubocop:disable Style/GuardClause + unless server_response['extraction_response'].nil? + @extraction_response = V2::Product::Extraction::ExtractionResponse.new(server_response['extraction_response']) + end + # rubocop:enable Style/GuardClause end # String representation. diff --git a/sig/mindee/v2/product/classification/classification_classifier.rbs b/sig/mindee/v2/product/classification/classification_classifier.rbs index 4a1aafe5..47c2dc45 100644 --- a/sig/mindee/v2/product/classification/classification_classifier.rbs +++ b/sig/mindee/v2/product/classification/classification_classifier.rbs @@ -6,6 +6,7 @@ module Mindee module Classification class ClassificationClassifier attr_reader document_type: String + attr_reader extraction_response: Extraction::ExtractionResponse def initialize: (Hash[String | Symbol, untyped]) -> void end diff --git a/sig/mindee/v2/product/crop/crop_item.rbs b/sig/mindee/v2/product/crop/crop_item.rbs index b87efc6d..d112ea96 100644 --- a/sig/mindee/v2/product/crop/crop_item.rbs +++ b/sig/mindee/v2/product/crop/crop_item.rbs @@ -3,8 +3,9 @@ module Mindee module Product module Crop class CropItem + attr_reader extraction_response: Extraction::ExtractionResponse attr_reader object_type: String - attr_reader location: Mindee::V2::Parsing::Field::FieldLocation + attr_reader location: Parsing::Field::FieldLocation def initialize: (Hash[String | Symbol, untyped]) -> void diff --git a/sig/mindee/v2/product/split/split_range.rbs b/sig/mindee/v2/product/split/split_range.rbs index 7ec193c5..efde2842 100644 --- a/sig/mindee/v2/product/split/split_range.rbs +++ b/sig/mindee/v2/product/split/split_range.rbs @@ -3,6 +3,7 @@ module Mindee module Product module Split class SplitRange + attr_reader extraction_response: Extraction::ExtractionResponse attr_reader page_range: Array[Integer] attr_reader document_type: String diff --git a/spec/data b/spec/data index c2e36f5b..315efcc3 160000 --- a/spec/data +++ b/spec/data @@ -1 +1 @@ -Subproject commit c2e36f5b635386cb9bb922b517c4e02039b0a122 +Subproject commit 315efcc302efacfb75a6b101788058658e8c37e4 diff --git a/spec/v2/file_operations/crop_operation_integration.rb b/spec/v2/file_operations/crop_operation_integration.rb index 6d28bd77..701d2a69 100644 --- a/spec/v2/file_operations/crop_operation_integration.rb +++ b/spec/v2/file_operations/crop_operation_integration.rb @@ -65,7 +65,7 @@ def check_findoc_return(findoc_response) extracted_images.save_all_to_disk(OUTPUT_DIR) - expect(File.size(File.join(OUTPUT_DIR, 'crop_001.jpg'))).to be_between(600_000, 672_913) - expect(File.size(File.join(OUTPUT_DIR, 'crop_002.jpg'))).to be_between(600_000, 675_728) + expect(File.size(File.join(OUTPUT_DIR, 'crop_001.jpg'))).to be_between(560_000, 675_000) + expect(File.size(File.join(OUTPUT_DIR, 'crop_002.jpg'))).to be_between(580_000, 680_000) end end diff --git a/spec/v2/product/classification/classification_integration.rb b/spec/v2/product/classification/classification_integration.rb index f0ac7ebb..f7e14e54 100644 --- a/spec/v2/product/classification/classification_integration.rb +++ b/spec/v2/product/classification/classification_integration.rb @@ -14,7 +14,7 @@ it 'processes classification default sample correctly' do input_source = Mindee::Input::Source::PathInputSource.new( - File.join(V2_PRODUCT_DATA_DIR, 'classification', 'default_invoice.jpg') + File.join(V2_PRODUCT_DATA_DIR, 'classification', 'default_sample.jpg') ) params = { model_id: classification_model_id } @@ -26,7 +26,7 @@ ) expect(response.inference).not_to be_nil - expect(response.inference.file.name).to eq('default_invoice.jpg') + expect(response.inference.file.name).to eq('default_sample.jpg') expect(response.inference.result.classification).not_to be_nil expect(response.inference.result.classification.document_type).to eq('invoice') end diff --git a/spec/v2/product/classification/classification_spec.rb b/spec/v2/product/classification/classification_spec.rb index b27345ae..a2bd5673 100644 --- a/spec/v2/product/classification/classification_spec.rb +++ b/spec/v2/product/classification/classification_spec.rb @@ -6,10 +6,9 @@ describe Mindee::V2::Product::Classification::Classification, :v2 do let(:classification_data_dir) { File.join(V2_PRODUCT_DATA_DIR, 'classification') } - it 'parses a single classification properly' do - json_path = File.join(classification_data_dir, 'classification_single.json') + it 'should load a single result' do + json_path = File.join(classification_data_dir, 'default_sample.json') json_sample = JSON.parse(File.read(json_path)) - response = Mindee::V2::Product::Classification::ClassificationResponse.new(json_sample) expect(response.inference).to be_a(Mindee::V2::Product::Classification::ClassificationInference) @@ -20,4 +19,18 @@ expect(response.inference.result.classification.document_type).to eq('invoice') end + + it 'should load a single result with extraction' do + json_path = File.join(classification_data_dir, 'default_sample_extraction.json') + json_sample = JSON.parse(File.read(json_path)) + response = Mindee::V2::Product::Classification::ClassificationResponse.new(json_sample) + + classification = response.inference.result.classification + expect(classification.document_type).to eq('invoice') + + expect(classification.extraction_response).to be_a(Mindee::V2::Product::Extraction::ExtractionResponse) + expect( + classification.extraction_response.inference.result.fields.get_simple_field('customer_name').value + ).to eq('Jiro Doi') + end end diff --git a/spec/v2/product/crop/crop_spec.rb b/spec/v2/product/crop/crop_spec.rb index 2ab6e1f5..0c828834 100644 --- a/spec/v2/product/crop/crop_spec.rb +++ b/spec/v2/product/crop/crop_spec.rb @@ -8,7 +8,7 @@ describe Mindee::V2::Product::Crop::Crop do let(:crop_data_dir) { File.join(V2_PRODUCT_DATA_DIR, 'crop') } - it 'parses a single crop properly' do + it 'should load a single result' do json_path = File.join(crop_data_dir, 'crop_single.json') rst_path = File.join(crop_data_dir, 'crop_single.rst') @@ -37,7 +37,7 @@ expect(response.to_s).to eq(rst_sample) end - it 'parses multiple crops properly' do + it 'should load multiple results' do json_path = File.join(crop_data_dir, 'crop_multiple.json') rst_path = File.join(crop_data_dir, 'crop_multiple.rst') @@ -52,35 +52,65 @@ expect(response.inference.result.crops.size).to eq(2) # First Crop assertions - crop_zero = response.inference.result.crops[0] - expect(crop_zero.location.polygon.size).to eq(4) - expect(crop_zero.location.polygon[0][0]).to eq(0.214) - expect(crop_zero.location.polygon[0][1]).to eq(0.079) - expect(crop_zero.location.polygon[1][0]).to eq(0.476) - expect(crop_zero.location.polygon[1][1]).to eq(0.079) - expect(crop_zero.location.polygon[2][0]).to eq(0.476) - expect(crop_zero.location.polygon[2][1]).to eq(0.979) - expect(crop_zero.location.polygon[3][0]).to eq(0.214) - expect(crop_zero.location.polygon[3][1]).to eq(0.979) - - expect(crop_zero.location.page).to eq(0) - expect(crop_zero.object_type).to eq('invoice') + crop0 = response.inference.result.crops[0] + expect(crop0.location.polygon.size).to eq(4) + expect(crop0.location.polygon[0][0]).to eq(0.214) + expect(crop0.location.polygon[0][1]).to eq(0.079) + expect(crop0.location.polygon[1][0]).to eq(0.476) + expect(crop0.location.polygon[1][1]).to eq(0.079) + expect(crop0.location.polygon[2][0]).to eq(0.476) + expect(crop0.location.polygon[2][1]).to eq(0.979) + expect(crop0.location.polygon[3][0]).to eq(0.214) + expect(crop0.location.polygon[3][1]).to eq(0.979) + + expect(crop0.location.page).to eq(0) + expect(crop0.object_type).to eq('invoice') # Second Crop assertions - crop_one = response.inference.result.crops[1] - expect(crop_one.location.polygon.size).to eq(4) - expect(crop_one.location.polygon[0][0]).to eq(0.547) - expect(crop_one.location.polygon[0][1]).to eq(0.15) - expect(crop_one.location.polygon[1][0]).to eq(0.862) - expect(crop_one.location.polygon[1][1]).to eq(0.15) - expect(crop_one.location.polygon[2][0]).to eq(0.862) - expect(crop_one.location.polygon[2][1]).to eq(0.97) - expect(crop_one.location.polygon[3][0]).to eq(0.547) - expect(crop_one.location.polygon[3][1]).to eq(0.97) - - expect(crop_one.location.page).to eq(0) - expect(crop_one.object_type).to eq('invoice') + crop1 = response.inference.result.crops[1] + expect(crop1.location.polygon.size).to eq(4) + expect(crop1.location.polygon[0][0]).to eq(0.547) + expect(crop1.location.polygon[0][1]).to eq(0.15) + expect(crop1.location.polygon[1][0]).to eq(0.862) + expect(crop1.location.polygon[1][1]).to eq(0.15) + expect(crop1.location.polygon[2][0]).to eq(0.862) + expect(crop1.location.polygon[2][1]).to eq(0.97) + expect(crop1.location.polygon[3][0]).to eq(0.547) + expect(crop1.location.polygon[3][1]).to eq(0.97) + + expect(crop1.location.page).to eq(0) + expect(crop1.object_type).to eq('receipt') expect(response.to_s).to eq(rst_sample) end + + it 'should load extraction properties' do + json_path = File.join(crop_data_dir, 'default_sample_extraction.json') + json_sample = JSON.parse(File.read(json_path)) + response = Mindee::V2::Product::Crop::CropResponse.new(json_sample) + + expect(response.inference).to be_a(Mindee::V2::Product::Crop::CropInference) + crops = response.inference.result.crops + expect(response.inference.result.crops.size).to eq(2) + + crop0 = crops[0] + expect(crop0.object_type).to eq('receipt') + expect(crop0.location.polygon).not_to be_nil + expect(crop0.location.page).to eq(0) + extraction_response0 = crop0.extraction_response + expect(extraction_response0).not_to be_nil + expect( + extraction_response0.inference.result.fields.get_simple_field('supplier_name').value + ).to eq('CHEZ ALAIN MIAM MIAM') + + crop1 = crops[1] + expect(crop1.object_type).to eq('receipt') + expect(crop1.location.polygon).not_to be_nil + expect(crop1.location.page).to eq(0) + extraction_response1 = crop1.extraction_response + expect(extraction_response1).not_to be_nil + expect( + extraction_response1.inference.result.fields.get_simple_field('supplier_name').value + ).to eq('La cerise sur la pizza') + end end diff --git a/spec/v2/product/split/split_spec.rb b/spec/v2/product/split/split_spec.rb index e946ee28..a423738e 100644 --- a/spec/v2/product/split/split_spec.rb +++ b/spec/v2/product/split/split_spec.rb @@ -24,7 +24,6 @@ it 'parses multiple splits properly' do json_path = File.join(split_data_dir, 'split_multiple.json') json_sample = JSON.parse(File.read(json_path)) - response = Mindee::V2::Product::Split::SplitResponse.new(json_sample) expect(response.inference).to be_a(Mindee::V2::Product::Split::SplitInference) @@ -32,22 +31,51 @@ expect(response.inference.result.splits[0]).to be_a(Mindee::V2::Product::Split::SplitRange) expect(response.inference.result.splits.size).to eq(3) - split_zero = response.inference.result.splits[0] - expect(split_zero.page_range.size).to eq(2) - expect(split_zero.page_range[0]).to eq(0) - expect(split_zero.page_range[1]).to eq(0) - expect(split_zero.document_type).to eq('invoice') - - split_one = response.inference.result.splits[1] - expect(split_one.page_range.size).to eq(2) - expect(split_one.page_range[0]).to eq(1) - expect(split_one.page_range[1]).to eq(3) - expect(split_one.document_type).to eq('invoice') - - split_two = response.inference.result.splits[2] - expect(split_two.page_range.size).to eq(2) - expect(split_two.page_range[0]).to eq(4) - expect(split_two.page_range[1]).to eq(4) - expect(split_two.document_type).to eq('invoice') + split0 = response.inference.result.splits[0] + expect(split0.page_range.size).to eq(2) + expect(split0.page_range[0]).to eq(0) + expect(split0.page_range[1]).to eq(0) + expect(split0.document_type).to eq('passport') + + split1 = response.inference.result.splits[1] + expect(split1.page_range.size).to eq(2) + expect(split1.page_range[0]).to eq(1) + expect(split1.page_range[1]).to eq(3) + expect(split1.document_type).to eq('invoice') + + split2 = response.inference.result.splits[2] + expect(split2.page_range.size).to eq(2) + expect(split2.page_range[0]).to eq(4) + expect(split2.page_range[1]).to eq(4) + expect(split2.document_type).to eq('receipt') + end + + it 'should load extraction properties' do + json_path = File.join(split_data_dir, 'default_sample_extraction.json') + json_sample = JSON.parse(File.read(json_path)) + response = Mindee::V2::Product::Split::SplitResponse.new(json_sample) + + expect(response.inference).to be_a(Mindee::V2::Product::Split::SplitInference) + + splits = response.inference.result.splits + expect(splits.size).to eq(2) + + split0 = splits[0] + expect(split0.document_type).to eq('invoice') + expect(split0.page_range[0]).to eq(0) + extraction_response0 = split0.extraction_response + expect(extraction_response0).not_to be_nil + expect( + extraction_response0.inference.result.fields.get_simple_field('supplier_phone_number').value + ).to eq('05 05 44 44 90') + + split1 = splits[1] + expect(split1.document_type).to eq('invoice') + expect(split1.page_range[0]).to eq(1) + extraction_response1 = split1.extraction_response + expect(extraction_response1).not_to be_nil + expect( + extraction_response1.inference.result.fields.get_simple_field('supplier_phone_number').value + ).to eq('416-555-1212') end end