Class: ActiveStorage::Ocr::Analyzer

Inherits:
Analyzer
  • Object
show all
Defined in:
lib/activestorage/ocr/analyzer.rb

Overview

Active Storage analyzer for OCR processing.

This analyzer integrates with Active Storage's analysis system to automatically extract text from uploaded images and PDFs.

When registered with Active Storage, it will automatically process supported file types and store OCR results in the blob's metadata.

Metadata

After analysis, blobs will have the following metadata:

  • ocr_text - The extracted text
  • ocr_confidence - Confidence score (0.0 to 1.0)
  • ocr_engine - The OCR engine used ("ocrs" or "leptess")
  • ocr_processed_at - ISO 8601 timestamp

Example

document.file.analyze
document.file.["ocr_text"]  # => "Extracted text..."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept?(blob) ⇒ Boolean

Determines if this analyzer can process the blob.

Parameters

  • blob - An ActiveStorage::Blob instance

Returns

true if the blob's content type is supported.

Returns:

  • (Boolean)


36
37
38
# File 'lib/activestorage/ocr/analyzer.rb', line 36

def self.accept?(blob)
  ActiveStorage::Ocr.configuration.accept_content_type?(blob.content_type)
end

Instance Method Details

#metadata ⇒ Object

Extracts OCR metadata from the blob.

Called by Active Storage during analysis.

Returns

A Hash containing OCR results, or an empty Hash if extraction fails.



47
48
49
50
51
52
53
54
55
# File 'lib/activestorage/ocr/analyzer.rb', line 47

def 
  result = extract_text
  return {} unless result&.success?

  result.
rescue Error => e
  Rails.logger.error("[ActiveStorage::Ocr] OCR failed: #{e.message}") if defined?(Rails)
  {}
end