| | | 1 | | // <auto-generated> |
| | | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 3 | | // Licensed under the MIT License. See License.txt in the project root for |
| | | 4 | | // license information. |
| | | 5 | | // |
| | | 6 | | // Code generated by Microsoft (R) AutoRest Code Generator. |
| | | 7 | | // Changes may cause incorrect behavior and will be lost if the code is |
| | | 8 | | // regenerated. |
| | | 9 | | // </auto-generated> |
| | | 10 | | |
| | | 11 | | namespace Microsoft.Azure.CognitiveServices.Vision.Face |
| | | 12 | | { |
| | | 13 | | using Microsoft.Rest; |
| | | 14 | | using Models; |
| | | 15 | | using Newtonsoft.Json; |
| | | 16 | | using System.Collections; |
| | | 17 | | using System.Collections.Generic; |
| | | 18 | | using System.IO; |
| | | 19 | | using System.Linq; |
| | | 20 | | using System.Net; |
| | | 21 | | using System.Net.Http; |
| | | 22 | | using System.Threading; |
| | | 23 | | using System.Threading.Tasks; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// FaceOperations operations. |
| | | 27 | | /// </summary> |
| | | 28 | | public partial class FaceOperations : IServiceOperations<FaceClient>, IFaceOperations |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the FaceOperations class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name='client'> |
| | | 34 | | /// Reference to the service client. |
| | | 35 | | /// </param> |
| | | 36 | | /// <exception cref="System.ArgumentNullException"> |
| | | 37 | | /// Thrown when a required parameter is null |
| | | 38 | | /// </exception> |
| | 34 | 39 | | public FaceOperations(FaceClient client) |
| | | 40 | | { |
| | 34 | 41 | | if (client == null) |
| | | 42 | | { |
| | 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | | 44 | | } |
| | 34 | 45 | | Client = client; |
| | 34 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a reference to the FaceClient |
| | | 50 | | /// </summary> |
| | 1110 | 51 | | public FaceClient Client { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Given query face's faceId, to search the similar-looking faces from a |
| | | 55 | | /// faceId array, a face list or a large face list. faceId array contains the |
| | | 56 | | /// faces created by [Face - |
| | | 57 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236), |
| | | 58 | | /// which will expire 24 hours after creation. A "faceListId" is created by |
| | | 59 | | /// [FaceList - |
| | | 60 | | /// Create](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524b) |
| | | 61 | | /// containing persistedFaceIds that will not expire. And a "largeFaceListId" |
| | | 62 | | /// is created by [LargeFaceList - |
| | | 63 | | /// Create](/docs/services/563879b61984550e40cbbe8d/operations/5a157b68d2de3616c086f2cc) |
| | | 64 | | /// containing persistedFaceIds that will also not expire. Depending on the |
| | | 65 | | /// input the returned similar faces list contains faceIds or persistedFaceIds |
| | | 66 | | /// ranked by similarity. |
| | | 67 | | /// <br/>Find similar has two working modes, "matchPerson" and |
| | | 68 | | /// "matchFace". "matchPerson" is the default mode that it tries to find faces |
| | | 69 | | /// of the same person as possible by using internal same-person thresholds. It |
| | | 70 | | /// is useful to find a known person's other photos. Note that an empty list |
| | | 71 | | /// will be returned if no faces pass the internal thresholds. "matchFace" mode |
| | | 72 | | /// ignores same-person thresholds and returns ranked similar faces anyway, |
| | | 73 | | /// even the similarity is low. It can be used in the cases like searching |
| | | 74 | | /// celebrity-looking faces. |
| | | 75 | | /// <br/>The 'recognitionModel' associated with the query face's faceId |
| | | 76 | | /// should be the same as the 'recognitionModel' used by the target faceId |
| | | 77 | | /// array, face list or large face list. |
| | | 78 | | /// |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name='faceId'> |
| | | 81 | | /// FaceId of the query face. User needs to call Face - Detect first to get a |
| | | 82 | | /// valid faceId. Note that this faceId is not persisted and will expire 24 |
| | | 83 | | /// hours after the detection call |
| | | 84 | | /// </param> |
| | | 85 | | /// <param name='faceListId'> |
| | | 86 | | /// An existing user-specified unique candidate face list, created in Face List |
| | | 87 | | /// - Create a Face List. Face list contains a set of persistedFaceIds which |
| | | 88 | | /// are persisted and will never expire. Parameter faceListId, largeFaceListId |
| | | 89 | | /// and faceIds should not be provided at the same time. |
| | | 90 | | /// </param> |
| | | 91 | | /// <param name='largeFaceListId'> |
| | | 92 | | /// An existing user-specified unique candidate large face list, created in |
| | | 93 | | /// LargeFaceList - Create. Large face list contains a set of persistedFaceIds |
| | | 94 | | /// which are persisted and will never expire. Parameter faceListId, |
| | | 95 | | /// largeFaceListId and faceIds should not be provided at the same time. |
| | | 96 | | /// </param> |
| | | 97 | | /// <param name='faceIds'> |
| | | 98 | | /// An array of candidate faceIds. All of them are created by Face - Detect and |
| | | 99 | | /// the faceIds will expire 24 hours after the detection call. The number of |
| | | 100 | | /// faceIds is limited to 1000. Parameter faceListId, largeFaceListId and |
| | | 101 | | /// faceIds should not be provided at the same time. |
| | | 102 | | /// </param> |
| | | 103 | | /// <param name='maxNumOfCandidatesReturned'> |
| | | 104 | | /// The number of top similar faces returned. The valid range is [1, 1000]. |
| | | 105 | | /// </param> |
| | | 106 | | /// <param name='mode'> |
| | | 107 | | /// Similar face searching mode. It can be "matchPerson" or "matchFace". |
| | | 108 | | /// Possible values include: 'matchPerson', 'matchFace' |
| | | 109 | | /// </param> |
| | | 110 | | /// <param name='customHeaders'> |
| | | 111 | | /// Headers that will be added to request. |
| | | 112 | | /// </param> |
| | | 113 | | /// <param name='cancellationToken'> |
| | | 114 | | /// The cancellation token. |
| | | 115 | | /// </param> |
| | | 116 | | /// <exception cref="APIErrorException"> |
| | | 117 | | /// Thrown when the operation returned an invalid status code |
| | | 118 | | /// </exception> |
| | | 119 | | /// <exception cref="SerializationException"> |
| | | 120 | | /// Thrown when unable to deserialize the response |
| | | 121 | | /// </exception> |
| | | 122 | | /// <exception cref="ValidationException"> |
| | | 123 | | /// Thrown when a required parameter is null |
| | | 124 | | /// </exception> |
| | | 125 | | /// <exception cref="System.ArgumentNullException"> |
| | | 126 | | /// Thrown when a required parameter is null |
| | | 127 | | /// </exception> |
| | | 128 | | /// <return> |
| | | 129 | | /// A response object containing the response body and response headers. |
| | | 130 | | /// </return> |
| | | 131 | | public async Task<HttpOperationResponse<IList<SimilarFace>>> FindSimilarWithHttpMessagesAsync(System.Guid faceId |
| | | 132 | | { |
| | 6 | 133 | | if (Client.Endpoint == null) |
| | | 134 | | { |
| | 0 | 135 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 136 | | } |
| | 6 | 137 | | if (faceListId != null) |
| | | 138 | | { |
| | 2 | 139 | | if (faceListId.Length > 64) |
| | | 140 | | { |
| | 0 | 141 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | | 142 | | } |
| | 2 | 143 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | | 144 | | { |
| | 0 | 145 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | | 146 | | } |
| | | 147 | | } |
| | 6 | 148 | | if (largeFaceListId != null) |
| | | 149 | | { |
| | 2 | 150 | | if (largeFaceListId.Length > 64) |
| | | 151 | | { |
| | 0 | 152 | | throw new ValidationException(ValidationRules.MaxLength, "largeFaceListId", 64); |
| | | 153 | | } |
| | 2 | 154 | | if (!System.Text.RegularExpressions.Regex.IsMatch(largeFaceListId, "^[a-z0-9-_]+$")) |
| | | 155 | | { |
| | 0 | 156 | | throw new ValidationException(ValidationRules.Pattern, "largeFaceListId", "^[a-z0-9-_]+$"); |
| | | 157 | | } |
| | | 158 | | } |
| | 6 | 159 | | if (faceIds != null) |
| | | 160 | | { |
| | 2 | 161 | | if (faceIds.Count > 1000) |
| | | 162 | | { |
| | 0 | 163 | | throw new ValidationException(ValidationRules.MaxItems, "faceIds", 1000); |
| | | 164 | | } |
| | | 165 | | } |
| | 6 | 166 | | if (maxNumOfCandidatesReturned > 1000) |
| | | 167 | | { |
| | 0 | 168 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "maxNumOfCandidatesReturned", 1000); |
| | | 169 | | } |
| | 6 | 170 | | if (maxNumOfCandidatesReturned < 1) |
| | | 171 | | { |
| | 0 | 172 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "maxNumOfCandidatesReturned", 1); |
| | | 173 | | } |
| | 6 | 174 | | FindSimilarRequest body = new FindSimilarRequest(); |
| | 6 | 175 | | if (faceListId != null || largeFaceListId != null || faceIds != null || maxNumOfCandidatesReturned != null) |
| | | 176 | | { |
| | 6 | 177 | | body.FaceId = faceId; |
| | 6 | 178 | | body.FaceListId = faceListId; |
| | 6 | 179 | | body.LargeFaceListId = largeFaceListId; |
| | 6 | 180 | | body.FaceIds = faceIds; |
| | 6 | 181 | | body.MaxNumOfCandidatesReturned = maxNumOfCandidatesReturned; |
| | 6 | 182 | | body.Mode = mode; |
| | | 183 | | } |
| | | 184 | | // Tracing |
| | 6 | 185 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 6 | 186 | | string _invocationId = null; |
| | 6 | 187 | | if (_shouldTrace) |
| | | 188 | | { |
| | 0 | 189 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 190 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 191 | | tracingParameters.Add("body", body); |
| | 0 | 192 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 193 | | ServiceClientTracing.Enter(_invocationId, this, "FindSimilar", tracingParameters); |
| | | 194 | | } |
| | | 195 | | // Construct URL |
| | 6 | 196 | | var _baseUrl = Client.BaseUri; |
| | 6 | 197 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "findsimilars"; |
| | 6 | 198 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 199 | | // Create HTTP transport objects |
| | 6 | 200 | | var _httpRequest = new HttpRequestMessage(); |
| | 6 | 201 | | HttpResponseMessage _httpResponse = null; |
| | 6 | 202 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 6 | 203 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 204 | | // Set Headers |
| | | 205 | | |
| | | 206 | | |
| | 6 | 207 | | if (customHeaders != null) |
| | | 208 | | { |
| | 0 | 209 | | foreach(var _header in customHeaders) |
| | | 210 | | { |
| | 0 | 211 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 212 | | { |
| | 0 | 213 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 214 | | } |
| | 0 | 215 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | // Serialize Request |
| | 6 | 220 | | string _requestContent = null; |
| | 6 | 221 | | if(body != null) |
| | | 222 | | { |
| | 6 | 223 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| | 6 | 224 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 6 | 225 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 226 | | } |
| | | 227 | | // Set Credentials |
| | 6 | 228 | | if (Client.Credentials != null) |
| | | 229 | | { |
| | 6 | 230 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 6 | 231 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 232 | | } |
| | | 233 | | // Send Request |
| | 6 | 234 | | if (_shouldTrace) |
| | | 235 | | { |
| | 0 | 236 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 237 | | } |
| | 6 | 238 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 6 | 239 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 6 | 240 | | if (_shouldTrace) |
| | | 241 | | { |
| | 0 | 242 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 243 | | } |
| | 6 | 244 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 6 | 245 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 6 | 246 | | string _responseContent = null; |
| | 6 | 247 | | if ((int)_statusCode != 200) |
| | | 248 | | { |
| | 0 | 249 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 250 | | try |
| | | 251 | | { |
| | 0 | 252 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 253 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 254 | | if (_errorBody != null) |
| | | 255 | | { |
| | 0 | 256 | | ex.Body = _errorBody; |
| | | 257 | | } |
| | 0 | 258 | | } |
| | 0 | 259 | | catch (JsonException) |
| | | 260 | | { |
| | | 261 | | // Ignore the exception |
| | 0 | 262 | | } |
| | 0 | 263 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 264 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 265 | | if (_shouldTrace) |
| | | 266 | | { |
| | 0 | 267 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 268 | | } |
| | 0 | 269 | | _httpRequest.Dispose(); |
| | 0 | 270 | | if (_httpResponse != null) |
| | | 271 | | { |
| | 0 | 272 | | _httpResponse.Dispose(); |
| | | 273 | | } |
| | 0 | 274 | | throw ex; |
| | | 275 | | } |
| | | 276 | | // Create Result |
| | 6 | 277 | | var _result = new HttpOperationResponse<IList<SimilarFace>>(); |
| | 6 | 278 | | _result.Request = _httpRequest; |
| | 6 | 279 | | _result.Response = _httpResponse; |
| | | 280 | | // Deserialize Response |
| | 6 | 281 | | if ((int)_statusCode == 200) |
| | | 282 | | { |
| | 6 | 283 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 284 | | try |
| | | 285 | | { |
| | 6 | 286 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<SimilarFace>>(_responseCon |
| | 6 | 287 | | } |
| | 0 | 288 | | catch (JsonException ex) |
| | | 289 | | { |
| | 0 | 290 | | _httpRequest.Dispose(); |
| | 0 | 291 | | if (_httpResponse != null) |
| | | 292 | | { |
| | 0 | 293 | | _httpResponse.Dispose(); |
| | | 294 | | } |
| | 0 | 295 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 296 | | } |
| | | 297 | | } |
| | 6 | 298 | | if (_shouldTrace) |
| | | 299 | | { |
| | 0 | 300 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 301 | | } |
| | 6 | 302 | | return _result; |
| | 6 | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Divide candidate faces into groups based on face similarity.<br /> |
| | | 307 | | /// * The output is one or more disjointed face groups and a messyGroup. A face |
| | | 308 | | /// group contains faces that have similar looking, often of the same person. |
| | | 309 | | /// Face groups are ranked by group size, i.e. number of faces. Notice that |
| | | 310 | | /// faces belonging to a same person might be split into several groups in the |
| | | 311 | | /// result. |
| | | 312 | | /// * MessyGroup is a special face group containing faces that cannot find any |
| | | 313 | | /// similar counterpart face from original faces. The messyGroup will not |
| | | 314 | | /// appear in the result if all faces found their counterparts. |
| | | 315 | | /// * Group API needs at least 2 candidate faces and 1000 at most. We suggest |
| | | 316 | | /// to try [Face - |
| | | 317 | | /// Verify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a) |
| | | 318 | | /// when you only have 2 candidate faces. |
| | | 319 | | /// * The 'recognitionModel' associated with the query faces' faceIds should be |
| | | 320 | | /// the same. |
| | | 321 | | /// |
| | | 322 | | /// </summary> |
| | | 323 | | /// <param name='faceIds'> |
| | | 324 | | /// Array of candidate faceId created by Face - Detect. The maximum is 1000 |
| | | 325 | | /// faces |
| | | 326 | | /// </param> |
| | | 327 | | /// <param name='customHeaders'> |
| | | 328 | | /// Headers that will be added to request. |
| | | 329 | | /// </param> |
| | | 330 | | /// <param name='cancellationToken'> |
| | | 331 | | /// The cancellation token. |
| | | 332 | | /// </param> |
| | | 333 | | /// <exception cref="APIErrorException"> |
| | | 334 | | /// Thrown when the operation returned an invalid status code |
| | | 335 | | /// </exception> |
| | | 336 | | /// <exception cref="SerializationException"> |
| | | 337 | | /// Thrown when unable to deserialize the response |
| | | 338 | | /// </exception> |
| | | 339 | | /// <exception cref="ValidationException"> |
| | | 340 | | /// Thrown when a required parameter is null |
| | | 341 | | /// </exception> |
| | | 342 | | /// <exception cref="System.ArgumentNullException"> |
| | | 343 | | /// Thrown when a required parameter is null |
| | | 344 | | /// </exception> |
| | | 345 | | /// <return> |
| | | 346 | | /// A response object containing the response body and response headers. |
| | | 347 | | /// </return> |
| | | 348 | | public async Task<HttpOperationResponse<GroupResult>> GroupWithHttpMessagesAsync(IList<System.Guid> faceIds, Dic |
| | | 349 | | { |
| | 0 | 350 | | if (Client.Endpoint == null) |
| | | 351 | | { |
| | 0 | 352 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 353 | | } |
| | 0 | 354 | | if (faceIds == null) |
| | | 355 | | { |
| | 0 | 356 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceIds"); |
| | | 357 | | } |
| | 0 | 358 | | if (faceIds != null) |
| | | 359 | | { |
| | 0 | 360 | | if (faceIds.Count > 1000) |
| | | 361 | | { |
| | 0 | 362 | | throw new ValidationException(ValidationRules.MaxItems, "faceIds", 1000); |
| | | 363 | | } |
| | | 364 | | } |
| | 0 | 365 | | GroupRequest body = new GroupRequest(); |
| | 0 | 366 | | if (faceIds != null) |
| | | 367 | | { |
| | 0 | 368 | | body.FaceIds = faceIds; |
| | | 369 | | } |
| | | 370 | | // Tracing |
| | 0 | 371 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 372 | | string _invocationId = null; |
| | 0 | 373 | | if (_shouldTrace) |
| | | 374 | | { |
| | 0 | 375 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 376 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 377 | | tracingParameters.Add("body", body); |
| | 0 | 378 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 379 | | ServiceClientTracing.Enter(_invocationId, this, "Group", tracingParameters); |
| | | 380 | | } |
| | | 381 | | // Construct URL |
| | 0 | 382 | | var _baseUrl = Client.BaseUri; |
| | 0 | 383 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "group"; |
| | 0 | 384 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 385 | | // Create HTTP transport objects |
| | 0 | 386 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 387 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 388 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 0 | 389 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 390 | | // Set Headers |
| | | 391 | | |
| | | 392 | | |
| | 0 | 393 | | if (customHeaders != null) |
| | | 394 | | { |
| | 0 | 395 | | foreach(var _header in customHeaders) |
| | | 396 | | { |
| | 0 | 397 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 398 | | { |
| | 0 | 399 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 400 | | } |
| | 0 | 401 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 402 | | } |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | // Serialize Request |
| | 0 | 406 | | string _requestContent = null; |
| | 0 | 407 | | if(body != null) |
| | | 408 | | { |
| | 0 | 409 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| | 0 | 410 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 0 | 411 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 412 | | } |
| | | 413 | | // Set Credentials |
| | 0 | 414 | | if (Client.Credentials != null) |
| | | 415 | | { |
| | 0 | 416 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 417 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 418 | | } |
| | | 419 | | // Send Request |
| | 0 | 420 | | if (_shouldTrace) |
| | | 421 | | { |
| | 0 | 422 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 423 | | } |
| | 0 | 424 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 425 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 426 | | if (_shouldTrace) |
| | | 427 | | { |
| | 0 | 428 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 429 | | } |
| | 0 | 430 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 431 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 432 | | string _responseContent = null; |
| | 0 | 433 | | if ((int)_statusCode != 200) |
| | | 434 | | { |
| | 0 | 435 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 436 | | try |
| | | 437 | | { |
| | 0 | 438 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 439 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 440 | | if (_errorBody != null) |
| | | 441 | | { |
| | 0 | 442 | | ex.Body = _errorBody; |
| | | 443 | | } |
| | 0 | 444 | | } |
| | 0 | 445 | | catch (JsonException) |
| | | 446 | | { |
| | | 447 | | // Ignore the exception |
| | 0 | 448 | | } |
| | 0 | 449 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 450 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 451 | | if (_shouldTrace) |
| | | 452 | | { |
| | 0 | 453 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 454 | | } |
| | 0 | 455 | | _httpRequest.Dispose(); |
| | 0 | 456 | | if (_httpResponse != null) |
| | | 457 | | { |
| | 0 | 458 | | _httpResponse.Dispose(); |
| | | 459 | | } |
| | 0 | 460 | | throw ex; |
| | | 461 | | } |
| | | 462 | | // Create Result |
| | 0 | 463 | | var _result = new HttpOperationResponse<GroupResult>(); |
| | 0 | 464 | | _result.Request = _httpRequest; |
| | 0 | 465 | | _result.Response = _httpResponse; |
| | | 466 | | // Deserialize Response |
| | 0 | 467 | | if ((int)_statusCode == 200) |
| | | 468 | | { |
| | 0 | 469 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 470 | | try |
| | | 471 | | { |
| | 0 | 472 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<GroupResult>(_responseContent, C |
| | 0 | 473 | | } |
| | 0 | 474 | | catch (JsonException ex) |
| | | 475 | | { |
| | 0 | 476 | | _httpRequest.Dispose(); |
| | 0 | 477 | | if (_httpResponse != null) |
| | | 478 | | { |
| | 0 | 479 | | _httpResponse.Dispose(); |
| | | 480 | | } |
| | 0 | 481 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 482 | | } |
| | | 483 | | } |
| | 0 | 484 | | if (_shouldTrace) |
| | | 485 | | { |
| | 0 | 486 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 487 | | } |
| | 0 | 488 | | return _result; |
| | 0 | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <summary> |
| | | 492 | | /// 1-to-many identification to find the closest matches of the specific query |
| | | 493 | | /// person face from a person group or large person group. |
| | | 494 | | /// <br/> For each face in the faceIds array, Face Identify will compute |
| | | 495 | | /// similarities between the query face and all the faces in the person group |
| | | 496 | | /// (given by personGroupId) or large person group (given by |
| | | 497 | | /// largePersonGroupId), and return candidate person(s) for that face ranked by |
| | | 498 | | /// similarity confidence. The person group/large person group should be |
| | | 499 | | /// trained to make it ready for identification. See more in [PersonGroup - |
| | | 500 | | /// Train](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395249) |
| | | 501 | | /// and [LargePersonGroup - |
| | | 502 | | /// Train](/docs/services/563879b61984550e40cbbe8d/operations/599ae2d16ac60f11b48b5aa4). |
| | | 503 | | /// <br/> |
| | | 504 | | /// |
| | | 505 | | /// Remarks:<br /> |
| | | 506 | | /// * The algorithm allows more than one face to be identified independently at |
| | | 507 | | /// the same request, but no more than 10 faces. |
| | | 508 | | /// * Each person in the person group/large person group could have more than |
| | | 509 | | /// one face, but no more than 248 faces. |
| | | 510 | | /// * Higher face image quality means better identification precision. Please |
| | | 511 | | /// consider high-quality faces: frontal, clear, and face size is 200x200 |
| | | 512 | | /// pixels (100 pixels between eyes) or bigger. |
| | | 513 | | /// * Number of candidates returned is restricted by maxNumOfCandidatesReturned |
| | | 514 | | /// and confidenceThreshold. If no person is identified, the returned |
| | | 515 | | /// candidates will be an empty array. |
| | | 516 | | /// * Try [Face - Find |
| | | 517 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237) |
| | | 518 | | /// when you need to find similar faces from a face list/large face list |
| | | 519 | | /// instead of a person group/large person group. |
| | | 520 | | /// * The 'recognitionModel' associated with the query faces' faceIds should be |
| | | 521 | | /// the same as the 'recognitionModel' used by the target person group or large |
| | | 522 | | /// person group. |
| | | 523 | | /// |
| | | 524 | | /// </summary> |
| | | 525 | | /// <param name='faceIds'> |
| | | 526 | | /// Array of query faces faceIds, created by the Face - Detect. Each of the |
| | | 527 | | /// faces are identified independently. The valid number of faceIds is between |
| | | 528 | | /// [1, 10]. |
| | | 529 | | /// </param> |
| | | 530 | | /// <param name='personGroupId'> |
| | | 531 | | /// PersonGroupId of the target person group, created by PersonGroup - Create. |
| | | 532 | | /// Parameter personGroupId and largePersonGroupId should not be provided at |
| | | 533 | | /// the same time. |
| | | 534 | | /// </param> |
| | | 535 | | /// <param name='largePersonGroupId'> |
| | | 536 | | /// LargePersonGroupId of the target large person group, created by |
| | | 537 | | /// LargePersonGroup - Create. Parameter personGroupId and largePersonGroupId |
| | | 538 | | /// should not be provided at the same time. |
| | | 539 | | /// </param> |
| | | 540 | | /// <param name='maxNumOfCandidatesReturned'> |
| | | 541 | | /// The range of maxNumOfCandidatesReturned is between 1 and 5 (default is 1). |
| | | 542 | | /// </param> |
| | | 543 | | /// <param name='confidenceThreshold'> |
| | | 544 | | /// Confidence threshold of identification, used to judge whether one face |
| | | 545 | | /// belong to one person. The range of confidenceThreshold is [0, 1] (default |
| | | 546 | | /// specified by algorithm). |
| | | 547 | | /// </param> |
| | | 548 | | /// <param name='customHeaders'> |
| | | 549 | | /// Headers that will be added to request. |
| | | 550 | | /// </param> |
| | | 551 | | /// <param name='cancellationToken'> |
| | | 552 | | /// The cancellation token. |
| | | 553 | | /// </param> |
| | | 554 | | /// <exception cref="APIErrorException"> |
| | | 555 | | /// Thrown when the operation returned an invalid status code |
| | | 556 | | /// </exception> |
| | | 557 | | /// <exception cref="SerializationException"> |
| | | 558 | | /// Thrown when unable to deserialize the response |
| | | 559 | | /// </exception> |
| | | 560 | | /// <exception cref="ValidationException"> |
| | | 561 | | /// Thrown when a required parameter is null |
| | | 562 | | /// </exception> |
| | | 563 | | /// <exception cref="System.ArgumentNullException"> |
| | | 564 | | /// Thrown when a required parameter is null |
| | | 565 | | /// </exception> |
| | | 566 | | /// <return> |
| | | 567 | | /// A response object containing the response body and response headers. |
| | | 568 | | /// </return> |
| | | 569 | | public async Task<HttpOperationResponse<IList<IdentifyResult>>> IdentifyWithHttpMessagesAsync(IList<System.Guid> |
| | | 570 | | { |
| | 4 | 571 | | if (Client.Endpoint == null) |
| | | 572 | | { |
| | 0 | 573 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 574 | | } |
| | 4 | 575 | | if (faceIds == null) |
| | | 576 | | { |
| | 0 | 577 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceIds"); |
| | | 578 | | } |
| | 4 | 579 | | if (faceIds != null) |
| | | 580 | | { |
| | 4 | 581 | | if (faceIds.Count > 10) |
| | | 582 | | { |
| | 0 | 583 | | throw new ValidationException(ValidationRules.MaxItems, "faceIds", 10); |
| | | 584 | | } |
| | | 585 | | } |
| | 4 | 586 | | if (personGroupId != null) |
| | | 587 | | { |
| | 2 | 588 | | if (personGroupId.Length > 64) |
| | | 589 | | { |
| | 0 | 590 | | throw new ValidationException(ValidationRules.MaxLength, "personGroupId", 64); |
| | | 591 | | } |
| | 2 | 592 | | if (!System.Text.RegularExpressions.Regex.IsMatch(personGroupId, "^[a-z0-9-_]+$")) |
| | | 593 | | { |
| | 0 | 594 | | throw new ValidationException(ValidationRules.Pattern, "personGroupId", "^[a-z0-9-_]+$"); |
| | | 595 | | } |
| | | 596 | | } |
| | 4 | 597 | | if (largePersonGroupId != null) |
| | | 598 | | { |
| | 2 | 599 | | if (largePersonGroupId.Length > 64) |
| | | 600 | | { |
| | 0 | 601 | | throw new ValidationException(ValidationRules.MaxLength, "largePersonGroupId", 64); |
| | | 602 | | } |
| | 2 | 603 | | if (!System.Text.RegularExpressions.Regex.IsMatch(largePersonGroupId, "^[a-z0-9-_]+$")) |
| | | 604 | | { |
| | 0 | 605 | | throw new ValidationException(ValidationRules.Pattern, "largePersonGroupId", "^[a-z0-9-_]+$"); |
| | | 606 | | } |
| | | 607 | | } |
| | 4 | 608 | | if (maxNumOfCandidatesReturned > 5) |
| | | 609 | | { |
| | 0 | 610 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "maxNumOfCandidatesReturned", 5); |
| | | 611 | | } |
| | 4 | 612 | | if (maxNumOfCandidatesReturned < 1) |
| | | 613 | | { |
| | 0 | 614 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "maxNumOfCandidatesReturned", 1); |
| | | 615 | | } |
| | 4 | 616 | | IdentifyRequest body = new IdentifyRequest(); |
| | 4 | 617 | | if (faceIds != null || personGroupId != null || largePersonGroupId != null || maxNumOfCandidatesReturned != |
| | | 618 | | { |
| | 4 | 619 | | body.FaceIds = faceIds; |
| | 4 | 620 | | body.PersonGroupId = personGroupId; |
| | 4 | 621 | | body.LargePersonGroupId = largePersonGroupId; |
| | 4 | 622 | | body.MaxNumOfCandidatesReturned = maxNumOfCandidatesReturned; |
| | 4 | 623 | | body.ConfidenceThreshold = confidenceThreshold; |
| | | 624 | | } |
| | | 625 | | // Tracing |
| | 4 | 626 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 4 | 627 | | string _invocationId = null; |
| | 4 | 628 | | if (_shouldTrace) |
| | | 629 | | { |
| | 0 | 630 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 631 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 632 | | tracingParameters.Add("body", body); |
| | 0 | 633 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 634 | | ServiceClientTracing.Enter(_invocationId, this, "Identify", tracingParameters); |
| | | 635 | | } |
| | | 636 | | // Construct URL |
| | 4 | 637 | | var _baseUrl = Client.BaseUri; |
| | 4 | 638 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "identify"; |
| | 4 | 639 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 640 | | // Create HTTP transport objects |
| | 4 | 641 | | var _httpRequest = new HttpRequestMessage(); |
| | 4 | 642 | | HttpResponseMessage _httpResponse = null; |
| | 4 | 643 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 4 | 644 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 645 | | // Set Headers |
| | | 646 | | |
| | | 647 | | |
| | 4 | 648 | | if (customHeaders != null) |
| | | 649 | | { |
| | 0 | 650 | | foreach(var _header in customHeaders) |
| | | 651 | | { |
| | 0 | 652 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 653 | | { |
| | 0 | 654 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 655 | | } |
| | 0 | 656 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 657 | | } |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | // Serialize Request |
| | 4 | 661 | | string _requestContent = null; |
| | 4 | 662 | | if(body != null) |
| | | 663 | | { |
| | 4 | 664 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| | 4 | 665 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 4 | 666 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 667 | | } |
| | | 668 | | // Set Credentials |
| | 4 | 669 | | if (Client.Credentials != null) |
| | | 670 | | { |
| | 4 | 671 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 672 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 673 | | } |
| | | 674 | | // Send Request |
| | 4 | 675 | | if (_shouldTrace) |
| | | 676 | | { |
| | 0 | 677 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 678 | | } |
| | 4 | 679 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 680 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 4 | 681 | | if (_shouldTrace) |
| | | 682 | | { |
| | 0 | 683 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 684 | | } |
| | 4 | 685 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 4 | 686 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 687 | | string _responseContent = null; |
| | 4 | 688 | | if ((int)_statusCode != 200) |
| | | 689 | | { |
| | 0 | 690 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 691 | | try |
| | | 692 | | { |
| | 0 | 693 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 694 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 695 | | if (_errorBody != null) |
| | | 696 | | { |
| | 0 | 697 | | ex.Body = _errorBody; |
| | | 698 | | } |
| | 0 | 699 | | } |
| | 0 | 700 | | catch (JsonException) |
| | | 701 | | { |
| | | 702 | | // Ignore the exception |
| | 0 | 703 | | } |
| | 0 | 704 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 705 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 706 | | if (_shouldTrace) |
| | | 707 | | { |
| | 0 | 708 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 709 | | } |
| | 0 | 710 | | _httpRequest.Dispose(); |
| | 0 | 711 | | if (_httpResponse != null) |
| | | 712 | | { |
| | 0 | 713 | | _httpResponse.Dispose(); |
| | | 714 | | } |
| | 0 | 715 | | throw ex; |
| | | 716 | | } |
| | | 717 | | // Create Result |
| | 4 | 718 | | var _result = new HttpOperationResponse<IList<IdentifyResult>>(); |
| | 4 | 719 | | _result.Request = _httpRequest; |
| | 4 | 720 | | _result.Response = _httpResponse; |
| | | 721 | | // Deserialize Response |
| | 4 | 722 | | if ((int)_statusCode == 200) |
| | | 723 | | { |
| | 4 | 724 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 725 | | try |
| | | 726 | | { |
| | 4 | 727 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<IdentifyResult>>(_response |
| | 4 | 728 | | } |
| | 0 | 729 | | catch (JsonException ex) |
| | | 730 | | { |
| | 0 | 731 | | _httpRequest.Dispose(); |
| | 0 | 732 | | if (_httpResponse != null) |
| | | 733 | | { |
| | 0 | 734 | | _httpResponse.Dispose(); |
| | | 735 | | } |
| | 0 | 736 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 737 | | } |
| | | 738 | | } |
| | 4 | 739 | | if (_shouldTrace) |
| | | 740 | | { |
| | 0 | 741 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 742 | | } |
| | 4 | 743 | | return _result; |
| | 4 | 744 | | } |
| | | 745 | | |
| | | 746 | | /// <summary> |
| | | 747 | | /// Verify whether two faces belong to a same person or whether one face |
| | | 748 | | /// belongs to a person. |
| | | 749 | | /// <br/> |
| | | 750 | | /// Remarks:<br /> |
| | | 751 | | /// * Higher face image quality means better identification precision. Please |
| | | 752 | | /// consider high-quality faces: frontal, clear, and face size is 200x200 |
| | | 753 | | /// pixels (100 pixels between eyes) or bigger. |
| | | 754 | | /// * For the scenarios that are sensitive to accuracy please make your own |
| | | 755 | | /// judgment. |
| | | 756 | | /// * The 'recognitionModel' associated with the query faces' faceIds should be |
| | | 757 | | /// the same as the 'recognitionModel' used by the target face, person group or |
| | | 758 | | /// large person group. |
| | | 759 | | /// |
| | | 760 | | /// </summary> |
| | | 761 | | /// <param name='faceId1'> |
| | | 762 | | /// FaceId of the first face, comes from Face - Detect |
| | | 763 | | /// </param> |
| | | 764 | | /// <param name='faceId2'> |
| | | 765 | | /// FaceId of the second face, comes from Face - Detect |
| | | 766 | | /// </param> |
| | | 767 | | /// <param name='customHeaders'> |
| | | 768 | | /// Headers that will be added to request. |
| | | 769 | | /// </param> |
| | | 770 | | /// <param name='cancellationToken'> |
| | | 771 | | /// The cancellation token. |
| | | 772 | | /// </param> |
| | | 773 | | /// <exception cref="APIErrorException"> |
| | | 774 | | /// Thrown when the operation returned an invalid status code |
| | | 775 | | /// </exception> |
| | | 776 | | /// <exception cref="SerializationException"> |
| | | 777 | | /// Thrown when unable to deserialize the response |
| | | 778 | | /// </exception> |
| | | 779 | | /// <exception cref="ValidationException"> |
| | | 780 | | /// Thrown when a required parameter is null |
| | | 781 | | /// </exception> |
| | | 782 | | /// <exception cref="System.ArgumentNullException"> |
| | | 783 | | /// Thrown when a required parameter is null |
| | | 784 | | /// </exception> |
| | | 785 | | /// <return> |
| | | 786 | | /// A response object containing the response body and response headers. |
| | | 787 | | /// </return> |
| | | 788 | | public async Task<HttpOperationResponse<VerifyResult>> VerifyFaceToFaceWithHttpMessagesAsync(System.Guid faceId1 |
| | | 789 | | { |
| | 4 | 790 | | if (Client.Endpoint == null) |
| | | 791 | | { |
| | 0 | 792 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 793 | | } |
| | 4 | 794 | | VerifyFaceToFaceRequest body = new VerifyFaceToFaceRequest(); |
| | 4 | 795 | | body.FaceId1 = faceId1; |
| | 4 | 796 | | body.FaceId2 = faceId2; |
| | | 797 | | // Tracing |
| | 4 | 798 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 4 | 799 | | string _invocationId = null; |
| | 4 | 800 | | if (_shouldTrace) |
| | | 801 | | { |
| | 0 | 802 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 803 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 804 | | tracingParameters.Add("body", body); |
| | 0 | 805 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 806 | | ServiceClientTracing.Enter(_invocationId, this, "VerifyFaceToFace", tracingParameters); |
| | | 807 | | } |
| | | 808 | | // Construct URL |
| | 4 | 809 | | var _baseUrl = Client.BaseUri; |
| | 4 | 810 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "verify"; |
| | 4 | 811 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 812 | | // Create HTTP transport objects |
| | 4 | 813 | | var _httpRequest = new HttpRequestMessage(); |
| | 4 | 814 | | HttpResponseMessage _httpResponse = null; |
| | 4 | 815 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 4 | 816 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 817 | | // Set Headers |
| | | 818 | | |
| | | 819 | | |
| | 4 | 820 | | if (customHeaders != null) |
| | | 821 | | { |
| | 0 | 822 | | foreach(var _header in customHeaders) |
| | | 823 | | { |
| | 0 | 824 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 825 | | { |
| | 0 | 826 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 827 | | } |
| | 0 | 828 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 829 | | } |
| | | 830 | | } |
| | | 831 | | |
| | | 832 | | // Serialize Request |
| | 4 | 833 | | string _requestContent = null; |
| | 4 | 834 | | if(body != null) |
| | | 835 | | { |
| | 4 | 836 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| | 4 | 837 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 4 | 838 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 839 | | } |
| | | 840 | | // Set Credentials |
| | 4 | 841 | | if (Client.Credentials != null) |
| | | 842 | | { |
| | 4 | 843 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 844 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 845 | | } |
| | | 846 | | // Send Request |
| | 4 | 847 | | if (_shouldTrace) |
| | | 848 | | { |
| | 0 | 849 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 850 | | } |
| | 4 | 851 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 852 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 4 | 853 | | if (_shouldTrace) |
| | | 854 | | { |
| | 0 | 855 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 856 | | } |
| | 4 | 857 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 4 | 858 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 4 | 859 | | string _responseContent = null; |
| | 4 | 860 | | if ((int)_statusCode != 200) |
| | | 861 | | { |
| | 0 | 862 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 863 | | try |
| | | 864 | | { |
| | 0 | 865 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 866 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 867 | | if (_errorBody != null) |
| | | 868 | | { |
| | 0 | 869 | | ex.Body = _errorBody; |
| | | 870 | | } |
| | 0 | 871 | | } |
| | 0 | 872 | | catch (JsonException) |
| | | 873 | | { |
| | | 874 | | // Ignore the exception |
| | 0 | 875 | | } |
| | 0 | 876 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 877 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 878 | | if (_shouldTrace) |
| | | 879 | | { |
| | 0 | 880 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 881 | | } |
| | 0 | 882 | | _httpRequest.Dispose(); |
| | 0 | 883 | | if (_httpResponse != null) |
| | | 884 | | { |
| | 0 | 885 | | _httpResponse.Dispose(); |
| | | 886 | | } |
| | 0 | 887 | | throw ex; |
| | | 888 | | } |
| | | 889 | | // Create Result |
| | 4 | 890 | | var _result = new HttpOperationResponse<VerifyResult>(); |
| | 4 | 891 | | _result.Request = _httpRequest; |
| | 4 | 892 | | _result.Response = _httpResponse; |
| | | 893 | | // Deserialize Response |
| | 4 | 894 | | if ((int)_statusCode == 200) |
| | | 895 | | { |
| | 4 | 896 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 897 | | try |
| | | 898 | | { |
| | 4 | 899 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<VerifyResult>(_responseContent, |
| | 4 | 900 | | } |
| | 0 | 901 | | catch (JsonException ex) |
| | | 902 | | { |
| | 0 | 903 | | _httpRequest.Dispose(); |
| | 0 | 904 | | if (_httpResponse != null) |
| | | 905 | | { |
| | 0 | 906 | | _httpResponse.Dispose(); |
| | | 907 | | } |
| | 0 | 908 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 909 | | } |
| | | 910 | | } |
| | 4 | 911 | | if (_shouldTrace) |
| | | 912 | | { |
| | 0 | 913 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 914 | | } |
| | 4 | 915 | | return _result; |
| | 4 | 916 | | } |
| | | 917 | | |
| | | 918 | | /// <summary> |
| | | 919 | | /// Detect human faces in an image, return face rectangles, and optionally with |
| | | 920 | | /// faceIds, landmarks, and attributes.<br /> |
| | | 921 | | /// * No image will be stored. Only the extracted face feature will be stored |
| | | 922 | | /// on server. The faceId is an identifier of the face feature and will be used |
| | | 923 | | /// in [Face - |
| | | 924 | | /// Identify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239), |
| | | 925 | | /// [Face - |
| | | 926 | | /// Verify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a), |
| | | 927 | | /// and [Face - Find |
| | | 928 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237). |
| | | 929 | | /// The stored face feature(s) will expire and be deleted 24 hours after the |
| | | 930 | | /// original detection call. |
| | | 931 | | /// * Optional parameters include faceId, landmarks, and attributes. Attributes |
| | | 932 | | /// include age, gender, headPose, smile, facialHair, glasses, emotion, hair, |
| | | 933 | | /// makeup, occlusion, accessories, blur, exposure and noise. Some of the |
| | | 934 | | /// results returned for specific attributes may not be highly accurate. |
| | | 935 | | /// * JPEG, PNG, GIF (the first frame), and BMP format are supported. The |
| | | 936 | | /// allowed image file size is from 1KB to 6MB. |
| | | 937 | | /// * Up to 100 faces can be returned for an image. Faces are ranked by face |
| | | 938 | | /// rectangle size from large to small. |
| | | 939 | | /// * For optimal results when querying [Face - |
| | | 940 | | /// Identify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239), |
| | | 941 | | /// [Face - |
| | | 942 | | /// Verify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a), |
| | | 943 | | /// and [Face - Find |
| | | 944 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237) |
| | | 945 | | /// ('returnFaceId' is true), please use faces that are: frontal, clear, and |
| | | 946 | | /// with a minimum size of 200x200 pixels (100 pixels between eyes). |
| | | 947 | | /// * The minimum detectable face size is 36x36 pixels in an image no larger |
| | | 948 | | /// than 1920x1080 pixels. Images with dimensions higher than 1920x1080 pixels |
| | | 949 | | /// will need a proportionally larger minimum face size. |
| | | 950 | | /// * Different 'detectionModel' values can be provided. To use and compare |
| | | 951 | | /// different detection models, please refer to [How to specify a detection |
| | | 952 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-detecti |
| | | 953 | | /// | Model | Recommended use-case(s) | |
| | | 954 | | /// | ---------- | -------- | |
| | | 955 | | /// | 'detection_01': | The default detection model for [Face - |
| | | 956 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | | 957 | | /// Recommend for near frontal face detection. For scenarios with exceptionally |
| | | 958 | | /// large angle (head-pose) faces, occluded faces or wrong image orientation, |
| | | 959 | | /// the faces in such cases may not be detected. | |
| | | 960 | | /// | 'detection_02': | Detection model released in 2019 May with improved |
| | | 961 | | /// accuracy especially on small, side and blurry faces. | |
| | | 962 | | /// |
| | | 963 | | /// * Different 'recognitionModel' values are provided. If follow-up operations |
| | | 964 | | /// like Verify, Identify, Find Similar are needed, please specify the |
| | | 965 | | /// recognition model with 'recognitionModel' parameter. The default value for |
| | | 966 | | /// 'recognitionModel' is 'recognition_01', if latest model needed, please |
| | | 967 | | /// explicitly specify the model you need in this parameter. Once specified, |
| | | 968 | | /// the detected faceIds will be associated with the specified recognition |
| | | 969 | | /// model. More details, please refer to [How to specify a recognition |
| | | 970 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-recogni |
| | | 971 | | /// | Model | Recommended use-case(s) | |
| | | 972 | | /// | ---------- | -------- | |
| | | 973 | | /// | 'recognition_01': | The default recognition model for [Face - |
| | | 974 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | | 975 | | /// All those faceIds created before 2019 March are bonded with this |
| | | 976 | | /// recognition model. | |
| | | 977 | | /// | 'recognition_02': | Recognition model released in 2019 March. |
| | | 978 | | /// 'recognition_02' is recommended since its overall accuracy is improved |
| | | 979 | | /// compared with 'recognition_01'. | |
| | | 980 | | /// </summary> |
| | | 981 | | /// <param name='url'> |
| | | 982 | | /// Publicly reachable URL of an image |
| | | 983 | | /// </param> |
| | | 984 | | /// <param name='returnFaceId'> |
| | | 985 | | /// A value indicating whether the operation should return faceIds of detected |
| | | 986 | | /// faces. |
| | | 987 | | /// </param> |
| | | 988 | | /// <param name='returnFaceLandmarks'> |
| | | 989 | | /// A value indicating whether the operation should return landmarks of the |
| | | 990 | | /// detected faces. |
| | | 991 | | /// </param> |
| | | 992 | | /// <param name='returnFaceAttributes'> |
| | | 993 | | /// Analyze and return the one or more specified face attributes in the |
| | | 994 | | /// comma-separated string like "returnFaceAttributes=age,gender". Supported |
| | | 995 | | /// face attributes include age, gender, headPose, smile, facialHair, glasses |
| | | 996 | | /// and emotion. Note that each face attribute analysis has additional |
| | | 997 | | /// computational and time cost. |
| | | 998 | | /// </param> |
| | | 999 | | /// <param name='recognitionModel'> |
| | | 1000 | | /// Name of recognition model. Recognition model is used when the face features |
| | | 1001 | | /// are extracted and associated with detected faceIds, (Large)FaceList or |
| | | 1002 | | /// (Large)PersonGroup. A recognition model name can be provided when |
| | | 1003 | | /// performing Face - Detect or (Large)FaceList - Create or (Large)PersonGroup |
| | | 1004 | | /// - Create. The default value is 'recognition_01', if latest model needed, |
| | | 1005 | | /// please explicitly specify the model you need. Possible values include: |
| | | 1006 | | /// 'recognition_01', 'recognition_02' |
| | | 1007 | | /// </param> |
| | | 1008 | | /// <param name='returnRecognitionModel'> |
| | | 1009 | | /// A value indicating whether the operation should return 'recognitionModel' |
| | | 1010 | | /// in response. |
| | | 1011 | | /// </param> |
| | | 1012 | | /// <param name='detectionModel'> |
| | | 1013 | | /// Name of detection model. Detection model is used to detect faces in the |
| | | 1014 | | /// submitted image. A detection model name can be provided when performing |
| | | 1015 | | /// Face - Detect or (Large)FaceList - Add Face or (Large)PersonGroup - Add |
| | | 1016 | | /// Face. The default value is 'detection_01', if another model is needed, |
| | | 1017 | | /// please explicitly specify it. Possible values include: 'detection_01', |
| | | 1018 | | /// 'detection_02' |
| | | 1019 | | /// </param> |
| | | 1020 | | /// <param name='customHeaders'> |
| | | 1021 | | /// Headers that will be added to request. |
| | | 1022 | | /// </param> |
| | | 1023 | | /// <param name='cancellationToken'> |
| | | 1024 | | /// The cancellation token. |
| | | 1025 | | /// </param> |
| | | 1026 | | /// <exception cref="APIErrorException"> |
| | | 1027 | | /// Thrown when the operation returned an invalid status code |
| | | 1028 | | /// </exception> |
| | | 1029 | | /// <exception cref="SerializationException"> |
| | | 1030 | | /// Thrown when unable to deserialize the response |
| | | 1031 | | /// </exception> |
| | | 1032 | | /// <exception cref="ValidationException"> |
| | | 1033 | | /// Thrown when a required parameter is null |
| | | 1034 | | /// </exception> |
| | | 1035 | | /// <exception cref="System.ArgumentNullException"> |
| | | 1036 | | /// Thrown when a required parameter is null |
| | | 1037 | | /// </exception> |
| | | 1038 | | /// <return> |
| | | 1039 | | /// A response object containing the response body and response headers. |
| | | 1040 | | /// </return> |
| | | 1041 | | public async Task<HttpOperationResponse<IList<DetectedFace>>> DetectWithUrlWithHttpMessagesAsync(string url, boo |
| | | 1042 | | { |
| | 0 | 1043 | | if (Client.Endpoint == null) |
| | | 1044 | | { |
| | 0 | 1045 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 1046 | | } |
| | 0 | 1047 | | if (url == null) |
| | | 1048 | | { |
| | 0 | 1049 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | | 1050 | | } |
| | 0 | 1051 | | ImageUrl imageUrl = new ImageUrl(); |
| | 0 | 1052 | | if (url != null) |
| | | 1053 | | { |
| | 0 | 1054 | | imageUrl.Url = url; |
| | | 1055 | | } |
| | | 1056 | | // Tracing |
| | 0 | 1057 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 1058 | | string _invocationId = null; |
| | 0 | 1059 | | if (_shouldTrace) |
| | | 1060 | | { |
| | 0 | 1061 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 1062 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 1063 | | tracingParameters.Add("returnFaceId", returnFaceId); |
| | 0 | 1064 | | tracingParameters.Add("returnFaceLandmarks", returnFaceLandmarks); |
| | 0 | 1065 | | tracingParameters.Add("returnFaceAttributes", returnFaceAttributes); |
| | 0 | 1066 | | tracingParameters.Add("recognitionModel", recognitionModel); |
| | 0 | 1067 | | tracingParameters.Add("returnRecognitionModel", returnRecognitionModel); |
| | 0 | 1068 | | tracingParameters.Add("detectionModel", detectionModel); |
| | 0 | 1069 | | tracingParameters.Add("imageUrl", imageUrl); |
| | 0 | 1070 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 1071 | | ServiceClientTracing.Enter(_invocationId, this, "DetectWithUrl", tracingParameters); |
| | | 1072 | | } |
| | | 1073 | | // Construct URL |
| | 0 | 1074 | | var _baseUrl = Client.BaseUri; |
| | 0 | 1075 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "detect"; |
| | 0 | 1076 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 0 | 1077 | | List<string> _queryParameters = new List<string>(); |
| | 0 | 1078 | | if (returnFaceId != null) |
| | | 1079 | | { |
| | 0 | 1080 | | _queryParameters.Add(string.Format("returnFaceId={0}", System.Uri.EscapeDataString(Rest.Serialization.Sa |
| | | 1081 | | } |
| | 0 | 1082 | | if (returnFaceLandmarks != null) |
| | | 1083 | | { |
| | 0 | 1084 | | _queryParameters.Add(string.Format("returnFaceLandmarks={0}", System.Uri.EscapeDataString(Rest.Serializa |
| | | 1085 | | } |
| | 0 | 1086 | | if (returnFaceAttributes != null) |
| | | 1087 | | { |
| | 0 | 1088 | | _queryParameters.Add(string.Format("returnFaceAttributes={0}", System.Uri.EscapeDataString(string.Join(" |
| | | 1089 | | } |
| | 0 | 1090 | | if (recognitionModel != null) |
| | | 1091 | | { |
| | 0 | 1092 | | _queryParameters.Add(string.Format("recognitionModel={0}", System.Uri.EscapeDataString(recognitionModel) |
| | | 1093 | | } |
| | 0 | 1094 | | if (returnRecognitionModel != null) |
| | | 1095 | | { |
| | 0 | 1096 | | _queryParameters.Add(string.Format("returnRecognitionModel={0}", System.Uri.EscapeDataString(Rest.Serial |
| | | 1097 | | } |
| | 0 | 1098 | | if (detectionModel != null) |
| | | 1099 | | { |
| | 0 | 1100 | | _queryParameters.Add(string.Format("detectionModel={0}", System.Uri.EscapeDataString(detectionModel))); |
| | | 1101 | | } |
| | 0 | 1102 | | if (_queryParameters.Count > 0) |
| | | 1103 | | { |
| | 0 | 1104 | | _url += "?" + string.Join("&", _queryParameters); |
| | | 1105 | | } |
| | | 1106 | | // Create HTTP transport objects |
| | 0 | 1107 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 1108 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 1109 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 0 | 1110 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 1111 | | // Set Headers |
| | | 1112 | | |
| | | 1113 | | |
| | 0 | 1114 | | if (customHeaders != null) |
| | | 1115 | | { |
| | 0 | 1116 | | foreach(var _header in customHeaders) |
| | | 1117 | | { |
| | 0 | 1118 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 1119 | | { |
| | 0 | 1120 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 1121 | | } |
| | 0 | 1122 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 1123 | | } |
| | | 1124 | | } |
| | | 1125 | | |
| | | 1126 | | // Serialize Request |
| | 0 | 1127 | | string _requestContent = null; |
| | 0 | 1128 | | if(imageUrl != null) |
| | | 1129 | | { |
| | 0 | 1130 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(imageUrl, Client.SerializationSetti |
| | 0 | 1131 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 0 | 1132 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 1133 | | } |
| | | 1134 | | // Set Credentials |
| | 0 | 1135 | | if (Client.Credentials != null) |
| | | 1136 | | { |
| | 0 | 1137 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1138 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 1139 | | } |
| | | 1140 | | // Send Request |
| | 0 | 1141 | | if (_shouldTrace) |
| | | 1142 | | { |
| | 0 | 1143 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 1144 | | } |
| | 0 | 1145 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1146 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 1147 | | if (_shouldTrace) |
| | | 1148 | | { |
| | 0 | 1149 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 1150 | | } |
| | 0 | 1151 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 1152 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1153 | | string _responseContent = null; |
| | 0 | 1154 | | if ((int)_statusCode != 200) |
| | | 1155 | | { |
| | 0 | 1156 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 1157 | | try |
| | | 1158 | | { |
| | 0 | 1159 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 1160 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 1161 | | if (_errorBody != null) |
| | | 1162 | | { |
| | 0 | 1163 | | ex.Body = _errorBody; |
| | | 1164 | | } |
| | 0 | 1165 | | } |
| | 0 | 1166 | | catch (JsonException) |
| | | 1167 | | { |
| | | 1168 | | // Ignore the exception |
| | 0 | 1169 | | } |
| | 0 | 1170 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 1171 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 1172 | | if (_shouldTrace) |
| | | 1173 | | { |
| | 0 | 1174 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 1175 | | } |
| | 0 | 1176 | | _httpRequest.Dispose(); |
| | 0 | 1177 | | if (_httpResponse != null) |
| | | 1178 | | { |
| | 0 | 1179 | | _httpResponse.Dispose(); |
| | | 1180 | | } |
| | 0 | 1181 | | throw ex; |
| | | 1182 | | } |
| | | 1183 | | // Create Result |
| | 0 | 1184 | | var _result = new HttpOperationResponse<IList<DetectedFace>>(); |
| | 0 | 1185 | | _result.Request = _httpRequest; |
| | 0 | 1186 | | _result.Response = _httpResponse; |
| | | 1187 | | // Deserialize Response |
| | 0 | 1188 | | if ((int)_statusCode == 200) |
| | | 1189 | | { |
| | 0 | 1190 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 1191 | | try |
| | | 1192 | | { |
| | 0 | 1193 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<DetectedFace>>(_responseCo |
| | 0 | 1194 | | } |
| | 0 | 1195 | | catch (JsonException ex) |
| | | 1196 | | { |
| | 0 | 1197 | | _httpRequest.Dispose(); |
| | 0 | 1198 | | if (_httpResponse != null) |
| | | 1199 | | { |
| | 0 | 1200 | | _httpResponse.Dispose(); |
| | | 1201 | | } |
| | 0 | 1202 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 1203 | | } |
| | | 1204 | | } |
| | 0 | 1205 | | if (_shouldTrace) |
| | | 1206 | | { |
| | 0 | 1207 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 1208 | | } |
| | 0 | 1209 | | return _result; |
| | 0 | 1210 | | } |
| | | 1211 | | |
| | | 1212 | | /// <summary> |
| | | 1213 | | /// Verify whether two faces belong to a same person. Compares a face Id with a |
| | | 1214 | | /// Person Id |
| | | 1215 | | /// </summary> |
| | | 1216 | | /// <param name='faceId'> |
| | | 1217 | | /// FaceId of the face, comes from Face - Detect |
| | | 1218 | | /// </param> |
| | | 1219 | | /// <param name='personId'> |
| | | 1220 | | /// Specify a certain person in a person group or a large person group. |
| | | 1221 | | /// personId is created in PersonGroup Person - Create or LargePersonGroup |
| | | 1222 | | /// Person - Create. |
| | | 1223 | | /// </param> |
| | | 1224 | | /// <param name='personGroupId'> |
| | | 1225 | | /// Using existing personGroupId and personId for fast loading a specified |
| | | 1226 | | /// person. personGroupId is created in PersonGroup - Create. Parameter |
| | | 1227 | | /// personGroupId and largePersonGroupId should not be provided at the same |
| | | 1228 | | /// time. |
| | | 1229 | | /// </param> |
| | | 1230 | | /// <param name='largePersonGroupId'> |
| | | 1231 | | /// Using existing largePersonGroupId and personId for fast loading a specified |
| | | 1232 | | /// person. largePersonGroupId is created in LargePersonGroup - Create. |
| | | 1233 | | /// Parameter personGroupId and largePersonGroupId should not be provided at |
| | | 1234 | | /// the same time. |
| | | 1235 | | /// </param> |
| | | 1236 | | /// <param name='customHeaders'> |
| | | 1237 | | /// Headers that will be added to request. |
| | | 1238 | | /// </param> |
| | | 1239 | | /// <param name='cancellationToken'> |
| | | 1240 | | /// The cancellation token. |
| | | 1241 | | /// </param> |
| | | 1242 | | /// <exception cref="APIErrorException"> |
| | | 1243 | | /// Thrown when the operation returned an invalid status code |
| | | 1244 | | /// </exception> |
| | | 1245 | | /// <exception cref="SerializationException"> |
| | | 1246 | | /// Thrown when unable to deserialize the response |
| | | 1247 | | /// </exception> |
| | | 1248 | | /// <exception cref="ValidationException"> |
| | | 1249 | | /// Thrown when a required parameter is null |
| | | 1250 | | /// </exception> |
| | | 1251 | | /// <exception cref="System.ArgumentNullException"> |
| | | 1252 | | /// Thrown when a required parameter is null |
| | | 1253 | | /// </exception> |
| | | 1254 | | /// <return> |
| | | 1255 | | /// A response object containing the response body and response headers. |
| | | 1256 | | /// </return> |
| | | 1257 | | public async Task<HttpOperationResponse<VerifyResult>> VerifyFaceToPersonWithHttpMessagesAsync(System.Guid faceI |
| | | 1258 | | { |
| | 8 | 1259 | | if (Client.Endpoint == null) |
| | | 1260 | | { |
| | 0 | 1261 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 1262 | | } |
| | 8 | 1263 | | if (personGroupId != null) |
| | | 1264 | | { |
| | 4 | 1265 | | if (personGroupId.Length > 64) |
| | | 1266 | | { |
| | 0 | 1267 | | throw new ValidationException(ValidationRules.MaxLength, "personGroupId", 64); |
| | | 1268 | | } |
| | 4 | 1269 | | if (!System.Text.RegularExpressions.Regex.IsMatch(personGroupId, "^[a-z0-9-_]+$")) |
| | | 1270 | | { |
| | 0 | 1271 | | throw new ValidationException(ValidationRules.Pattern, "personGroupId", "^[a-z0-9-_]+$"); |
| | | 1272 | | } |
| | | 1273 | | } |
| | 8 | 1274 | | if (largePersonGroupId != null) |
| | | 1275 | | { |
| | 4 | 1276 | | if (largePersonGroupId.Length > 64) |
| | | 1277 | | { |
| | 0 | 1278 | | throw new ValidationException(ValidationRules.MaxLength, "largePersonGroupId", 64); |
| | | 1279 | | } |
| | 4 | 1280 | | if (!System.Text.RegularExpressions.Regex.IsMatch(largePersonGroupId, "^[a-z0-9-_]+$")) |
| | | 1281 | | { |
| | 0 | 1282 | | throw new ValidationException(ValidationRules.Pattern, "largePersonGroupId", "^[a-z0-9-_]+$"); |
| | | 1283 | | } |
| | | 1284 | | } |
| | 8 | 1285 | | VerifyFaceToPersonRequest body = new VerifyFaceToPersonRequest(); |
| | 8 | 1286 | | if (personGroupId != null || largePersonGroupId != null) |
| | | 1287 | | { |
| | 8 | 1288 | | body.FaceId = faceId; |
| | 8 | 1289 | | body.PersonGroupId = personGroupId; |
| | 8 | 1290 | | body.LargePersonGroupId = largePersonGroupId; |
| | 8 | 1291 | | body.PersonId = personId; |
| | | 1292 | | } |
| | | 1293 | | // Tracing |
| | 8 | 1294 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 8 | 1295 | | string _invocationId = null; |
| | 8 | 1296 | | if (_shouldTrace) |
| | | 1297 | | { |
| | 0 | 1298 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 1299 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 1300 | | tracingParameters.Add("body", body); |
| | 0 | 1301 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 1302 | | ServiceClientTracing.Enter(_invocationId, this, "VerifyFaceToPerson", tracingParameters); |
| | | 1303 | | } |
| | | 1304 | | // Construct URL |
| | 8 | 1305 | | var _baseUrl = Client.BaseUri; |
| | 8 | 1306 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "verify"; |
| | 8 | 1307 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 1308 | | // Create HTTP transport objects |
| | 8 | 1309 | | var _httpRequest = new HttpRequestMessage(); |
| | 8 | 1310 | | HttpResponseMessage _httpResponse = null; |
| | 8 | 1311 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 8 | 1312 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 1313 | | // Set Headers |
| | | 1314 | | |
| | | 1315 | | |
| | 8 | 1316 | | if (customHeaders != null) |
| | | 1317 | | { |
| | 0 | 1318 | | foreach(var _header in customHeaders) |
| | | 1319 | | { |
| | 0 | 1320 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 1321 | | { |
| | 0 | 1322 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 1323 | | } |
| | 0 | 1324 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 1325 | | } |
| | | 1326 | | } |
| | | 1327 | | |
| | | 1328 | | // Serialize Request |
| | 8 | 1329 | | string _requestContent = null; |
| | 8 | 1330 | | if(body != null) |
| | | 1331 | | { |
| | 8 | 1332 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| | 8 | 1333 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 8 | 1334 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 1335 | | } |
| | | 1336 | | // Set Credentials |
| | 8 | 1337 | | if (Client.Credentials != null) |
| | | 1338 | | { |
| | 8 | 1339 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 8 | 1340 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 1341 | | } |
| | | 1342 | | // Send Request |
| | 8 | 1343 | | if (_shouldTrace) |
| | | 1344 | | { |
| | 0 | 1345 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 1346 | | } |
| | 8 | 1347 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 8 | 1348 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 8 | 1349 | | if (_shouldTrace) |
| | | 1350 | | { |
| | 0 | 1351 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 1352 | | } |
| | 8 | 1353 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 8 | 1354 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 8 | 1355 | | string _responseContent = null; |
| | 8 | 1356 | | if ((int)_statusCode != 200) |
| | | 1357 | | { |
| | 0 | 1358 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 1359 | | try |
| | | 1360 | | { |
| | 0 | 1361 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 1362 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 1363 | | if (_errorBody != null) |
| | | 1364 | | { |
| | 0 | 1365 | | ex.Body = _errorBody; |
| | | 1366 | | } |
| | 0 | 1367 | | } |
| | 0 | 1368 | | catch (JsonException) |
| | | 1369 | | { |
| | | 1370 | | // Ignore the exception |
| | 0 | 1371 | | } |
| | 0 | 1372 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 1373 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 1374 | | if (_shouldTrace) |
| | | 1375 | | { |
| | 0 | 1376 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 1377 | | } |
| | 0 | 1378 | | _httpRequest.Dispose(); |
| | 0 | 1379 | | if (_httpResponse != null) |
| | | 1380 | | { |
| | 0 | 1381 | | _httpResponse.Dispose(); |
| | | 1382 | | } |
| | 0 | 1383 | | throw ex; |
| | | 1384 | | } |
| | | 1385 | | // Create Result |
| | 8 | 1386 | | var _result = new HttpOperationResponse<VerifyResult>(); |
| | 8 | 1387 | | _result.Request = _httpRequest; |
| | 8 | 1388 | | _result.Response = _httpResponse; |
| | | 1389 | | // Deserialize Response |
| | 8 | 1390 | | if ((int)_statusCode == 200) |
| | | 1391 | | { |
| | 8 | 1392 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 1393 | | try |
| | | 1394 | | { |
| | 8 | 1395 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<VerifyResult>(_responseContent, |
| | 8 | 1396 | | } |
| | 0 | 1397 | | catch (JsonException ex) |
| | | 1398 | | { |
| | 0 | 1399 | | _httpRequest.Dispose(); |
| | 0 | 1400 | | if (_httpResponse != null) |
| | | 1401 | | { |
| | 0 | 1402 | | _httpResponse.Dispose(); |
| | | 1403 | | } |
| | 0 | 1404 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 1405 | | } |
| | | 1406 | | } |
| | 8 | 1407 | | if (_shouldTrace) |
| | | 1408 | | { |
| | 0 | 1409 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 1410 | | } |
| | 8 | 1411 | | return _result; |
| | 8 | 1412 | | } |
| | | 1413 | | |
| | | 1414 | | /// <summary> |
| | | 1415 | | /// Detect human faces in an image, return face rectangles, and optionally with |
| | | 1416 | | /// faceIds, landmarks, and attributes.<br /> |
| | | 1417 | | /// * No image will be stored. Only the extracted face feature will be stored |
| | | 1418 | | /// on server. The faceId is an identifier of the face feature and will be used |
| | | 1419 | | /// in [Face - |
| | | 1420 | | /// Identify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239), |
| | | 1421 | | /// [Face - |
| | | 1422 | | /// Verify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a), |
| | | 1423 | | /// and [Face - Find |
| | | 1424 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237). |
| | | 1425 | | /// The stored face feature(s) will expire and be deleted 24 hours after the |
| | | 1426 | | /// original detection call. |
| | | 1427 | | /// * Optional parameters include faceId, landmarks, and attributes. Attributes |
| | | 1428 | | /// include age, gender, headPose, smile, facialHair, glasses, emotion, hair, |
| | | 1429 | | /// makeup, occlusion, accessories, blur, exposure and noise. Some of the |
| | | 1430 | | /// results returned for specific attributes may not be highly accurate. |
| | | 1431 | | /// * JPEG, PNG, GIF (the first frame), and BMP format are supported. The |
| | | 1432 | | /// allowed image file size is from 1KB to 6MB. |
| | | 1433 | | /// * Up to 100 faces can be returned for an image. Faces are ranked by face |
| | | 1434 | | /// rectangle size from large to small. |
| | | 1435 | | /// * For optimal results when querying [Face - |
| | | 1436 | | /// Identify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239), |
| | | 1437 | | /// [Face - |
| | | 1438 | | /// Verify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a), |
| | | 1439 | | /// and [Face - Find |
| | | 1440 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237) |
| | | 1441 | | /// ('returnFaceId' is true), please use faces that are: frontal, clear, and |
| | | 1442 | | /// with a minimum size of 200x200 pixels (100 pixels between eyes). |
| | | 1443 | | /// * The minimum detectable face size is 36x36 pixels in an image no larger |
| | | 1444 | | /// than 1920x1080 pixels. Images with dimensions higher than 1920x1080 pixels |
| | | 1445 | | /// will need a proportionally larger minimum face size. |
| | | 1446 | | /// * Different 'detectionModel' values can be provided. To use and compare |
| | | 1447 | | /// different detection models, please refer to [How to specify a detection |
| | | 1448 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-detecti |
| | | 1449 | | /// | Model | Recommended use-case(s) | |
| | | 1450 | | /// | ---------- | -------- | |
| | | 1451 | | /// | 'detection_01': | The default detection model for [Face - |
| | | 1452 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | | 1453 | | /// Recommend for near frontal face detection. For scenarios with exceptionally |
| | | 1454 | | /// large angle (head-pose) faces, occluded faces or wrong image orientation, |
| | | 1455 | | /// the faces in such cases may not be detected. | |
| | | 1456 | | /// | 'detection_02': | Detection model released in 2019 May with improved |
| | | 1457 | | /// accuracy especially on small, side and blurry faces. | |
| | | 1458 | | /// |
| | | 1459 | | /// * Different 'recognitionModel' values are provided. If follow-up operations |
| | | 1460 | | /// like Verify, Identify, Find Similar are needed, please specify the |
| | | 1461 | | /// recognition model with 'recognitionModel' parameter. The default value for |
| | | 1462 | | /// 'recognitionModel' is 'recognition_01', if latest model needed, please |
| | | 1463 | | /// explicitly specify the model you need in this parameter. Once specified, |
| | | 1464 | | /// the detected faceIds will be associated with the specified recognition |
| | | 1465 | | /// model. More details, please refer to [How to specify a recognition |
| | | 1466 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-recogni |
| | | 1467 | | /// | Model | Recommended use-case(s) | |
| | | 1468 | | /// | ---------- | -------- | |
| | | 1469 | | /// | 'recognition_01': | The default recognition model for [Face - |
| | | 1470 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | | 1471 | | /// All those faceIds created before 2019 March are bonded with this |
| | | 1472 | | /// recognition model. | |
| | | 1473 | | /// | 'recognition_02': | Recognition model released in 2019 March. |
| | | 1474 | | /// 'recognition_02' is recommended since its overall accuracy is improved |
| | | 1475 | | /// compared with 'recognition_01'. | |
| | | 1476 | | /// </summary> |
| | | 1477 | | /// <param name='image'> |
| | | 1478 | | /// An image stream. |
| | | 1479 | | /// </param> |
| | | 1480 | | /// <param name='returnFaceId'> |
| | | 1481 | | /// A value indicating whether the operation should return faceIds of detected |
| | | 1482 | | /// faces. |
| | | 1483 | | /// </param> |
| | | 1484 | | /// <param name='returnFaceLandmarks'> |
| | | 1485 | | /// A value indicating whether the operation should return landmarks of the |
| | | 1486 | | /// detected faces. |
| | | 1487 | | /// </param> |
| | | 1488 | | /// <param name='returnFaceAttributes'> |
| | | 1489 | | /// Analyze and return the one or more specified face attributes in the |
| | | 1490 | | /// comma-separated string like "returnFaceAttributes=age,gender". Supported |
| | | 1491 | | /// face attributes include age, gender, headPose, smile, facialHair, glasses |
| | | 1492 | | /// and emotion. Note that each face attribute analysis has additional |
| | | 1493 | | /// computational and time cost. |
| | | 1494 | | /// </param> |
| | | 1495 | | /// <param name='recognitionModel'> |
| | | 1496 | | /// Name of recognition model. Recognition model is used when the face features |
| | | 1497 | | /// are extracted and associated with detected faceIds, (Large)FaceList or |
| | | 1498 | | /// (Large)PersonGroup. A recognition model name can be provided when |
| | | 1499 | | /// performing Face - Detect or (Large)FaceList - Create or (Large)PersonGroup |
| | | 1500 | | /// - Create. The default value is 'recognition_01', if latest model needed, |
| | | 1501 | | /// please explicitly specify the model you need. Possible values include: |
| | | 1502 | | /// 'recognition_01', 'recognition_02' |
| | | 1503 | | /// </param> |
| | | 1504 | | /// <param name='returnRecognitionModel'> |
| | | 1505 | | /// A value indicating whether the operation should return 'recognitionModel' |
| | | 1506 | | /// in response. |
| | | 1507 | | /// </param> |
| | | 1508 | | /// <param name='detectionModel'> |
| | | 1509 | | /// Name of detection model. Detection model is used to detect faces in the |
| | | 1510 | | /// submitted image. A detection model name can be provided when performing |
| | | 1511 | | /// Face - Detect or (Large)FaceList - Add Face or (Large)PersonGroup - Add |
| | | 1512 | | /// Face. The default value is 'detection_01', if another model is needed, |
| | | 1513 | | /// please explicitly specify it. Possible values include: 'detection_01', |
| | | 1514 | | /// 'detection_02' |
| | | 1515 | | /// </param> |
| | | 1516 | | /// <param name='customHeaders'> |
| | | 1517 | | /// Headers that will be added to request. |
| | | 1518 | | /// </param> |
| | | 1519 | | /// <param name='cancellationToken'> |
| | | 1520 | | /// The cancellation token. |
| | | 1521 | | /// </param> |
| | | 1522 | | /// <exception cref="APIErrorException"> |
| | | 1523 | | /// Thrown when the operation returned an invalid status code |
| | | 1524 | | /// </exception> |
| | | 1525 | | /// <exception cref="SerializationException"> |
| | | 1526 | | /// Thrown when unable to deserialize the response |
| | | 1527 | | /// </exception> |
| | | 1528 | | /// <exception cref="ValidationException"> |
| | | 1529 | | /// Thrown when a required parameter is null |
| | | 1530 | | /// </exception> |
| | | 1531 | | /// <exception cref="System.ArgumentNullException"> |
| | | 1532 | | /// Thrown when a required parameter is null |
| | | 1533 | | /// </exception> |
| | | 1534 | | /// <return> |
| | | 1535 | | /// A response object containing the response body and response headers. |
| | | 1536 | | /// </return> |
| | | 1537 | | public async Task<HttpOperationResponse<IList<DetectedFace>>> DetectWithStreamWithHttpMessagesAsync(Stream image |
| | | 1538 | | { |
| | 90 | 1539 | | if (Client.Endpoint == null) |
| | | 1540 | | { |
| | 0 | 1541 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 1542 | | } |
| | 90 | 1543 | | if (image == null) |
| | | 1544 | | { |
| | 0 | 1545 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | | 1546 | | } |
| | | 1547 | | // Tracing |
| | 90 | 1548 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 90 | 1549 | | string _invocationId = null; |
| | 90 | 1550 | | if (_shouldTrace) |
| | | 1551 | | { |
| | 0 | 1552 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 1553 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 1554 | | tracingParameters.Add("returnFaceId", returnFaceId); |
| | 0 | 1555 | | tracingParameters.Add("returnFaceLandmarks", returnFaceLandmarks); |
| | 0 | 1556 | | tracingParameters.Add("returnFaceAttributes", returnFaceAttributes); |
| | 0 | 1557 | | tracingParameters.Add("image", image); |
| | 0 | 1558 | | tracingParameters.Add("recognitionModel", recognitionModel); |
| | 0 | 1559 | | tracingParameters.Add("returnRecognitionModel", returnRecognitionModel); |
| | 0 | 1560 | | tracingParameters.Add("detectionModel", detectionModel); |
| | 0 | 1561 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 1562 | | ServiceClientTracing.Enter(_invocationId, this, "DetectWithStream", tracingParameters); |
| | | 1563 | | } |
| | | 1564 | | // Construct URL |
| | 90 | 1565 | | var _baseUrl = Client.BaseUri; |
| | 90 | 1566 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "detect"; |
| | 90 | 1567 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 90 | 1568 | | List<string> _queryParameters = new List<string>(); |
| | 90 | 1569 | | if (returnFaceId != null) |
| | | 1570 | | { |
| | 90 | 1571 | | _queryParameters.Add(string.Format("returnFaceId={0}", System.Uri.EscapeDataString(Rest.Serialization.Sa |
| | | 1572 | | } |
| | 90 | 1573 | | if (returnFaceLandmarks != null) |
| | | 1574 | | { |
| | 90 | 1575 | | _queryParameters.Add(string.Format("returnFaceLandmarks={0}", System.Uri.EscapeDataString(Rest.Serializa |
| | | 1576 | | } |
| | 90 | 1577 | | if (returnFaceAttributes != null) |
| | | 1578 | | { |
| | 2 | 1579 | | _queryParameters.Add(string.Format("returnFaceAttributes={0}", System.Uri.EscapeDataString(string.Join(" |
| | | 1580 | | } |
| | 90 | 1581 | | if (recognitionModel != null) |
| | | 1582 | | { |
| | 90 | 1583 | | _queryParameters.Add(string.Format("recognitionModel={0}", System.Uri.EscapeDataString(recognitionModel) |
| | | 1584 | | } |
| | 90 | 1585 | | if (returnRecognitionModel != null) |
| | | 1586 | | { |
| | 90 | 1587 | | _queryParameters.Add(string.Format("returnRecognitionModel={0}", System.Uri.EscapeDataString(Rest.Serial |
| | | 1588 | | } |
| | 90 | 1589 | | if (detectionModel != null) |
| | | 1590 | | { |
| | 90 | 1591 | | _queryParameters.Add(string.Format("detectionModel={0}", System.Uri.EscapeDataString(detectionModel))); |
| | | 1592 | | } |
| | 90 | 1593 | | if (_queryParameters.Count > 0) |
| | | 1594 | | { |
| | 90 | 1595 | | _url += "?" + string.Join("&", _queryParameters); |
| | | 1596 | | } |
| | | 1597 | | // Create HTTP transport objects |
| | 90 | 1598 | | var _httpRequest = new HttpRequestMessage(); |
| | 90 | 1599 | | HttpResponseMessage _httpResponse = null; |
| | 90 | 1600 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 90 | 1601 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 1602 | | // Set Headers |
| | | 1603 | | |
| | | 1604 | | |
| | 90 | 1605 | | if (customHeaders != null) |
| | | 1606 | | { |
| | 0 | 1607 | | foreach(var _header in customHeaders) |
| | | 1608 | | { |
| | 0 | 1609 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 1610 | | { |
| | 0 | 1611 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 1612 | | } |
| | 0 | 1613 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 1614 | | } |
| | | 1615 | | } |
| | | 1616 | | |
| | | 1617 | | // Serialize Request |
| | 90 | 1618 | | string _requestContent = null; |
| | 90 | 1619 | | if(image == null) |
| | | 1620 | | { |
| | 0 | 1621 | | throw new System.ArgumentNullException("image"); |
| | | 1622 | | } |
| | 90 | 1623 | | if (image != null && image != Stream.Null) |
| | | 1624 | | { |
| | 90 | 1625 | | _httpRequest.Content = new StreamContent(image); |
| | 90 | 1626 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 1627 | | } |
| | | 1628 | | // Set Credentials |
| | 90 | 1629 | | if (Client.Credentials != null) |
| | | 1630 | | { |
| | 90 | 1631 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 90 | 1632 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 1633 | | } |
| | | 1634 | | // Send Request |
| | 90 | 1635 | | if (_shouldTrace) |
| | | 1636 | | { |
| | 0 | 1637 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 1638 | | } |
| | 90 | 1639 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 90 | 1640 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 90 | 1641 | | if (_shouldTrace) |
| | | 1642 | | { |
| | 0 | 1643 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 1644 | | } |
| | 90 | 1645 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 90 | 1646 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 90 | 1647 | | string _responseContent = null; |
| | 90 | 1648 | | if ((int)_statusCode != 200) |
| | | 1649 | | { |
| | 0 | 1650 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 1651 | | try |
| | | 1652 | | { |
| | 0 | 1653 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 1654 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| | 0 | 1655 | | if (_errorBody != null) |
| | | 1656 | | { |
| | 0 | 1657 | | ex.Body = _errorBody; |
| | | 1658 | | } |
| | 0 | 1659 | | } |
| | 0 | 1660 | | catch (JsonException) |
| | | 1661 | | { |
| | | 1662 | | // Ignore the exception |
| | 0 | 1663 | | } |
| | 0 | 1664 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 1665 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 1666 | | if (_shouldTrace) |
| | | 1667 | | { |
| | 0 | 1668 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 1669 | | } |
| | 0 | 1670 | | _httpRequest.Dispose(); |
| | 0 | 1671 | | if (_httpResponse != null) |
| | | 1672 | | { |
| | 0 | 1673 | | _httpResponse.Dispose(); |
| | | 1674 | | } |
| | 0 | 1675 | | throw ex; |
| | | 1676 | | } |
| | | 1677 | | // Create Result |
| | 90 | 1678 | | var _result = new HttpOperationResponse<IList<DetectedFace>>(); |
| | 90 | 1679 | | _result.Request = _httpRequest; |
| | 90 | 1680 | | _result.Response = _httpResponse; |
| | | 1681 | | // Deserialize Response |
| | 90 | 1682 | | if ((int)_statusCode == 200) |
| | | 1683 | | { |
| | 90 | 1684 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 1685 | | try |
| | | 1686 | | { |
| | 90 | 1687 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<DetectedFace>>(_responseCo |
| | 90 | 1688 | | } |
| | 0 | 1689 | | catch (JsonException ex) |
| | | 1690 | | { |
| | 0 | 1691 | | _httpRequest.Dispose(); |
| | 0 | 1692 | | if (_httpResponse != null) |
| | | 1693 | | { |
| | 0 | 1694 | | _httpResponse.Dispose(); |
| | | 1695 | | } |
| | 0 | 1696 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 1697 | | } |
| | | 1698 | | } |
| | 90 | 1699 | | if (_shouldTrace) |
| | | 1700 | | { |
| | 0 | 1701 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 1702 | | } |
| | 90 | 1703 | | return _result; |
| | 90 | 1704 | | } |
| | | 1705 | | |
| | | 1706 | | } |
| | | 1707 | | } |