|
| 1 | +require 'base64' |
| 2 | + |
| 3 | +module Appium |
| 4 | + module Core |
| 5 | + module Device |
| 6 | + module ImageComparison |
| 7 | + extend Forwardable |
| 8 | + |
| 9 | + MODE = [:matchFeatures, :getSimilarity, :matchTemplate].freeze |
| 10 | + |
| 11 | + MATCH_FEATURES = { |
| 12 | + detector_name: %w(AKAZE AGAST BRISK FAST GFTT KAZE MSER SIFT ORB), |
| 13 | + match_func: %w(FlannBased BruteForce BruteForceL1 BruteForceHamming BruteForceHammingLut BruteForceSL2), |
| 14 | + goodMatchesFactor: nil, # Integer |
| 15 | + visualize: [true, false] |
| 16 | + }.freeze |
| 17 | + |
| 18 | + MATCH_TEMPLATE = { |
| 19 | + visualize: [true, false] |
| 20 | + }.freeze |
| 21 | + |
| 22 | + GET_SIMILARITY = { |
| 23 | + visualize: [true, false] |
| 24 | + }.freeze |
| 25 | + |
| 26 | + # @!method match_images_features(first_image:, second_image:, detector_name: 'ORB', |
| 27 | + # match_func: 'BruteForce', good_matches_factor: 100, visualize: false) |
| 28 | + # Performs images matching by features with default options. Read https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html |
| 29 | + # for more details on this topic. |
| 30 | + # |
| 31 | + # @param [String] first_image An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 32 | + # @param [String] second_image An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 33 | + # @param [String] detector_name Sets the detector name for features matching |
| 34 | + # algorithm. Some of these detectors (FAST, AGAST, GFTT, FAST, SIFT and MSER) are |
| 35 | + # not available in the default OpenCV installation and have to be enabled manually |
| 36 | + # before library compilation. The default detector name is 'ORB'. |
| 37 | + # @param [String] match_func The name of the matching function. The default one is 'BruteForce'. |
| 38 | + # @param [String] good_matches_factor The maximum count of "good" matches (e. g. with minimal distances). |
| 39 | + # The default one is nil. |
| 40 | + # @param [Bool] visualise Makes the endpoint to return an image, which contains the visualized result of |
| 41 | + # the corresponding picture matching operation. This option is disabled by default. |
| 42 | + # |
| 43 | + # @example |
| 44 | + # @driver.match_images_features first_image: "image data 1", second_image: "image data 2" |
| 45 | + # |
| 46 | + # visual = @@driver.match_images_features first_image: image1, second_image: image2, visualize: true |
| 47 | + # File.write 'match_images_visual.png', Base64.decode64(visual['visualization']) # if the image is PNG |
| 48 | + # |
| 49 | + |
| 50 | + # @!method find_image_occurrence(full_image:, partial_image:, detector_name: 'ORB', visualize: false) |
| 51 | + # Performs images matching by template to find possible occurrence of the partial image |
| 52 | + # in the full image with default options. Read https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html |
| 53 | + # for more details on this topic. |
| 54 | + # |
| 55 | + # @param [String] full_image: A full image data. |
| 56 | + # @param [String] partial_image: A partial image data. All image formats, that OpenCV library itself accepts, |
| 57 | + # are supported. |
| 58 | + # @param [Bool] visualise: Makes the endpoint to return an image, which contains the visualized result of |
| 59 | + # the corresponding picture matching operation. This option is disabled by default. |
| 60 | + # |
| 61 | + # @example |
| 62 | + # @driver.find_image_occurrence full_image: "image data 1", partial_image: "image data 2" |
| 63 | + # |
| 64 | + # visual = @@driver.find_image_occurrence full_image: image1, partial_image: image2, visualize: true |
| 65 | + # File.write 'find_result_visual.png', Base64.decode64(visual['visualization']) # if the image is PNG |
| 66 | + # |
| 67 | + |
| 68 | + # @!method get_images_similarity(first_image:, second_image:, detector_name: 'ORB', visualize: false) |
| 69 | + # Performs images matching to calculate the similarity score between them |
| 70 | + # with default options. The flow there is similar to the one used in `find_image_occurrence` |
| 71 | + # but it is mandatory that both images are of equal size. |
| 72 | + # |
| 73 | + # @param [String] first_image: An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 74 | + # @param [String] second_image: An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 75 | + # @param [Bool] visualise: Makes the endpoint to return an image, which contains the visualized result of |
| 76 | + # the corresponding picture matching operation. This option is disabled by default. |
| 77 | + # |
| 78 | + # @example |
| 79 | + # @driver.get_images_similarity first_image: "image data 1", second_image: "image data 2" |
| 80 | + # |
| 81 | + # visual = @@driver.get_images_similarity first_image: image1, second_image: image2, visualize: true |
| 82 | + # File.write 'images_similarity_visual.png', Base64.decode64(visual['visualization']) # if the image is PNG |
| 83 | + # |
| 84 | + |
| 85 | + # @!method compare_images(mode:, first_image:, second_image:, options:) |
| 86 | + # |
| 87 | + # Performs images comparison using OpenCV framework features. |
| 88 | + # It is expected that both OpenCV framework and opencv4nodejs |
| 89 | + # module are installed on the machine where Appium server is running. |
| 90 | + # |
| 91 | + # @param [Symbol] mode: One of possible comparison modes: `:matchFeatures`, `:getSimilarity`, `:matchTemplate`. |
| 92 | + # `:matchFeatures is by default. |
| 93 | + # @param [String] first_image: An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 94 | + # @param [String] second_image: An image data. All image formats, that OpenCV library itself accepts, are supported. |
| 95 | + # @param [Hash] options: The content of this dictionary depends on the actual `mode` value. |
| 96 | + # See the documentation on `appium-support` module for more details. |
| 97 | + # @returns [Hash] The content of the resulting dictionary depends on the actual `mode` and `options` values. |
| 98 | + # See the documentation on `appium-support` module for more details. |
| 99 | + # |
| 100 | + |
| 101 | + #### |
| 102 | + ## class << self |
| 103 | + #### |
| 104 | + |
| 105 | + def self.extended |
| 106 | + ::Appium::Core::Device.add_endpoint_method(:match_images_features) do |
| 107 | + def match_images_features(first_image:, # rubocop:disable Metrics/ParameterLists |
| 108 | + second_image:, |
| 109 | + detector_name: 'ORB', |
| 110 | + match_func: 'BruteForce', |
| 111 | + good_matches_factor: nil, |
| 112 | + visualize: false) |
| 113 | + unless ::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:detector_name].member?(detector_name.to_s) |
| 114 | + raise "detector_name should be #{::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:detector_name]}" |
| 115 | + end |
| 116 | + unless ::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:match_func].member?(match_func.to_s) |
| 117 | + raise "match_func should be #{::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:match_func]}" |
| 118 | + end |
| 119 | + unless ::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:visualize].member?(visualize) |
| 120 | + raise "visualize should be #{::Appium::Core::Device::ImageComparison::MATCH_FEATURES[:visualize]}" |
| 121 | + end |
| 122 | + |
| 123 | + options = {} |
| 124 | + options[:detectorName] = detector_name.to_s.upcase |
| 125 | + options[:matchFunc] = match_func.to_s |
| 126 | + options[:goodMatchesFactor] = good_matches_factor.to_i unless good_matches_factor.nil? |
| 127 | + options[:visualize] = visualize |
| 128 | + |
| 129 | + compare_images(mode: :matchFeatures, first_image: first_image, second_image: second_image, options: options) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + ::Appium::Core::Device.add_endpoint_method(:find_image_occurrence) do |
| 134 | + def find_image_occurrence(full_image:, partial_image:, visualize: false) |
| 135 | + unless ::Appium::Core::Device::ImageComparison::MATCH_TEMPLATE[:visualize].member?(visualize) |
| 136 | + raise "visualize should be #{::Appium::Core::Device::ImageComparison::MATCH_TEMPLATE[:visualize]}" |
| 137 | + end |
| 138 | + |
| 139 | + options = {} |
| 140 | + options[:visualize] = visualize |
| 141 | + |
| 142 | + compare_images(mode: :matchTemplate, first_image: full_image, second_image: partial_image, options: options) |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + ::Appium::Core::Device.add_endpoint_method(:get_images_similarity) do |
| 147 | + def get_images_similarity(first_image:, second_image:, visualize: false) |
| 148 | + unless ::Appium::Core::Device::ImageComparison::GET_SIMILARITY[:visualize].member?(visualize) |
| 149 | + raise "visualize should be #{::Appium::Core::Device::ImageComparison::GET_SIMILARITY[:visualize]}" |
| 150 | + end |
| 151 | + |
| 152 | + options = {} |
| 153 | + options[:visualize] = visualize |
| 154 | + |
| 155 | + compare_images(mode: :getSimilarity, first_image: first_image, second_image: second_image, options: options) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + ::Appium::Core::Device.add_endpoint_method(:compare_images) do |
| 160 | + def compare_images(mode: :matchFeatures, first_image:, second_image:, options: nil) |
| 161 | + unless ::Appium::Core::Device::ImageComparison::MODE.member?(mode) |
| 162 | + raise "content_type should be #{::Appium::Core::Device::ImageComparison::MODE}" |
| 163 | + end |
| 164 | + |
| 165 | + params = {} |
| 166 | + params[:mode] = mode |
| 167 | + params[:firstImage] = Base64.encode64 first_image |
| 168 | + params[:secondImage] = Base64.encode64 second_image |
| 169 | + params[:options] = options if options |
| 170 | + |
| 171 | + execute(:compare_images, {}, params) |
| 172 | + end |
| 173 | + end |
| 174 | + end # self |
| 175 | + end # module ImageComparison |
| 176 | + end # module Device |
| 177 | + end # module Core |
| 178 | +end # module Appium |
0 commit comments