Class: Cognitive::Face::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cognitive/face/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:) ⇒ Client

Constructor

Parameters:

  • key (String)

    Subscription key which provides access to this API



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cognitive/face/client.rb', line 9

def initialize(key:)
  @key = key
  @headers = {
    'Ocp-Apim-Subscription-Key' => @key,
    'Content-Type': 'application/json'
  }
  @endpoints = {
    detect:'https://api.projectoxford.ai/face/v1.0/detect',
    facelist: 'https://api.projectoxford.ai/face/v1.0/facelists/',
    findsimilar: 'https://api.projectoxford.ai/face/v1.0/findsimilars'
  }
end

Instance Method Details

#add_face_to_face_list(face:, face_list_id:, user_data: nil, target_face: nil) ⇒ Object

Add face to face list

Parameters:

  • face (File)

    Image

  • face_list_id (String)
  • user_data (String) (defaults to: nil)

    Optinal

  • target_face (defaults to: nil)

    Optional



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cognitive/face/client.rb', line 116

def add_face_to_face_list(face:, face_list_id:, user_data: nil, target_face: nil)
  headers = @headers.merge(
    params: {
      userData: user_data,
      targetFace: target_face
    },
    'Content-Type': 'application/octet-stream'
  )
  client = RestClient.post(
    @endpoints[:facelist]+"#{face_list_id}/persistedFaces",
    face,
    headers
  )
  Response.new(client).get
end

#create_face_list(face_list_id:, name:, user_data: nil) ⇒ Object

Create face list

Parameters:

  • face_list_id (String)

    unique indentify

  • name (String)

    name face list

  • user_data (String) (defaults to: nil)

    Optinal info



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cognitive/face/client.rb', line 94

def create_face_list(face_list_id:,name:,user_data: nil)
  params = {
    name: name,
    userData: user_data
  }.to_json
  client = RestClient.put(
    @endpoints[:facelist]+face_list_id.to_s,
    params,
    @headers
  )
   Response.new(
    client,
    json: false,
    response_custom: lambda {|client| client.code == 200}
    ).get
end

#delete_face_list(face_list_id:) ⇒ Object

Parameters:

  • face_list_id (String)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cognitive/face/client.rb', line 65

def delete_face_list(face_list_id:)
  client = RestClient.delete(
    @endpoints[:facelist] + face_list_id.to_s,
    @headers
  )
  Response.new(
    client,
    json: false,
    response_custom: lambda {|client| client.code == 200}
  ).get
end

#delete_face_to_face_list(face_id:, face_list_id:) ⇒ Object

Deleted face from a face list

Parameters:

  • face_id (String)
  • face_list_id (String)


136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cognitive/face/client.rb', line 136

def delete_face_to_face_list(face_id:, face_list_id:)
  client = RestClient.delete(
    @endpoints[:facelist]+"#{face_list_id}/persistedFaces/#{face_id}",
    @headers
  )
  Response.new(
    client,
    json: false,
    response_custom: lambda {|client| client.code == 200}
  ).get
end

#detect(face:, return_face_id: true, returnFaceLandmarks: false, returnFaceAttributes: nil) ⇒ Object

Detect human faces in an image and returns face locations, and optionally with faceIds, landmarks, and attributes Api rest documentation dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

Parameters:

  • face (File)

    JPEG, PNG, GIF

  • return_face_id (Boolean) (defaults to: true)

    return face id or not. default true



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cognitive/face/client.rb', line 28

def detect(face:,return_face_id: true,
          returnFaceLandmarks:false, 
          returnFaceAttributes: nil)
  query_params = {
    returnFaceId: return_face_id,
    returnFaceLandmarks: returnFaceLandmarks
  }

  headers = @headers.merge(
    params: query_params,
    'Content-Type': 'application/octet-stream'
  )

 client = RestClient.post(
  @endpoints[:detect],
  face,
  headers
 )
 Response.new(client).get
end

#find_similar(face_id:, face_list_id:, mode: 'matchPerson') ⇒ Object

Find face from a face list

Parameters:

  • face_id (String)
  • face_list_id (String)
  • mode (String) (defaults to: 'matchPerson')


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cognitive/face/client.rb', line 153

def find_similar(face_id:, face_list_id:, mode: 'matchPerson')
  data = {
    faceId: face_id,
    faceListId: face_list_id,
    mode: mode
  }
  client = RestClient.post(
    @endpoints[:findsimilar],
    data.to_json,
    @headers
  )
  Response.new(client).get
end

#get_face_list(face_list_id:) ⇒ Object

Parameters:

  • face_list_id (String)


53
54
55
56
57
58
59
# File 'lib/cognitive/face/client.rb', line 53

def get_face_list(face_list_id:)
  client = RestClient.get(
    @endpoints[:facelist] + face_list_id.to_s,
    @headers
  )
  Response.new(client).get
end

#get_face_listsObject



81
82
83
84
85
86
87
# File 'lib/cognitive/face/client.rb', line 81

def get_face_lists
  client = RestClient.get(
    @endpoints[:facelist],
    @headers
  )
  Response.new(client).get
end