Ejemplo n.º 1
0
        public async Task <Response <Stream> > DetectLazyAsync(CancellationToken cancellation, Uri image, FaceDetectOptions options = default)
        {
            if (options == null)
            {
                options = new FaceDetectOptions();
            }
            Uri uri = BuildUri(options);

            HttpMessage message = null;

            try
            {
                message = _client.CreateMessage(_options, cancellation);
                message.SetRequestLine(PipelineMethod.Post, uri);

                message.AddHeader(_keyHeader);
                message.AddHeader(HttpHeader.Common.JsonContentType);
                message.SetContent(new FaceContent(image, message));

                await _client.ProcessAsync(message).ConfigureAwait(false);

                return(new Response <Stream>(message.Response, message.Response.ContentStream));
            }
            catch
            {
                if (message != null)
                {
                    message.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <Response <Stream> > DetectLazyAsync(CancellationToken cancellation, Uri image, FaceDetectOptions options = default)
        {
            if (options == null)
            {
                options = new FaceDetectOptions();
            }
            Uri uri = BuildUri(options);

            PipelineCallContext context = null;

            try
            {
                context = _client.CreateContext(_options, cancellation);
                context.SetRequestLine(ServiceMethod.Post, uri);

                context.AddHeader(_keyHeader);
                context.AddHeader(Header.Common.JsonContentType);
                context.SetContent(new FaceContent(image, context));

                await _client.ProcessAsync(context).ConfigureAwait(false);

                return(new Response <Stream>(context.Response, context.Response.ContentStream));
            }
            catch
            {
                if (context != null)
                {
                    context.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 3
0
        Uri BuildUri(FaceDetectOptions options)
        {
            var ub = new UriBuilder(_baseUrl);

            ub.Path = ub.Path + Options.ApiVersion + s_detectMethod;
            options.BuildRequestParameters(ub);
            return(ub.Uri);
        }
Ejemplo n.º 4
0
        public async Task <Response <FaceDetectResult> > DetectAsync(CancellationToken cancellation, string imagePath, FaceDetectOptions options)
        {
            if (options == null)
            {
                options = new FaceDetectOptions();
            }
            Uri uri = BuildUri(options);

            HttpMessage message = null;

            try
            {
                message = _client.CreateMessage(_options, cancellation);
                message.SetRequestLine(PipelineMethod.Post, uri);

                message.AddHeader(_keyHeader);
                message.AddHeader(HttpHeader.Common.OctetStreamContentType);

                SetContentStream(message, imagePath);

                await _client.ProcessAsync(message).ConfigureAwait(false);

                Response response = message.Response;
                if (!response.TryGetHeader(s_contentLength, out long contentLength))
                {
                    throw new Exception("bad response: no content length header");
                }

                var buffer = new byte[contentLength];
                var read   = await response.ContentStream.ReadAsync(buffer, cancellation);

                Func <Response, FaceDetectResult> contentParser = null;
                if (response.Status == 200)
                {
                    contentParser = (rsp) => { return(FaceDetectResult.Parse(new ReadOnlySequence <byte>(buffer, 0, read))); };
                }
                return(new Response <FaceDetectResult>(response, contentParser));
            }
            catch
            {
                if (message != null)
                {
                    message.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 5
0
        public async Task <Response <FaceDetectResult> > DetectAsync(CancellationToken cancellation, Uri image, FaceDetectOptions options)
        {
            if (options == null)
            {
                options = new FaceDetectOptions();
            }
            Uri uri = BuildUri(options);

            PipelineCallContext context = null;

            try
            {
                context = _client.CreateContext(_options, cancellation);

                context.SetRequestLine(ServiceMethod.Post, uri);
                context.AddHeader(_keyHeader);
                context.AddHeader(Header.Common.JsonContentType);
                context.SetContent(new FaceContent(image, context));

                await _client.ProcessAsync(context).ConfigureAwait(false);

                ServiceResponse response = context.Response;
                if (!response.TryGetHeader(s_contentLength, out long contentLength))
                {
                    throw new Exception("bad response: no content length header");
                }

                var buffer = new byte[contentLength];
                var read   = await response.ContentStream.ReadAsync(buffer, cancellation);

                Func <ServiceResponse, FaceDetectResult> contentParser = null;
                if (response.Status == 200)
                {
                    contentParser = (rsp) => { return(FaceDetectResult.Parse(new ReadOnlySequence <byte>(buffer, 0, read))); };
                }
                return(new Response <FaceDetectResult>(response, contentParser));
            }
            catch
            {
                if (context != null)
                {
                    context.Dispose();
                }
                throw;
            }
        }