Class: TextRoi

Inherits:
Roi show all
Defined in:
lib/roi/text/text_roi.rb

Overview

The TextRoi class defines a text Roi and its methods

Instance Attribute Summary collapse

Attributes inherited from Roi

#height, #name, #ref_img, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Roi

#find_frame_displayed, #find_frame_not_displayed, #precise_time_to_change, #precise_time_to_disappear, #precise_time_to_display, #precise_time_to_match_dut, #precise_time_to_transition, #press_key_until_displayed?, #rectangle, #rectangle=, #time_to_change, #time_to_disappear, #time_to_display, #time_to_transition, #wait_for_change?, #wait_for_transition?, #wait_until_displayed?, #wait_until_not_displayed?

Methods included from CanHaveSpecialMethods

included

Constructor Details

#initialize(dut, args = {}) ⇒ TextRoi

Public: Initializes a text Roi.

dut - Platform (or subclass) instance to which this Roi belongs. x - Integer x coordinate (default: nil). y - Integer y coordinate (default: nil). width - Integer width (default: nil). height - Integer height (default: nil). rectangle - Hash defining rectangle with keys :x, :y, :width, :height (default: nil). element - Hash same as rectangle (default: nil). text - String expected text (default: ”). similarity - Integer text similarity threshold 0 - 100 (default: 70). ref_img - String path to reference image (default: nil). only_digits - Boolean indicating whether to convert all read characters to digits (default: false). substring - Boolean indicating whether to treat the expected text as a substring (default: false). ignore_spaces - Boolean indicating whether spaces should be ignored when verifying text (default: false). ignore_case - Boolean indicating whether case should be ignored when verifying text (default: false). ignore_special_chars - Boolean indicating whether special characters should be ignored when verifying text (default: false).

Returns nothing.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roi/text/text_roi.rb', line 29

def initialize(dut, args={})
  super(dut, args)
  self.text = args.fetch(:text, '')
  @preprocessing = [0]
  @only_digits = args.fetch(:only_digits, false)
  @substring = args.fetch(:substring, false)
  @ignore_spaces = args.fetch(:ignore_spaces, true)
  @ignore_case = args.fetch(:ignore_case, false)
  @ignore_special_chars = args.fetch(:ignore_special_chars, false)
  @similarity = args.fetch(:similarity, 70)
end

Instance Attribute Details

#ignore_caseObject

Returns the value of attribute ignore_case.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def ignore_case
  @ignore_case
end

#ignore_spacesObject

Returns the value of attribute ignore_spaces.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def ignore_spaces
  @ignore_spaces
end

#ignore_special_charsObject

Returns the value of attribute ignore_special_chars.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def ignore_special_chars
  @ignore_special_chars
end

#only_digitsObject

Returns the value of attribute only_digits.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def only_digits
  @only_digits
end

#similarityObject

Returns the value of attribute similarity.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def similarity
  @similarity
end

#substringObject

Returns the value of attribute substring.



8
9
10
# File 'lib/roi/text/text_roi.rb', line 8

def substring
  @substring
end

#textObject

Public: Gets expected text.

Returns a String expected text.



53
54
55
# File 'lib/roi/text/text_roi.rb', line 53

def text
  @text
end

Instance Method Details

#displayed?(args = {}) ⇒ Boolean

Public: Checks if text is displayed as defined in the given ROI.

Corresponds to api_is_text_displayed?

key - String name of key to press to trigger evaluation (default: nil).

If default, no keys will be pressed.

delay - Integer total milliseconds to delay before starting evaluation (default: 0). timeout - Integer total milliseconds to allow before timing out (default: 0). verifies_for - Integer total milliseconds for which the ROI must verify in order to succeed (default: 0).

If default, the ROI must only verify once.

only_digits - Boolean indicating whether to convert all read characters to digits (default: false). substring - Boolean indicating whether to treat the expected text as a substring (default: false). ignore_spaces - Boolean indicating whether spaces should be ignored when verifying text (default: false). ignore_case - Boolean indicating whether case should be ignored when verifying text (default: false). ignore_special_chars - Boolean indicating whether special characters should be ignored when verifying text (default: false). priority - Symbol representing evaluation priority used to throttle CPU usage (default: :normal):

:critical   - Sleep 10 milliseconds between intensive tasks (should be used sparingly).
:high       - Sleep 100 milliseconds between intensive tasks.
:normal     - Sleep 1 second between intensive tasks (default).
:low        - Sleep 10 seconds between intensive tasks.
:background - Sleep 1 minute between intensive tasks (should be used sparingly).

log_every - Integer interval in milliseconds between logs (default: 1000).

Returns a Boolean true if text is displayed before the timeout, otherwise false.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/roi/text/text_roi.rb', line 87

def displayed?(args={})
  logger.info('Checking if text is displayed')
  raise 'Text is required!' if @text.nil?
  frame = args[:frame]
  verify(args) do
    # Evaluate each OCR Roi object (one for each preprocessing option)

    text_found = false
    roi_options = get_roi_options(args.fetch(:set_roi_options, args))
    get_roi_objects(frame: frame).each do |roi|
      got_text = get_value(roi, frame.nil?)
      text_found = false
      @text.each do |txt|
        if args.fetch(:substring, @substring)
          sim = test_case.get_substring_similarity(txt, got_text, roi_options)
        else
          sim = test_case.get_string_similarity(txt, got_text, roi_options)
        end
        text_found = sim >= (@similarity / 100.0)
        logger.info("Comparing #{got_text.inspect} with #{txt.inspect}, similarity: #{(sim * 100.0).round}%")
        break if text_found
      end
      break if text_found
    end
    logger.roi(self, args.merge(message: "Text is#{text_found ? '' : ' not'} displayed", screenshot: frame.nil?,
                                use_last_image: frame.nil?))
    text_found
  end
end

#infoObject

Public: Gets text Roi info.

Returns a tidy String with text Roi info.



60
61
62
# File 'lib/roi/text/text_roi.rb', line 60

def info
  "#{super} Text=#{@text} Digits=#{@only_digits} Similarity=#{@similarity}"
end

#retrieve(args = {}) ⇒ Object

Public: Gets the text in the region defined in the Roi.

Corresponds to: api_read_text_from_screen

Returns the String text found.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/roi/text/text_roi.rb', line 121

def retrieve(args={})
  logger.info('Retrieving text')
  frame = args[:frame]
  value(args) do
    # Read the text for each preprocessing value

    found_text = get_roi_objects(frame: frame).map do |roi|
      get_value(roi, frame.nil?)
    end
    # Return the text with the most matches

    matches = found_text.map { |txt| txt.empty? ? 0 : found_text.count(txt) }
    index_of_max_matches = matches.index(matches.max)
    best_match = found_text[index_of_max_matches]
    confidence = ((found_text.count(best_match).to_f / found_text.size) * 100).round
    logger.roi(self, args.merge(message: "Retrieved text #{best_match.inspect} with confidence #{confidence}%",
                                screenshot: frame.nil?, use_last_image: frame.nil?))
    best_match
  end
end