Skip to content

Instantly share code, notes, and snippets.

@Tobias-pwnr
Created October 23, 2024 11:19
Show Gist options
  • Select an option

  • Save Tobias-pwnr/f6bcafbcf9880962082090e169dff88a to your computer and use it in GitHub Desktop.

Select an option

Save Tobias-pwnr/f6bcafbcf9880962082090e169dff88a to your computer and use it in GitHub Desktop.
collect all images that contains 'money forward' with OCR tool, inside app/assets directory and all of its sub directory
#Install Tesseract: Install Tesseract OCR on your system. On macOS, you can use Homebrew:
#brew install tesseract
#Create a Ruby script: Create a Ruby script to scan images in the app/assets directory and use Tesseract to check for the text "money forward".
#Run the script: Execute the Ruby script to find all images containing the text "money forward":
# ruby collect_images_with_text.rb
require 'find'
require 'open3'
def image_contains_text?(image_path, text)
stdout, _stderr, _status = Open3.capture3("tesseract #{image_path} stdout")
stdout.include?(text)
end
def collect_images_with_text(directory, text)
images_with_text = []
Find.find(directory) do |path|
if path =~ /\.(png|jpe?g|gif)$/i
if image_contains_text?(path, text)
puts path
# images_with_text << path
end
end
end
# images_with_text
end
directory = './'
text = 'orward'
puts "Images containing '#{text}':"
# images =
collect_images_with_text(directory, text)
# images.each { |image| puts image }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment