src/lib/detection_algorithms/custom_vision/exporter.js
//  testSplit - the percent of tragged frames to reserve for test set defaults to 20%
the percent of tragged frames to reserve for test set defaults to 20%
the percent of tragged frames to reserve for test set defaults to 20%
function Exporter(exportDirPath, classes, posFramesCount, frameWidth, frameHeight, testSplit) {
    var self = this;
    self.projectId = "";
    self.trainingKey = exportDirPath;
    self.classes = classes;
    self.tagMap = {};
    self.posFramesCount = posFramesCount;
    self.frameWidth = frameWidth;
    self.frameHeight = frameHeight;
    self.testSplit = testSplit || 0.2;
    this.init = function init() {
        return new Promise((resolve, reject) => {
            var params = {
                "domainId": "DA2E3A8A-40A5-4171-82F4-58522F70FBC1"
            }
            fetch("https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects?name=vott_export&"+ $.param(params), {
                method: 'post', headers: {
                    "Training-Key":self.trainingKey
                }
            }).then(response => response.json()
            ).then((data)=>{
                self.projectId = data.id;
                var promises = self.classes.map(tagName =>
                        fetch(`https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/${self.projectId}/tags?name=${tagName}`, {
                            method: 'post', headers: {
                                "Training-Key":self.trainingKey
                            }
                        }).then(res => res.json()
                        ).then((tagData)=>{
                            self.tagMap[tagData.name] = tagData.id;
                            console.log(`${tagData.id}:${tagData.name}`);
                        })
                );
                Promise.all(promises).then(()=>{
                    resolve();
                }).catch((err)=>{
                    reject(err);
                })
            });
        });
    }
    this.exportFrame = function exportFrame(frameFileName, frameBuffer, tags) {
        return new Promise((resolve, reject) => {       
            $(function() {
                request = {
                    url: `https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/${self.projectId}/images/files?`,
                    beforeSend: function(xhrObj){
                        xhrObj.setRequestHeader("Training-Key",self.trainingKey);
                        xhrObj.setRequestHeader("Content-Type","application/json");
                    },
                    type: "POST",
                    data: JSON.stringify( {
                            "images": [
                            {
                                "name": frameFileName,
                                "contents": frameBuffer.toJSON().data,
                                "width": self.frameWidth,
                                "height": self.frameHeight,
                                "regions": tags.map((tag)=>{
                                    return {
                                        "tagId": self.tagMap[tag.class],
                                        "left": tag.x1/self.frameWidth,
                                        "top": tag.y1/self.frameHeight,
                                        "width": Math.abs(tag.x2 - tag.x1)/self.frameWidth,
                                        "height": Math.abs(tag.y2 - tag.y1)/self.frameHeight
                                    }
                                })
                            }]
                        }),
                }
                console.log(request);
                $.ajax(request)
                .done(function(data) {
                    resolve();
                })
                .fail(function(error) {
                    reject(error);
                });
            });
        });
    }
}
function Exporter ( exportDirPath , classes , posFramesCount , frameWidth , frameHeight , testSplit ) { var self = this ; self . projectId = "" ; self . trainingKey = exportDirPath ; self . classes = classes ; self . tagMap = { } ; self . posFramesCount = posFramesCount ; self . frameWidth = frameWidth ; self . frameHeight = frameHeight ; self . testSplit = testSplit || 0.2 ; this . init = function init ( ) { return new Promise ( ( resolve , reject ) => { var params = { "domainId" : "DA2E3A8A-40A5-4171-82F4-58522F70FBC1" } fetch ( "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects?name=vott_export&" + $ . param ( params ) , { method : 'post' , headers : { "Training-Key" : self . trainingKey } } ) . then ( response => response . json ( ) ) . then ( ( data ) => { self . projectId = data . id ; var promises = self . classes . map ( tagName => fetch ( ` ${ self . projectId } ${ tagName } ` , { method : 'post' , headers : { "Training-Key" : self . trainingKey } } ) . then ( res => res . json ( ) ) . then ( ( tagData ) => { self . tagMap [ tagData . name ] = tagData . id ; console . log ( ` ${ tagData . id } ${ tagData . name } ` ) ; } ) ) ; Promise . all ( promises ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) } ) ; } ) ; } this . exportFrame = function exportFrame ( frameFileName , frameBuffer , tags ) { return new Promise ( ( resolve , reject ) => { $ ( function ( ) { request = { url : ` ${ self . projectId } ` , beforeSend : function ( xhrObj ) { xhrObj . setRequestHeader ( "Training-Key" , self . trainingKey ) ; xhrObj . setRequestHeader ( "Content-Type" , "application/json" ) ; } , type : "POST" , data : JSON . stringify ( { "images" : [ { "name" : frameFileName , "contents" : frameBuffer . toJSON ( ) . data , "width" : self . frameWidth , "height" : self . frameHeight , "regions" : tags . map ( ( tag ) => { return { "tagId" : self . tagMap [ tag . class ] , "left" : tag . x1 / self . frameWidth , "top" : tag . y1 / self . frameHeight , "width" : Math . abs ( tag . x2 - tag . x1 ) / self . frameWidth , "height" : Math . abs ( tag . y2 - tag . y1 ) / self . frameHeight } } ) } ] } ) , } console . log ( request ) ; $ . ajax ( request ) . done ( function ( data ) { resolve ( ) ; } ) . fail ( function ( error ) { reject ( error ) ; } ) ; } ) ; } ) ; } }
testSplit - the percent of tragged frames to reserve for test set defaults to 20%
testSplit - the percent of tragged frames to reserve for test set defaults to 20%
// Prepare everything for exporting (e.g. create metadata files, // directories, ..)     // Returns: A Promise object that resolves when the operation completes // create new project (TBI add exisiting project option) // Request parameters // Export a single frame to the training data // Parameters: //  frameFileName - The file name to use when saving the image file  //  frameBuffer - A buffer with the frame image data  //  bboxes  - a list of bboxes in the format of x1, y1, x2, y2 where the //           coordinates are in absolute values of the image //  tags - a list of objects containing the tagging data. Each object is in the format of: //         {'x1' : int, 'y1' : int, 'x2' : int, 'y2' : int, 'class' : string 'w': int, 'h' :int} //         Where (x1,y1) and (x2,y2) are the coordinates of the top left and bottom right corner and w an h are optional overloads for the frame demensions //         of the bounding boxes (respectively), and 'class' is the name of the class. // Returns: A Promise object that resolves when the operation completes // Request headers // Request body /* "left": tag.x1/tag.w,
                                        "top": tag.y1/tag.h,
                                        "width": (tag.x2 - tag.x1)/tag.w,
                                        "height": (tag.y2 - tag.y1)/tag.h */