Hi !

This is a special post. It’s the 1st one I write completely in my MacBook, so I’m sure that Javier (@jsuarezruiz), Yeray (@JosueYeray), Braulio (@braulio_sl), Luis, Sara, Roberto and other mac users will be proud of me 😀

So, I build and run my Custom Vision Marvel project in Docker for Mac. Smooth build and also a fast one!

docker build -t elbruno/cvmarvel:3.0 .

01 doker build on mac

Then get the image id and run the image

03 docker list images and run image

Final step is to play around with curl in bash to post the image (the file name with @ prefix took me some bing searches). Iron Fist detected !

curl -X POST http://127.0.0.1:8080/image -F imageData=@img1.jpg 

05 docker bash ls image analyzed and source image.png

Ok, the environment is working, so it’s time to create a .NetCore Console App to test this using amazing C# code. I have all my code in Azure Dev Ops, so I sync my repo and  added a new project in my current solution

06 new netcore project in visual studio for mac

Some C# lines in my console app and I was able to analyze a local picture using the Custom Vision Model in a container

07 console app in vs for mac detected image

The source code is very simple


using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CustomVisionMarvelConsoleDocker01
{
static class Program
{
static void Main()
{
MakePredictionRequest("IMG01.jpg").Wait();
Console.ReadLine();
}
static async Task MakePredictionRequest(string imageFilePath)
{
var client = new HttpClient();
var url = "http://127.0.0.1:8080/image";
var byteData = GetImageAsByteArray(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync(url, content);
var jsonResponse = await response.Content.ReadAsStringAsync();
var prettyJson = JToken.Parse(jsonResponse).ToString(Formatting.Indented);
Console.WriteLine(prettyJson);
}
}
static byte[] GetImageAsByteArray(string imageFilePath)
{
var fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
var binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}

Happy coding!

Greetings @ Toronto

El Bruno

References

My Posts

Windows 10 and YOLOV2 for Object Detection Series

19 responses to “#CustomVision – Analyzing images in a Console App using a #CustomVision project in a #Docker Container”

  1. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container (7) […]

    Like

  2. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container (7) […]

    Like

  3. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  4. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  5. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  6. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  7. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  8. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  9. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  10. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  11. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  12. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  13. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  14. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  15. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

  16. […] Analyzing images in a Console App using a Custom Vision project in a Docker Container […]

    Like

Leave a reply to #Docker – Tiempos de respuesta promedio utilizando #CustomVision.ai en un contenedor con Docker en #RaspberryPi u en PC – El Bruno Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading