Class: Sqed

Inherits:
Object
  • Object
show all
Defined in:
lib/sqed.rb,
lib/sqed/result.rb,
lib/sqed/version.rb,
lib/sqed/extractor.rb,
lib/sqed/boundaries.rb,
lib/sqed/boundary_finder.rb

Overview

Instances take the following 1) An :image @image 2) A target extraction pattern, or individually specified attributes

Return a Sqed::Result

a = Sqed.new(pattern: :vertical_offset_cross, image: image)
b = a.result # => Sqed::Result instance

Defined Under Namespace

Classes: Boundaries, BoundaryFinder, Error, Extractor, Parser, Result

Constant Summary collapse

VERSION =
'0.8.3'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Sqed

Returns a new instance of Sqed.



80
81
82
83
84
85
86
# File 'lib/sqed.rb', line 80

def initialize(**opts)
  # extraction metadata
  @image = opts[:image]

  configure(opts)
  stub_results
end

Instance Attribute Details

#boundaries(force = false) ⇒ Sqed::Boundaries instance?

Returns Contains the coordinates of the internal stage sections. Calculates when set.

Returns:

  • (Sqed::Boundaries instance, nil)

    Contains the coordinates of the internal stage sections. Calculates when set.



28
29
30
# File 'lib/sqed.rb', line 28

def boundaries
  @boundaries
end

#boundary_colorSymbol

describing the boundary color within the stage

Returns:

  • (Symbol)

    like ‘:red`, `:green`, `:blue`, defaults to `:green`



32
33
34
# File 'lib/sqed.rb', line 32

def boundary_color
  @boundary_color
end

#boundary_finderSqed::BoundaryFinder::<Klass>

Provide a boundary_finder, overrides metadata taken from pattern

Returns:



36
37
38
# File 'lib/sqed.rb', line 36

def boundary_finder
  @boundary_finder
end

#has_borderBoolean

Returns defaults to ‘true` when true detects border on initialization.

Returns:

  • (Boolean)

    defaults to ‘true` when true detects border on initialization



40
41
42
# File 'lib/sqed.rb', line 40

def has_border
  @has_border
end

#imageObject

initial image which is an instance of Magick::Image, containing background and stage, or just stage



43
44
45
# File 'lib/sqed.rb', line 43

def image
  @image
end

#layoutSymbol

!! Provide a specific layout, passed as option :layout, overrides layout metadata taken from :pattern, defaults to ‘:cross`

Returns:

  • (Symbol)

    like ‘:cross`



47
48
49
# File 'lib/sqed.rb', line 47

def layout
  @layout
end

#metadata_mapObject

Provide a metadata map, overrides metadata taken from pattern. No calculations are done here.



51
52
53
# File 'lib/sqed.rb', line 51

def 
  @metadata_map
end

#patternSymbol

!optional! A lookup macro that if provided sets boundary_finder, layout, and metadata_map. These can be individually overwritten. Legal values are symbols taken from SqedConfig::EXTRACTION_PATTERNS.

!! Patterns are not intended to be persisted in external databases (they may change names). !! To persist Sqed metadata in something like Postgres reference individual attributes (e.g. layout, metadata_map, boundary_finder).

default value is ‘nil`

not required if layout, metadata_map, and boundary_finder are provided

Returns:

  • (Symbol)

    like ‘:seven_slot`, see Sqed::CONFIG for valid options,



64
65
66
# File 'lib/sqed.rb', line 64

def pattern
  @pattern
end

#stage_boundarySqed::Boundaries instance

!! Does not require ‘metadata_map` or `layout`

Returns:



73
74
75
# File 'lib/sqed.rb', line 73

def stage_boundary
  @stage_boundary
end

#stage_imageObject

The image that is the cropped content for parsing !! Does not require ‘metadata_map` or `layout`



68
69
70
# File 'lib/sqed.rb', line 68

def stage_image
  @stage_image
end

#use_thumbnailBoolean

against a thumbnail version of the passed image

Returns:

  • (Boolean)

    defaults to ‘true` (faster, less accurate) if `true` do the boundary detection (not stage detection at present)



78
79
80
# File 'lib/sqed.rb', line 78

def use_thumbnail
  @use_thumbnail
end

Instance Method Details

#crop_imageImage

Returns crops the stage if not done, then sets/returns @stage_image.

Returns:

  • (Image)

    crops the stage if not done, then sets/returns @stage_image



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sqed.rb', line 118

def crop_image

  begin

    if has_border && image
      @stage_image = image.crop(*stage_boundary.for(SqedConfig.index_for_section_type(:stage, :stage)), true)
    elsif image
      @stage_image = image
    end
    @stage_image

  rescue Sqed::Error
    raise
  rescue
    raise Sqed::Error, 'error cropping image'
  end

end

#extractable?Boolean

Returns ‘true` if all boundary detection and image processing has been calculated with error.

Returns:

  • (Boolean)

    ‘true` if all boundary detection and image processing has been calculated with error



159
160
161
# File 'lib/sqed.rb', line 159

def extractable?
  stage_image &&  && boundaries
end

#extraction_metadataHash

Returns federate extraction options.

Returns:

  • (Hash)

    federate extraction options



90
91
92
93
94
95
96
97
98
99
# File 'lib/sqed.rb', line 90

def 
  {
    boundary_finder: boundary_finder,
    layout: layout,
    metadata_map: ,
    boundary_color: boundary_color,
    has_border: has_border,
    use_thumbnail: use_thumbnail
  }
end

#resultSqed::Result, false

Returns:



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/sqed.rb', line 164

def result
  return false unless extractable?

  extractor = Sqed::Extractor.new(
    boundaries: boundaries,
    metadata_map: ,
    image: stage_image
  )

  extractor.result
end