Ejemplo n.º 1
0
        public async Task DetectStreaming()
        {
            if (NoAccountSettings())
            {
                return;
            }
            var cancellation = new CancellationTokenSource();

            var options = new PipelineOptions();

            options.Transport = new SocketClientTransport();

            var service = new FaceClient(s_account, s_key, options);

            Response <Stream> response = await service.DetectLazyAsync(cancellation.Token, new Uri(@"https://upload.wikimedia.org/wikipedia/commons/5/50/Albert_Einstein_%28Nobel%29.png"));

            Assert.IsTrue(response.TryGetHeader(Encoding.ASCII.GetBytes("Content-Length"), out long contentLength));
            Assert.IsTrue(response.TryGetHeader("Content-Type", out string type));
            Assert.AreEqual("application/json; charset=utf-8", type);

            var content = ReadOnlySequence <byte> .Empty;

            while (content.Length != contentLength)
            {
                var buffer = new byte[contentLength];
                var read   = await response.Result.ReadAsync(buffer, cancellation.Token);

                content = new ReadOnlySequence <byte>(buffer, 0, read);
            }

            if (content.IsSingleSegment)
            {
                var            array      = content.First.Span.ToArray();
                var            stream     = new StreamReader(new MemoryStream(array));
                JsonSerializer serializer = new JsonSerializer();
                var            json       = (DetectResult[])serializer.Deserialize(stream, typeof(DetectResult[]));
                DetectResult   result     = json[0];
                Assert.AreEqual("male", result.FaceAttributes.Gender);
                Assert.Greater(100, result.FaceAttributes.Age);
                Assert.Less(10, result.FaceAttributes.Age);
            }

            response.Dispose();
        }