Skip to main content

Notifications

Dynamics 365 Community / Blogs / Jesús Almaraz blog / Artificial Intelligence Com...

Artificial Intelligence Computer Vision Rest API Access. How to upload an image.

 Introduction.

These services include the next features.
  • OCR and text recognition (in an image).
  • Image description.
  • Image analysis.
  • Tag an image.
The focus of this post is show how to work with image services from al BC365, or any system that only able API rest, systems that don´t able us to use third party libraries. It´s harder to access vision services because you must add to the body request a streamed image, instead a text.

Example: tag image service in the example.

First I select a picture in the page.
3286.APIVisionApi.png
When I click process image, with tag method I get this response about the picture:
{
    "tags": [

        {

            "name": "sky",

            "confidence": 0.99999034404754639

        },

        {

            "name": "person",

            "confidence": 0.99938201904296875

        },

        {

            "name": "outdoor",

            "confidence": 0.99894469976425171

        },

        {

            "name": "man",

            "confidence": 0.99297571182250977

        },

        {

            "name": "water",

            "confidence": 0.979530930519104

        },

        {

            "name": "human face",

            "confidence": 0.96689260005950928

        },

        {

            "name": "beach",

            "confidence": 0.95207488536834717

        },

        {

            "name": "clothing",

            "confidence": 0.82670098543167114

        },

        {

            "name": "shore",

            "confidence": 0.15449938178062439

        }

    ]

}
JSON response says picture contains a person face, outdoor and very likely in the beach.

Technical implementation.

Here in this post we can see Microsoft Artificial Intelligence connection basics explained:
But now the hard stuff is to upload the image in the body request. For this purpose, we do next code:
procedure GetAzureImageResp(Method: Text; InsStream: InStream) TextoRespuesta: Text;
    var
        ClienteHttp: HttpClient;
        RequestEntity: HttpRequestMessage;
        Cabecera: HttpHeaders;
        Cabecera2: HttpHeaders;
        Respuesta: HttpResponseMessage;
        ReqBody: HttpContent;
        url: Text;
    begin
        url := AzureURL + Method;
        RequestEntity.SetRequestUri(Url);
        RequestEntity.Method('POST');
        RequestEntity.GetHeaders(Cabecera);
        Cabecera.Add('Ocp-Apim-Subscription-Key', GetsubscriptionKey);
        ReqBody.WriteFrom(InsStream);
        ReqBody.GetHeaders(Cabecera2);
        Cabecera2.Remove('Content-Type');
        Cabecera2.Add('Content-Type', 'application/octet-stream');
        RequestEntity.Content(ReqBody);
        ClienteHttp.send(RequestEntity, Respuesta);
        Respuesta.Content.ReadAs(TextoRespuesta);
    end;
 
Comments.
  • Url is a text constant https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/
  • Input parameter instream is the image into an instream variable.
  • Method parameter is passed form the Page. In this example equals to tag.
Not a big piece of code but has been hard to find exactly all call parameters.

Comments

*This post is locked for comments