Module: DeepStack::Face

Included in:
DeepStack
Defined in:
lib/deep_stack/face.rb

Overview

APIs related to face recognition

Instance Method Summary collapse

Instance Method Details

#delete_face(userid) ⇒ Boolean

Delete the given face / userid

Parameters:

  • userid (String)

    to delete

Returns:

  • (Boolean)

    true when successful



63
64
65
66
# File 'lib/deep_stack/face.rb', line 63

def delete_face(userid)
  target = 'vision/face/delete'
  api_post(target, userid: userid)&.dig('success') == true
end

#delete_faces(*userids) ⇒ Hash

Delete the given list of faces / userids

Parameters:

  • userids (Array)

    a list of userids to delete

Returns:

  • (Hash)

    A hash of ‘userid => boolean` that indicates success/failure



52
53
54
# File 'lib/deep_stack/face.rb', line 52

def delete_faces(*userids)
  userids.flatten.compact.to_h { |userid| [userid, delete_face(userid)] }
end

#detect_faces(image, **options) ⇒ Array

Detect faces in an image

Parameters:

  • image (Object)

    binary data or a File object

  • options (Hash)

    additional fields for DeepStack, e.g. min_confidence: 0.5

Returns:

  • (Array)

    if successful, an array of DeepStack predictions



29
30
31
32
# File 'lib/deep_stack/face.rb', line 29

def detect_faces(image, **options)
  target = 'vision/face/' # the URL ends with a slash
  api_post(target, image, **options)&.dig('predictions')
end

#face_listArray

Get a list of registered faces

Returns:

  • (Array)

    a list of userids



39
40
41
42
43
# File 'lib/deep_stack/face.rb', line 39

def face_list
  target = 'vision/face/list'
  api_post(target)&.dig('faces')
  # {"success"=>true, "faces"=>["face_1", "face_2"], "duration"=>0}
end

#face_match(*images, **args) ⇒ Float?

Call DeepStack’s Face Match service. Compare two different pictures and tells the similarity between them.

Examples:

image1 = File.read('obama1.jpg')
image2 = File.read('obama2.jpg')
puts deepstack.face_match(image1, image2) > 0.6 ? 'similar' : 'different'

Parameters:

  • images (Array)

    two images to compare

  • args (kwargs)

    optional arguments to the API call

Returns:

  • (Float)

    The similarity score (0-1)

  • (nil)

    if failed



95
96
97
98
# File 'lib/deep_stack/face.rb', line 95

def face_match(*images, **args)
  target = 'vision/face/match'
  api_post(target, images, **args)&.dig('similarity')
end

#recognize_faces(image, **options) ⇒ Array?

Perform face recognition

Parameters:

  • image (Object)

    binary data or a File object

  • options (Hash)

    additional fields for DeepStack, e.g. min_confidence: 0.5

Returns:

  • (Array)

    if successful, an array of DeepStack predictions

  • (nil)

    if error



16
17
18
19
# File 'lib/deep_stack/face.rb', line 16

def recognize_faces(image, **options)
  target = 'vision/face/recognize'
  api_post(target, image, **options)&.dig('predictions')
end

#register_face(userid, *images) ⇒ Boolean

Register a face

Parameters:

  • userid (String)

    to register

  • images (Array)

    facial image data in binary form or File object

Returns:

  • (Boolean)

    true when successful



76
77
78
79
# File 'lib/deep_stack/face.rb', line 76

def register_face(userid, *images)
  target = 'vision/face/register'
  api_post(target, images, userid: userid)&.dig('success') == true
end