Class: Gcloud::Vision::Annotation::Face::Features

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

Overview

# Features

Represents facial landmarks or features. Left and right are defined from the vantage of the viewer of the image, without considering mirror projections typical of photos. So ‘face.features.eyes.left` typically is the person’s right eye.

See Gcloud::Vision::Annotation::Face.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

face.features.to_h.count #=> 9
face.features.eyes.left.pupil
#=> #<Landmark (x: 190.41544, y: 84.4557, z: -1.3682901)>
face.features.chin.center
#=> #<Landmark (x: 233.21977, y: 189.47475, z: 19.487228)>

See Also:

Defined Under Namespace

Classes: Chin, Ears, Eye, Eyebrow, Eyebrows, Eyes, Landmark, Lips, Mouth, Nose

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeatures

Returns a new instance of Features.



400
401
402
# File 'lib/gcloud/vision/annotation/face.rb', line 400

def initialize
  @gapi = {}
end

Instance Attribute Details

#gapiObject



396
397
398
# File 'lib/gcloud/vision/annotation/face.rb', line 396

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi) ⇒ Object

object.



578
579
580
# File 'lib/gcloud/vision/annotation/face.rb', line 578

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

Instance Method Details

#[](landmark_type) ⇒ Landmark

Returns the facial landmark for the provided type code.

API](https://cloud.google.com/vision/reference/rest/v1/images/annotate#Type_1).

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

face.features["RIGHT_EAR_TRAGION"]
#=> #<Landmark (x: 303.81198, y: 88.5782, z: 77.719193)>

Parameters:

  • landmark_type (String, Symbol)

    An ‘images.annotate` type code from the [Vision

Returns:

See Also:



437
438
439
440
441
442
443
# File 'lib/gcloud/vision/annotation/face.rb', line 437

def [] landmark_type
  landmark = Array(@gapi["landmarks"]).detect do |l|
    l["type"] == landmark_type
  end
  return nil if landmark.nil?
  Landmark.from_gapi landmark
end

#chinChin

The landmarks of the chin.

Returns:



450
451
452
453
# File 'lib/gcloud/vision/annotation/face.rb', line 450

def chin
  @chin ||= Chin.new self["CHIN_LEFT_GONION"], self["CHIN_GNATHION"],
                     self["CHIN_RIGHT_GONION"]
end

#confidenceFloat

The confidence of the facial landmarks detection.

Returns:

  • (Float)

    A value in the range [0,1].



409
410
411
# File 'lib/gcloud/vision/annotation/face.rb', line 409

def confidence
  @gapi["landmarkingConfidence"]
end

#earsEars

The landmarks of the ears.

Returns:



460
461
462
463
# File 'lib/gcloud/vision/annotation/face.rb', line 460

def ears
  @ears ||= Ears.new self["LEFT_EAR_TRAGION"],
                     self["RIGHT_EAR_TRAGION"]
end

#eyebrowsEyebrows

The landmarks of the eyebrows.

Returns:



470
471
472
473
474
475
476
477
478
479
480
# File 'lib/gcloud/vision/annotation/face.rb', line 470

def eyebrows
  @eyebrows ||= begin
    left = Eyebrow.new self["LEFT_OF_LEFT_EYEBROW"],
                       self["LEFT_EYEBROW_UPPER_MIDPOINT"],
                       self["RIGHT_OF_LEFT_EYEBROW"]
    right = Eyebrow.new self["LEFT_OF_RIGHT_EYEBROW"],
                        self["RIGHT_EYEBROW_UPPER_MIDPOINT"],
                        self["RIGHT_OF_RIGHT_EYEBROW"]
    Eyebrows.new left, right
  end
end

#eyesEyes

The landmarks of the eyes.

Returns:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/gcloud/vision/annotation/face.rb', line 487

def eyes
  @eyes ||= begin
    left = Eye.new self["LEFT_EYE_LEFT_CORNER"],
                   self["LEFT_EYE_BOTTOM_BOUNDARY"],
                   self["LEFT_EYE"], self["LEFT_EYE_PUPIL"],
                   self["LEFT_EYE_TOP_BOUNDARY"],
                   self["LEFT_EYE_RIGHT_CORNER"]
    right = Eye.new self["RIGHT_EYE_LEFT_CORNER"],
                    self["RIGHT_EYE_BOTTOM_BOUNDARY"],
                    self["RIGHT_EYE"], self["RIGHT_EYE_PUPIL"],
                    self["RIGHT_EYE_TOP_BOUNDARY"],
                    self["RIGHT_EYE_RIGHT_CORNER"]
    Eyes.new left, right
  end
end

#foreheadLandmark

The landmark for the forehead glabella.

Returns:



508
509
510
# File 'lib/gcloud/vision/annotation/face.rb', line 508

def forehead
  @forehead ||= self["FOREHEAD_GLABELLA"]
end

#inspectObject



571
572
573
# File 'lib/gcloud/vision/annotation/face.rb', line 571

def inspect
  "#<Features #{self}>"
end

#lipsLips

The landmarks of the lips.

Returns:



517
518
519
# File 'lib/gcloud/vision/annotation/face.rb', line 517

def lips
  @lips ||= Lips.new self["UPPER_LIP"], self["LOWER_LIP"]
end

#mouthMouth

The landmarks of the mouth.

Returns:



526
527
528
529
# File 'lib/gcloud/vision/annotation/face.rb', line 526

def mouth
  @mouth ||= Mouth.new self["MOUTH_LEFT"], self["MOUTH_CENTER"],
                       self["MOUTH_RIGHT"]
end

#noseNose

The landmarks of the nose.

Returns:



536
537
538
539
540
541
# File 'lib/gcloud/vision/annotation/face.rb', line 536

def nose
  @nose ||= Nose.new self["NOSE_BOTTOM_LEFT"],
                     self["NOSE_BOTTOM_CENTER"], self["NOSE_TIP"],
                     self["MIDPOINT_BETWEEN_EYES"],
                     self["NOSE_BOTTOM_RIGHT"]
end

#to_hHash

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

Returns:

  • (Hash)


548
549
550
# File 'lib/gcloud/vision/annotation/face.rb', line 548

def to_h
  to_hash
end

#to_hashHash

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

Returns:

  • (Hash)


557
558
559
560
561
# File 'lib/gcloud/vision/annotation/face.rb', line 557

def to_hash
  { confidence: confidence, chin: chin.to_h, ears: ears.to_h,
    eyebrows: eyebrows.to_h, eyes: eyes.to_h, forehead: forehead.to_h,
    lips: lips.to_h, mouth: mouth.to_h, nose: nose.to_h }
end

#to_sObject



564
565
566
567
568
# File 'lib/gcloud/vision/annotation/face.rb', line 564

def to_s
  # Keep console output low by not showing all sub-objects.
  "(confidence, chin, ears, eyebrows, eyes, " \
    "forehead, lips, mouth, nose)"
end