Class: Gcloud::Vision::Annotation::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/vision/annotation/entity.rb

Overview

# Entity

Represents characteristics of an entity detected in an image. May describe a real-world entity such as a person, place, or thing. May be identified with an entity ID as an entity in the Knowledge Graph (KG).

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/landmark.jpg"

landmark = image.landmark
landmark.score #=> 0.91912264
landmark.description #=> "Mount Rushmore"
landmark.mid #=> "/m/019dvv"
require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/logo.jpg"

 = image.
.score #=> 0.70057315
.description #=> "Google"
.mid #=> "/m/0b34hf"
require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/face.jpg"

labels = image.labels
labels.count #=> 4

label = labels.first
label.score #=> 0.9481349
label.description #=> "person"
label.mid #=> "/m/01g317"

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntity

Returns a new instance of Entity.



80
81
82
# File 'lib/gcloud/vision/annotation/entity.rb', line 80

def initialize
  @gapi = {}
end

Instance Attribute Details

#gapiObject



76
77
78
# File 'lib/gcloud/vision/annotation/entity.rb', line 76

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi) ⇒ Object



228
229
230
# File 'lib/gcloud/vision/annotation/entity.rb', line 228

def self.from_gapi gapi
  new.tap { |f| f.instance_variable_set :@gapi, gapi }
end

Instance Method Details

#boundsArray<Vertex>

Image region to which this entity belongs. Not filled currently for ‘labels` detection.

Returns:

  • (Array<Vertex>)

    An array of vertices.



155
156
157
158
159
160
# File 'lib/gcloud/vision/annotation/entity.rb', line 155

def bounds
  return [] unless @gapi.bounding_poly
  @bounds ||= Array(@gapi.bounding_poly.vertices).map do |v|
    Vertex.from_gapi v
  end
end

#confidenceFloat

The accuracy of the entity detection in an image. For example, for an image containing ‘Eiffel Tower,’ this field represents the confidence that there is a tower in the query image.

Returns:

  • (Float)

    A value in the range [0, 1].



132
133
134
# File 'lib/gcloud/vision/annotation/entity.rb', line 132

def confidence
  @gapi.confidence
end

#descriptionString

Entity textual description, expressed in the #locale language.

Returns:

  • (String)

    A description of the entity.



112
113
114
# File 'lib/gcloud/vision/annotation/entity.rb', line 112

def description
  @gapi.description
end

#inspectObject



222
223
224
# File 'lib/gcloud/vision/annotation/entity.rb', line 222

def inspect
  "#<#{self.class.name} #{self}>"
end

#localeString

The language code for the locale in which the ‘description` is expressed.

Returns:



103
104
105
# File 'lib/gcloud/vision/annotation/entity.rb', line 103

def locale
  @gapi.locale
end

#locationsArray<Location>

The location information for the detected entity. Multiple Location elements can be present since one location may indicate the location of the scene in the query image, and another the location of the place where the query image was taken. Location information is usually present for landmarks.

Returns:

  • (Array<Location>)

    An array of locations containing latitude and longitude.



172
173
174
175
176
# File 'lib/gcloud/vision/annotation/entity.rb', line 172

def locations
  @locations ||= Array(@gapi.locations).map do |l|
    Location.from_gapi l.lat_lng
  end
end

#midString

Opaque entity ID. Some IDs might be available in Knowledge Graph (KG).

Returns:

  • (String)

    The opaque entity ID.

See Also:



91
92
93
# File 'lib/gcloud/vision/annotation/entity.rb', line 91

def mid
  @gapi.mid
end

#propertiesHash

Some entities can have additional optional Property fields. For example a different kind of score or string that qualifies the entity. present for landmarks.

Returns:

  • (Hash)

    A hash containing property names and values.



185
186
187
188
# File 'lib/gcloud/vision/annotation/entity.rb', line 185

def properties
  @properties ||=
    Hash[Array(@gapi.properties).map { |p| [p.name, p.value] }]
end

#scoreFloat

Overall score of the result.

Returns:

  • (Float)

    A value in the range [0, 1].



121
122
123
# File 'lib/gcloud/vision/annotation/entity.rb', line 121

def score
  @gapi.score
end

#to_hHash

Deeply converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


195
196
197
# File 'lib/gcloud/vision/annotation/entity.rb', line 195

def to_h
  to_hash
end

#to_hashHash

Deeply converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


204
205
206
207
208
209
# File 'lib/gcloud/vision/annotation/entity.rb', line 204

def to_hash
  { mid: mid, locale: locale, description: description,
    score: score, confidence: confidence, topicality: topicality,
    bounds: bounds.map(&:to_h), locations: locations.map(&:to_h),
    properties: properties }
end

#to_sObject



212
213
214
215
216
217
218
219
# File 'lib/gcloud/vision/annotation/entity.rb', line 212

def to_s
  tmplt = "mid: %s, locale: %s, description: %s, score: %s, " \
          "confidence: %s, topicality: %s, bounds: %i, " \
          "locations: %i, properties: %s"
  format tmplt, mid.inspect, locale.inspect, description.inspect,
         score.inspect, confidence.inspect, topicality.inspect,
         bounds.count, locations.count, properties.inspect
end

#topicalityFloat

The relevancy of the ICA (Image Content Annotation) label to the image. For example, the relevancy of ‘tower’ to an image containing ‘Eiffel Tower’ is likely higher than an image containing a distant towering building, though the confidence that there is a tower may be the same.

Returns:

  • (Float)

    A value in the range [0, 1].



145
146
147
# File 'lib/gcloud/vision/annotation/entity.rb', line 145

def topicality
  @gapi.topicality
end