Closed HarrySpyrou closed 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
@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.
@HarrySpyrou Great to hear and thank you for the kind words! Also you are quite right about disposing the stream, I overlooked that one.
This issue is for a: (mark with an
x
)Minimal steps to reproduce
Any log messages given by the failure
Expected/desired behavior
OS and Version?
Versions
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