|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mindee |
| 4 | + module Ocr |
| 5 | + # A single word. |
| 6 | + class OcrWord |
| 7 | + # The confidence score, value will be between 0.0 and 1.0 |
| 8 | + # @return [Float] |
| 9 | + attr_accessor :confidence |
| 10 | + # @return [String] |
| 11 | + attr_reader :text |
| 12 | + # @return [Mindee::Geometry::Quadrilateral] |
| 13 | + attr_reader :bounding_box |
| 14 | + # @return [Mindee::Geometry::Polygon] |
| 15 | + attr_reader :polygon |
| 16 | + |
| 17 | + # @param prediction [Hash] |
| 18 | + def initialize(prediction) |
| 19 | + @text = prediction['text'] |
| 20 | + @confidence = prediction['confidence'] |
| 21 | + @polygon = Geometry.polygon_from_prediction(prediction['polygon']) |
| 22 | + @bounding_box = Geometry.get_bounding_box(@polygon) unless @polygon.nil? || @polygon.empty? |
| 23 | + end |
| 24 | + |
| 25 | + def to_s |
| 26 | + @text.to_s |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + # A list of words which are on the same line. |
| 31 | + class OcrLine < Array |
| 32 | + # @param prediction [Hash, nil] |
| 33 | + # @param array [Array, nil] |
| 34 | + # @param page_id [Integer, nil] |
| 35 | + def initialize(prediction = nil, from_array = nil) |
| 36 | + if !prediction.nil? |
| 37 | + super(prediction.map { |word_prediction| OcrWord.new(word_prediction) }) |
| 38 | + elsif !from_array.nil? |
| 39 | + super(from_array) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + # Sort the words on the line from left to right. |
| 44 | + def sort_on_x |
| 45 | + from_array = sort do |word1, word2| |
| 46 | + Geometry.get_min_max_x(word1.polygon).min <=> Geometry.get_min_max_x(word2.polygon).min |
| 47 | + end |
| 48 | + OcrLine.new(nil, from_array) |
| 49 | + end |
| 50 | + |
| 51 | + def to_s |
| 52 | + each(&:to_s).join(' ') |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + # OCR extraction for a single page. |
| 57 | + class OcrPage |
| 58 | + # All the words on the page, in semi-random order. |
| 59 | + # @param all_words [Array<OcrWord>] |
| 60 | + attr_reader :all_words |
| 61 | + # @param lines [Array<OcrLines>] |
| 62 | + attr_reader :lines |
| 63 | + |
| 64 | + def initialize(prediction) |
| 65 | + @lines = [] |
| 66 | + @all_words = [] |
| 67 | + prediction['all_words'].each do |word_prediction| |
| 68 | + @all_words.push(OcrWord.new(word_prediction)) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + # All the words on the page, ordered in lines. |
| 73 | + # @return [Array<OcrLine>] |
| 74 | + def all_lines |
| 75 | + @lines = to_lines if @lines.empty? |
| 76 | + @lines |
| 77 | + end |
| 78 | + |
| 79 | + def to_s |
| 80 | + lines = all_lines |
| 81 | + return '' if lines.empty? |
| 82 | + |
| 83 | + out_str = String.new |
| 84 | + lines.map do |line| |
| 85 | + out_str << "#{line}\n" unless line.to_s.strip.empty? |
| 86 | + end |
| 87 | + out_str.strip |
| 88 | + end |
| 89 | + |
| 90 | + private |
| 91 | + |
| 92 | + # Helper function that iterates through all the words and compares them to a candidate |
| 93 | + # @param sorted_words [Array<OcrWord>] |
| 94 | + # @param current [OcrWord] |
| 95 | + # @param indexes [Array<Integer>] |
| 96 | + # @param current [Array<OcrLine>] |
| 97 | + def parse_one(sorted_words, current, indexes, lines) |
| 98 | + line = OcrLine.new([]) |
| 99 | + sorted_words.each_with_index do |word, idx| |
| 100 | + next if indexes.include?(idx) |
| 101 | + |
| 102 | + if current.nil? |
| 103 | + current = word |
| 104 | + indexes.push(idx) |
| 105 | + line = OcrLine.new([]) |
| 106 | + line.push(word) |
| 107 | + elsif words_on_same_line?(current, word) |
| 108 | + line.push(word) |
| 109 | + indexes.push(idx) |
| 110 | + end |
| 111 | + end |
| 112 | + lines.push(line.sort_on_x) if line.any? |
| 113 | + end |
| 114 | + |
| 115 | + # Order all the words on the page into lines. |
| 116 | + # @param current [OcrWord, nil] |
| 117 | + # @param indexes [Array<Integer>] |
| 118 | + # @param lines [Array<OcrLine>] |
| 119 | + # @return [Array<OcrLine>] |
| 120 | + def to_lines |
| 121 | + current = nil |
| 122 | + indexes = [] |
| 123 | + lines = [] |
| 124 | + |
| 125 | + # make sure words are sorted from top to bottom |
| 126 | + all_words = @all_words.sort_by { |word| Geometry.get_min_max_y(word.polygon).min } |
| 127 | + all_words.each do |
| 128 | + parse_one(all_words, current, indexes, lines) |
| 129 | + current = nil |
| 130 | + end |
| 131 | + lines |
| 132 | + end |
| 133 | + |
| 134 | + # Determine if two words are on the same line. |
| 135 | + # @param current_word [Mindee::Ocr::OcrWord] |
| 136 | + # @param next_word [Mindee::Ocr::OcrWord] |
| 137 | + # @return Boolean |
| 138 | + def words_on_same_line?(current_word, next_word) |
| 139 | + current_in_next = current_word.polygon.point_in_y?(next_word.polygon.centroid) |
| 140 | + next_in_current = next_word.polygon.point_in_y?(current_word.polygon.centroid) |
| 141 | + current_in_next || next_in_current |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + # Mindee Vision V1. |
| 146 | + class MVisionV1 |
| 147 | + # List of pages. |
| 148 | + # @param pages [Array<OcrPage>] |
| 149 | + attr_reader :pages |
| 150 | + |
| 151 | + def initialize(prediction) |
| 152 | + @pages = [] |
| 153 | + prediction['pages'].each do |page_prediction| |
| 154 | + @pages.push(OcrPage.new(page_prediction)) |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + def to_s |
| 159 | + out_str = String.new |
| 160 | + @pages.map do |page| |
| 161 | + out_str << "\n" |
| 162 | + out_str << page.to_s |
| 163 | + end |
| 164 | + out_str.strip |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + # OCR extraction from the entire document. |
| 169 | + class Ocr |
| 170 | + # Mindee Vision v1 results. |
| 171 | + # @return [Mindee::Ocr::MVisionV1] |
| 172 | + attr_reader :mvision_v1 |
| 173 | + |
| 174 | + def initialize(prediction) |
| 175 | + @mvision_v1 = MVisionV1.new(prediction['mvision-v1']) |
| 176 | + end |
| 177 | + |
| 178 | + def to_s |
| 179 | + @mvision_v1.to_s |
| 180 | + end |
| 181 | + end |
| 182 | + end |
| 183 | +end |
0 commit comments