We can recognize the character's from an image using rtesseract gem and tesseract-ocr. So tesseract-ocr is an open-source OCR(optical character recognition) engine and rtesseract gem is a ruby wrapper for using tesseract-ocr.
To make rtesseract gem work we need tesseract-ocr, leptonica libraries, headers, and a C++ compiler. The tesseract-ocr gem will provide all of this. If you're on a distribution that separates the libraries from headers, remember to install the -dev package. On Debian, you will need to install libleptonica-dev and libtesseract-dev.
Installation steps are as follows:
First, install tesseract-ocr by using the following command "gem install tesseract-ocr" and check the installation by using "tesseract --version".
Then install rtesseract gem that's it start using the powerful tesseract for recognizing text in an image.
For example:
image = RTesseract.new('my_image.jpg', lang: 'eng') #lang is optional
image.to_s # Getting the text value
Here RTesseract takes image URL which is required and lang is the type of language we want to scan and the default is eng - English, the "to_s" method returns the text and we can also convert the image into pdf by using "to_pdf" on the image object. It supports many more options and methods you can check it here.
The reason I used this gem because I wanted to scan a folder of images and check whether an image consists of "No image" text if yes then move the image to the "no_image" folder.
So below is the ruby code of how I achieved this.
require 'fileutils'
require 'rtesseract'
class ExtractNameFromImage
def move_images_from_folder(path)
count = 0
Dir.glob("#{path}/*") do |file|
image = RTesseract.new(file)
text = image.to_s
count += 1
if text.include? "No Image"
FileUtils.mv(file, 'pathtomove') #/home/user/no_image_folder
puts "Moved #{file} scanned #{count}"
else
puts "Ok #{file} scanned #{count}"
end
end
end
end
puts "Scanning directory #{ARGV[0]}"
extracter = ExtractNameFromImage.new
if File.directory?(ARGV[0])
extracter.move_images_from_folder(ARGV[0])
else
puts "No such directory exist, try again"
end
Command to run this file "ruby image_name.rb home/user/pathtoimagefolder".
Conclusion:
Using this set up I can scan thousands of image without manually opening each of them one by one.
Top comments (0)