createCollection()

in source/video-analysis/lib/rekognition/base.js [429:482]


  createCollection(name, responses) {
    const collection = [];
    let cursor;

    while (responses.length) {
      const response = responses.shift();
      while (response.length) {
        const item = response.shift();

        if (item[this.propName][this.propKey].toString() !== name) {
          continue;
        }

        const current = this.createTrackItem(item);

        if (!cursor) {
          cursor = current;
          continue;
        }

        /* case 0: if no BoundingBox, combine the track. */
        if (!current.hasBoundingBox()) {
          if ((current.begin - cursor.end) < TrackItem.Constants.TimeDrift) {
            cursor.combineItem(current);
            continue;
          }
        }

        /* case 1: if timestamp is far apart (300ms), it is a break */
        if (cursor.timeDrift(current.begin)) {
          collection.push(cursor);
          cursor = current;
          continue;
        }

        /* case 2: check to see if position has drifted */
        if (cursor.positionDrift(current)) {
          collection.push(cursor);
          cursor = current;
          continue;
        }

        /* case 3: update end time */
        cursor.combineItem(current);
      }
    }

    /* case 4: process the last cursor */
    if (cursor && cursor.begin !== (collection[collection.length - 1] || {}).begin) {
      collection.push(cursor);
    }

    return collection;
  }