Azure-Samples / cognitive-services-quickstart-code

Code Examples used by the Quickstarts in the Cognitive Services Documentation
MIT License
356 stars 519 forks source link

Getting Error when trying to Upload as a stream. Documentation missing? #161

Closed HarrySpyrou closed 4 years ago

HarrySpyrou commented 4 years ago

Please provide us with the following information:

This issue is for a: (mark with an x)

- [ ] bug report -> please search issues before submitting
- [ ] feature request
- [X] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce

Create a FaceClient, IFormFile coming in, then make the file a MemoryStream and try to upload it through .DetectWithStreamAsync()

Any log messages given by the failure

Image size is too small (Error: InvalidImageSize)

Expected/desired behavior

As a stream, to make it easier to upload. Or maybe give me some information.

OS and Version?

Windows 10 Pro

Versions

1903

Mention any other details that might be useful

I'm not sure if that's a bug (I think I'm doing it wrong). The main problem I have is that nowhere on the internet (not in the documentation either) could I find an example where someone uploads it as a stream. Ideally I'd prefer not saving it to Cloud Blob Storage and use the url but rather get it straight from the frontend, change it into a stream and use Detect. Is it possible for an example using a Stream without filePath to be uploaded?

Thank you


v-jaswel commented 4 years ago

Hi @HarrySpyrou,

I agree there should be more examples of using DetectWithStreamAsync as opposed to DetectWithUrlAsync. I will add those.

Here is an example of how to convert an IFormFile to a MemoryStream and then pass the MemoryStream to DetectWithStreamAsync. I am cheating by instantiating the internal FormFile class, but it should be possible to adapt this code to use your IFormFile instance. Please let me know if this does not work for you.

using System;
using System.IO;
using System.Threading.Tasks;

// Install NuGet package Microsoft.Azure.CognitiveServices.Vision.Face.
using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
// Install NuGet package Microsoft.AspNetCore.Http.
using Microsoft.AspNetCore.Http.Internal;

namespace ConsoleApp1
{
    class Program
    {
        static string SUBSCRIPTION_KEY = Environment.GetEnvironmentVariable("FACE_SUBSCRIPTION_KEY");
        static string ENDPOINT = Environment.GetEnvironmentVariable("FACE_ENDPOINT");

        static async Task Quickstart()
        {
            IFaceClient client = new FaceClient(new ApiKeyServiceClientCredentials(SUBSCRIPTION_KEY)) { Endpoint = ENDPOINT };

            // This is just so we can create an IFormFile.
            using FileStream fs = File.OpenRead(@"INSERT PATH TO FILE ON YOUR COMPUTER HERE");
            FormFile file = new FormFile(fs, 0, fs.Length, null, Path.GetFileName(fs.Name));

            MemoryStream stream = new MemoryStream();
            file.CopyTo(stream);
            stream.Position = 0;
            System.Collections.Generic.IList<DetectedFace> faces = await client.Face.DetectWithStreamAsync(stream, detectionModel: DetectionModel.Detection02);
            DetectedFace face = faces[0];

            Console.WriteLine($"Rectangle(Left/Top/Width/Height) : {face.FaceRectangle.Left} {face.FaceRectangle.Top} {face.FaceRectangle.Width} {face.FaceRectangle.Height}");
        }

        static void Main(string[] args)
        {
            Task.WaitAll (Quickstart());
        }
    }
}

Output:

Rectangle(Left/Top/Width/Height) : 153 -42 995 1360
HarrySpyrou commented 4 years ago

@v-jaswel Just got to try it out! I love it, it works right out of the box. Only thing I changed is basically this:

await using var stream = new MemoryStream();

Just to make sure the stream is disposed. Great work man! You saved me the hassle of having to save the file to disk.

v-jaswel commented 4 years ago

@HarrySpyrou Great to hear and thank you for the kind words! Also you are quite right about disposing the stream, I overlooked that one.