コード例 #1
0
        public static async Task Run(
            [BlobTrigger("images/{name}")] Stream trigger,
            [Queue("Handwriting")] ICollector <string> queueItem,
            string name,
            TraceWriter log)
        {
            log.Info("Start");

            using (HttpContent content = new StreamContent(trigger))
            {
                var parameters = "recognizeText?handwriting=true";

                var response = await CognitiveServicesHttpClient.PostVisionRequest(content, parameters);

                if (response.IsSuccessStatusCode)
                {
                    var operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();

                    var request = new HandwritingRequest
                    {
                        Key = name,
                        OperationLocation = operationLocation
                    };

                    var json = JSONHelper.ToJson(request);

                    queueItem.Add(json);
                }
            }

            log.Info("Finish");
        }
コード例 #2
0
        public static async Task Run(
            [BlobTrigger("images/{name}")] Stream trigger,
            [Table("images")] CloudTable cloudTable,
            string name,
            TraceWriter log)
        {
            log.Info("Start");

            using (HttpContent content = new StreamContent(trigger))
            {
                var parameters = "detect" +
                                 "?returnFaceId=true" +
                                 "&returnFaceLandmarks=true" +
                                 "&returnFaceAttributes=age,gender,smile,facialHair,headPose,glasses,emotion,hair,makeup,accessories,blur,exposure,noise";

                var response = await CognitiveServicesHttpClient.PostFaceRequest(content, parameters);

                if (response.IsSuccessStatusCode)
                {
                    var responseBytes = await response.Content.ReadAsStringAsync();

                    await cloudTable.Update(name, responseBytes, (image, text) =>
                    {
                        image.Faces = text;
                    });
                }
            }

            log.Info("Finish");
        }
コード例 #3
0
        public static async Task Run(
            [BlobTrigger("images/{name}")] Stream trigger,
            [Table("images")] CloudTable cloudTable,
            string name,
            TraceWriter log)
        {
            log.Info("Start");

            using (HttpContent content = new StreamContent(trigger))
            {
                var parameters = "describe" +
                                 "?maxCandidates=1";

                var response = await CognitiveServicesHttpClient.PostVisionRequest(content, parameters);

                if (response.IsSuccessStatusCode)
                {
                    var responseBytes = await response.Content.ReadAsStringAsync();

                    await cloudTable.Update(name, responseBytes, (image, text) =>
                    {
                        image.Description = text;
                    });
                }
            }

            log.Info("Finish");
        }
コード例 #4
0
        public static async Task Run(
            [BlobTrigger("images/{name}")] Stream trigger,
            [Blob("thumbnails/{name}", FileAccess.ReadWrite)] ICloudBlob thumbnail,
            [Table("images")] CloudTable cloudTable,
            string name,
            TraceWriter log
            )
        {
            log.Info("Start");

            using (HttpContent content = new StreamContent(trigger))
            {
                var parameters = "generateThumbnail?width=200&height=150&smartCropping=true";

                var response = await CognitiveServicesHttpClient.PostVisionRequest(content, parameters);

                if (response.IsSuccessStatusCode)
                {
                    var responseBytes = await response.Content.ReadAsStreamAsync();

                    await thumbnail.UploadFromStreamAsync(responseBytes);

                    await cloudTable.Update(name, thumbnail.Uri.AbsoluteUri, (image, text) =>
                    {
                        image.ThumbUri = text;
                    });
                }
            }

            log.Info("Finish");
        }
コード例 #5
0
        public static async Task Run(
            [BlobTrigger("images/{name}")] Stream trigger,
            [Table("images")] CloudTable cloudTable,
            [Blob("faces/{name}", FileAccess.ReadWrite)] ICloudBlob faceOutput,
            string name,
            TraceWriter log)
        {
            var analyseStream = new MemoryStream();

            trigger.CopyTo(analyseStream);

            using (var ms = new MemoryStream())
            {
                trigger.Position = 0;

                trigger.CopyTo(ms);

                ms.Position = 0;

                using (HttpContent content = new StreamContent(ms))
                {
                    var parameters = "analyze" +
                                     "?visualFeatures=Categories,Tags,Description,Faces,ImageType,Color,Adult" +
                                     "&details=Celebrities,Landmarks";

                    var response = await CognitiveServicesHttpClient.PostVisionRequest(content, parameters);

                    if (response.IsSuccessStatusCode)
                    {
                        var responseBytes = await response.Content.ReadAsStringAsync();

                        await cloudTable.Update(name, responseBytes, (image, text) => { image.Analyse = text; });

                        var describe = JSONHelper.FromJson <Analyse>(responseBytes);

                        if (describe.Faces.Any())
                        {
                            try
                            {
                                using (var bitmap = new Bitmap(analyseStream))
                                {
                                    using (var graph = Graphics.FromImage(bitmap))
                                    {
                                        var outline = new Pen(Brushes.Red, 2);
                                        var text    = new Pen(Color.White, 2);

                                        foreach (var face in describe.Faces)
                                        {
                                            graph.DrawRectangle(outline, face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height);

                                            var font = new Font(FontFamily.GenericSerif, 16, FontStyle.Bold);
                                            graph.DrawString($"{face.Gender} {face.Age}", font, text.Brush, face.FaceRectangle.Left, face.FaceRectangle.Top);
                                        }
                                    }

                                    var outputStream = new MemoryStream();

                                    bitmap.Save(outputStream, ImageFormat.Jpeg);

                                    outputStream.Position = 0;

                                    await faceOutput.UploadFromStreamAsync(outputStream);

                                    await cloudTable.Update(name, faceOutput.Uri.AbsoluteUri, (image, text) => { image.FaceUri = text; });
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }

            log.Info("Finish");
        }