Module: ActiveStorage::Ocr
- Defined in:
- lib/activestorage/ocr.rb,
lib/activestorage/ocr/binary.rb,
lib/activestorage/ocr/client.rb,
lib/activestorage/ocr/result.rb,
lib/activestorage/ocr/railtie.rb,
lib/activestorage/ocr/version.rb,
lib/activestorage/ocr/analyzer.rb,
lib/activestorage/ocr/configuration.rb
Overview
OCR support for Rails Active Storage attachments.
This module provides optical character recognition (OCR) for files stored with Active Storage using a high-performance Rust server.
OCR Engines
Two engines are available:
:ocrs- Pure Rust engine (default). Fast, no system dependencies.:leptess- Tesseract-based engine. Better for noisy/messy images.
Configuration
ActiveStorage::Ocr.configure do |config|
config.server_url = "http://localhost:9292"
config.timeout = 30
config.engine = :leptess # Use Tesseract instead of default ocrs
end
Basic Usage
# Extract text from an Active Storage blob
result = ActiveStorage::Ocr.extract_text(document.file)
result.text # => "Extracted text..."
result.confidence # => 0.95
result.engine # => "ocrs"
# Use a specific engine for one request
client = ActiveStorage::Ocr::Client.new
result = client.extract_text(document.file, engine: :leptess)
# Compare both engines
comparison = client.compare(document.file)
comparison[:ocrs].text # => "Text from ocrs..."
comparison[:leptess].text # => "Text from leptess..."
Error Handling
All errors inherit from ActiveStorage::Ocr::Error:
- ActiveStorage::Ocr::ConnectionError - server unreachable
- ActiveStorage::Ocr::ServerError - server returned an error
Defined Under Namespace
Classes: Analyzer, Binary, Client, Configuration, ConnectionError, Error, Railtie, Result, ServerError
Constant Summary collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the current configuration.
Class Method Summary collapse
-
.configure {|configuration| ... } ⇒ Object
Configures the OCR module.
-
.extract_text(blob) ⇒ Object
Extracts text from an Active Storage blob.
-
.reset_configuration! ⇒ Object
Resets configuration to defaults.
Class Attribute Details
.configuration ⇒ Object
Returns the current configuration.
Creates a new Configuration with defaults if none exists.
77 78 79 |
# File 'lib/activestorage/ocr.rb', line 77 def configuration @configuration ||= Configuration.new end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
Configures the OCR module.
Example
ActiveStorage::Ocr.configure do |config|
config.server_url = "http://localhost:9292"
config.timeout = 60
end
89 90 91 |
# File 'lib/activestorage/ocr.rb', line 89 def configure yield(configuration) end |
.extract_text(blob) ⇒ Object
Extracts text from an Active Storage blob.
This is a convenience method that creates a new Client and calls extract_text on it.
Parameters
blob- An ActiveStorage::Blob instance
Returns
A Result object containing extracted text and metadata.
Raises
- ConnectionError - if the server is unreachable
- ServerError - if the server returns an error
117 118 119 |
# File 'lib/activestorage/ocr.rb', line 117 def extract_text(blob) Client.new.extract_text(blob) end |
.reset_configuration! ⇒ Object
Resets configuration to defaults.
Useful for testing.
96 97 98 |
# File 'lib/activestorage/ocr.rb', line 96 def reset_configuration! @configuration = Configuration.new end |