1

When using the "CreateImagesFromUrls" endpoint of the Microsoft Cognitive Services Custom Vision Training API I get an error returned for each image saying "ErrorSource".

I am able to create tags using the API so I don't think it's an authentication issue, although it could be that the API can't load the image URL's which are images stored within SharePoint.

I have tried using a few different JSON formats for the HTTP body in the REST call.

This is an SPFX web part built using React and TypeScript. The goal is to load images from a SharePoint library and post them to the Training API. Here's my upload function:

private async uploadImages(tag: string, imageUrls: Array<any>): Promise<String> {
        const imageBatch: any = {images:[], tagIds: [tag]};
        imageUrls.map(iUrl => {
            imageBatch.images.push({url: iUrl, tagIds: [tag], regions: [
                {
                    tagId: tag,
                    left: 0.0,
                    top: 0.0,
                    width: 0.0,
                    height: 0.0
                  }
            ]});
        });
        console.log('ImageBatch:');
        console.log(imageBatch);
        //Add images
        const keyPostUrl: string = this.endPoint + 'projects/' + this.pRID + '/images/urls';
        const requestHeaders: Headers = new Headers();
        requestHeaders.append('Content-type', 'application/json');
        requestHeaders.append('Cache-Control', 'no-cache');
        requestHeaders.append('Training-key', this.trainingKey);

        const httpClientOptions: IHttpClientOptions = {
            body: JSON.stringify(imageBatch),
            headers: requestHeaders
        };

        return this.context.post(
        keyPostUrl,
        HttpClient.configurations.v1,
        httpClientOptions)
        .then((response: Response): Promise<HttpClientResponse> => {
            console.log("Sending Images.");
            return response.json();
        }).then(data => {
            console.log(data);
            console.log("Is batch successful: " + data.isBatchSuccessful); 
        });  
    }

Expected to see isBatchSuccessful = true and the images uploaded. However I get isBatchSuccessful = false, the error of "ErrorSource".

Example of response returned from API:

{
   "isBatchSuccessful":false,
   "images":[
      {
         "sourceUrl":"https://myserver.sharepoint.com/sites/dev/Vision%20Training%20List/1234/image_example_752%20-%20Copy%20(2).jpg",
         "status":"ErrorSource",
         "image":null
      },
      {
         "sourceUrl":"https://myserver.sharepoint.com/sites/dev/Vision%20Training%20List/1234/basic-image-template%20-%20Copy%20(3).png",
         "status":"ErrorSource",
         "image":null
      },
   ]
}
David G
  • 56
  • 5
  • My question may look like stupid, but does your Sharepoint images are "public" (on the web without the need of authenticating)? – Nicolas R May 22 '19 at 21:25
  • Hi Nicolas, not a stupid question. The images aren't public, but the user running the app does have permission to view them. I definitely think this could be an issue, but I need someone to confirm. I have also tried using the official API test site using a public image I googled and I couldn't get it to work, so I think I need some advice on what a good request body looks like. – David G May 23 '19 at 09:29
  • 1
    Honestly, I think that the problem with the images you mentioned is first the fact that they are not public. As you can see, you call a service by just giving links. it's its backend that will use these links, so it does not have the authentication that you have on the images and will not be able to access those items. Your other test with the API test site may be another reason (did you got the same error?) – Nicolas R May 29 '19 at 08:20
  • Hi Nicolas. I think you are correct, I have now managed to get it to work with some public images on the API test site, so this must be an authentication issue. I may try sending the images as base64 using the CreateImagesFromFiles end point instead. Thanks for your help. – David G May 31 '19 at 09:54

0 Answers0