robbie-sdk
Version:
Robbie Visio SDK to send events for analaysis
1 lines • 15.6 kB
JavaScript
var RobbieVisio=function(e){function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var t={};return n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s="./index.js")}({"./RobbieApi.js":function(module,exports,__webpack_require__){"use strict";eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar CREATE_SESSION_ROUTE = exports.CREATE_SESSION_ROUTE = 'rtc/create/';\nvar REQUEST_EMOTIONS_ROUTE = exports.REQUEST_EMOTIONS_ROUTE = 'rtc/ingest/';\nvar CLOSE_SESSION_ROUTE = exports.CLOSE_SESSION_ROUTE = 'close/';\n\nvar CONTENT_SOURCE_SDK = exports.CONTENT_SOURCE_SDK = 'SDK'; // Content source 2 = API\n\nvar RobbieApi = function () {\n function RobbieApi(token, properties) {\n _classCallCheck(this, RobbieApi);\n\n if (token === undefined || token === '') {\n throw new Error('Cannot connect to Robbie API without a token');\n }\n this._token = token;\n this._properties = Object.assign({}, properties);\n }\n\n _createClass(RobbieApi, [{\n key: '_containsToken',\n\n\n /**\n * Checks wether the class has been provided with a token\n * @returns {boolean}\n */\n value: function _containsToken() {\n return this._token != null && this._token !== '';\n }\n }, {\n key: 'requestNewSession',\n value: function requestNewSession() {\n var _this = this;\n\n return new Promise(function (resolve, reject) {\n var xhr = new XMLHttpRequest();\n xhr.open('POST', RobbieApi.baseUrl + CREATE_SESSION_ROUTE);\n xhr.setRequestHeader('Content-Type', 'application/json');\n xhr.setRequestHeader('Robbie-Sdk-Web-X-Token', '' + _this._token);\n xhr.onreadystatechange = function () {\n if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 201) {\n var response = JSON.parse(xhr.responseText);\n resolve(response);\n } else if (xhr.readyState === XMLHttpRequest.DONE) {\n reject(xhr);\n }\n };\n xhr.onerror = function () {\n reject(xhr);\n };\n xhr.ontimeout = function () {\n reject(new Error('Robbie Api timed out'));\n };\n xhr.send(JSON.stringify({\n source: CONTENT_SOURCE_SDK\n }));\n });\n }\n\n /**\n * @param {string} frame Base64 string of an image\n * @param {string} id Session id to send images to\n * @param {object} properties Labels to send to the api\n */\n\n }, {\n key: 'requestEmotions',\n value: function requestEmotions(id, frame) {\n var _this2 = this;\n\n return new Promise(function (resolve, reject) {\n var xhr = new XMLHttpRequest();\n xhr.open('POST', '' + RobbieApi.baseUrl + REQUEST_EMOTIONS_ROUTE + '?single=true&persist=false&gender=false');\n xhr.setRequestHeader('Content-Type', 'application/json');\n xhr.setRequestHeader('Robbie-Sdk-Web-X-Token', '' + _this2._token);\n xhr.onerror = function () {\n reject(xhr);\n };\n xhr.ontimeout = function () {\n reject(new Error('Robbie Api timed out'));\n };\n var timestamp = new Date().getTime();\n xhr.onreadystatechange = function () {\n if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 201) {\n var response = JSON.parse(xhr.responseText);\n response.TIMESTAMP = timestamp;\n resolve(response);\n } else if (xhr.readyState === XMLHttpRequest.DONE) {\n reject(xhr);\n }\n };\n xhr.send(JSON.stringify({\n user_id: 'ANONYMOUS',\n timestamp: new Date(),\n image: frame,\n content: id,\n source: CONTENT_SOURCE_SDK,\n labels: _this2._properties\n }));\n });\n }\n }, {\n key: 'properties',\n set: function set(properties) {\n this._properties = properties;\n },\n get: function get() {\n return this._properties;\n }\n }], [{\n key: 'baseUrl',\n get: function get() {\n if (true) {\n return 'https://visio.robbieapis.com/sdk/web/v1/';\n } else if (process.env.NODE_ENV === 'development') {\n return 'http://localhost:8888/sdk/web/v1/';\n } else if (process.env.NODE_ENV === 'staging') {\n return 'https://visio-staging.robbieapis.com/sdk/web/v1/';\n }\n return 'http://localhost:8888/sdk/web/v1/';\n }\n }]);\n\n return RobbieApi;\n}();\n\nexports.default = RobbieApi;\n\n//////////////////\n// WEBPACK FOOTER\n// ./RobbieApi.js\n// module id = ./RobbieApi.js\n// module chunks = 0\n\n//# sourceURL=webpack:///./RobbieApi.js?")},"./RobbieVisioSdk.js":function(module,exports,__webpack_require__){"use strict";eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _Session = __webpack_require__(\"./Session.js\");\n\nvar _Session2 = _interopRequireDefault(_Session);\n\nvar _RobbieApi = __webpack_require__(\"./RobbieApi.js\");\n\nvar _RobbieApi2 = _interopRequireDefault(_RobbieApi);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar RobbieVisioSdk = function () {\n function RobbieVisioSdk() {\n _classCallCheck(this, RobbieVisioSdk);\n\n this.robbieApi = null;\n this.session = null;\n this._video = null;\n this._canvas = null;\n }\n\n /**\n * @param token Api token to authenticate client\n * @param onEmotionsReceived {function} Callback executed when emotions are received\n * @param properties {object} Group of labels to send with the frame\n */\n\n\n _createClass(RobbieVisioSdk, [{\n key: 'init',\n value: function init(token, properties, onEmotionsReceived) {\n var _this = this;\n\n this.robbieApi = new _RobbieApi2.default(token, properties);\n return this.validateCameraPermissions().then(function () {\n return _this.createSession();\n }).then(function (session) {\n return session.startRecording(onEmotionsReceived);\n });\n }\n }, {\n key: 'silentInit',\n value: function silentInit(token) {\n this.robbieApi = new _RobbieApi2.default(token);\n }\n\n /**\n * Finds UserMedia object through different types of browsers and returns it\n * @returns {*}\n * @private\n */\n\n }, {\n key: '_createImageCaptureElements',\n\n\n /**\n * Create canvas and video tag inside the document\n * @private\n */\n value: function _createImageCaptureElements() {\n this._video = document.createElement('video');\n this._canvas = document.createElement('canvas');\n this._canvas.width = 600;\n this._canvas.height = 400;\n }\n\n /**\n * Destroy video and canvas tag in document;\n * @private\n */\n\n }, {\n key: '_destroyImageCaptureElements',\n value: function _destroyImageCaptureElements() {\n if (this._video != null) {\n this._video.remove();\n }\n if (this._canvas != null) {\n this._canvas.remove();\n }\n this._video = null;\n this._canvas = null;\n }\n }, {\n key: 'validateCameraPermissions',\n value: function validateCameraPermissions() {\n return new Promise(function (resolve, reject) {\n if (navigator === null) {\n reject(new Error('No navigator Found'));\n }\n navigator.getUserMedia = RobbieVisioSdk._findNavigator();\n navigator.getUserMedia({\n video: {\n width: {},\n height: { min: 720, max: 1024 },\n facingMode: 'user',\n frameRate: 30\n }\n }, function (stream) {\n return resolve(stream);\n }, function () {\n return reject(new Error('User declined permission'));\n });\n });\n }\n\n /**\n * @returns {Promise.<Session>} Contains a Sessions object with proper methods to start/stop\n * event sending.\n */\n\n }, {\n key: 'createSession',\n value: function createSession() {\n var _this2 = this;\n\n if (this.session != null) {\n return new Promise(function (resolve, reject) {\n return reject(new Error('First close opened session'));\n });\n }\n if (this.robbieApi == null) {\n return new Promise(function (resolve, reject) {\n return reject(new Error('First init RobbieVisio!'));\n });\n }\n return this.validateCameraPermissions().then(function () {\n return _this2.robbieApi.requestNewSession();\n }).then(function (response) {\n _this2._createImageCaptureElements();\n _this2.session = new _Session2.default(response.id, _this2);\n return _this2.session;\n });\n }\n\n /**\n * If Session is being recorded, close it\n */\n\n }, {\n key: 'close',\n value: function close() {\n if (this.session == null) {\n console.warn('To close the session, start it first!');\n return;\n }\n this.session.close();\n this._destroyImageCaptureElements();\n this.session = null;\n }\n }], [{\n key: '_findNavigator',\n value: function _findNavigator() {\n return navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;\n }\n }]);\n\n return RobbieVisioSdk;\n}();\n\nexports.default = RobbieVisioSdk;\n\n//////////////////\n// WEBPACK FOOTER\n// ./RobbieVisioSdk.js\n// module id = ./RobbieVisioSdk.js\n// module chunks = 0\n\n//# sourceURL=webpack:///./RobbieVisioSdk.js?")},"./Session.js":function(module,exports,__webpack_require__){"use strict";eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar FRAME_RATE = exports.FRAME_RATE = 2000;\n\nvar Session = function () {\n function Session(id, robbieSdk) {\n _classCallCheck(this, Session);\n\n this._id = id;\n this._isOpened = true;\n this._intervalCallback = undefined;\n this._robbieSdk = robbieSdk;\n this._video = robbieSdk._video;\n this._canvas = robbieSdk._canvas;\n this._robbieApi = robbieSdk.robbieApi;\n }\n\n _createClass(Session, [{\n key: 'containsId',\n value: function containsId() {\n return this._id != null && this._id !== '';\n }\n }, {\n key: 'startRecording',\n value: function startRecording(onEmotionsReceived) {\n var _this = this;\n\n return new Promise(function (resolve, reject) {\n if (!_this.containsId() || !_this.isOpened) {\n reject(new Error('Session does not contains an id'));\n }\n _this._robbieSdk.validateCameraPermissions().then(function (stream) {\n _this._video.src = window.URL.createObjectURL(stream);\n }).then(function () {\n _this._intervalCallback = setInterval(function () {\n var frame = _this._getVideoFrameString();\n _this._robbieApi.requestEmotions(_this._id, frame).then(function (_response) {\n resolve(_response);\n if (onEmotionsReceived != null) {\n onEmotionsReceived(_response);\n }\n });\n }, FRAME_RATE);\n var frame = _this._getVideoFrameString();\n return _this._robbieApi.requestEmotions(_this._id, frame).then(function (response) {\n resolve(response);\n if (onEmotionsReceived != null) {\n onEmotionsReceived(response);\n }\n });\n });\n });\n }\n }, {\n key: 'close',\n value: function close() {\n this._isOpened = false;\n clearInterval(this._intervalCallback);\n }\n }, {\n key: '_getVideoFrameString',\n value: function _getVideoFrameString() {\n var width = this._canvas.width;\n var height = this._canvas.height;\n var ctx = this._canvas.getContext('2d');\n ctx.drawImage(this._video, 0, 0, width, height);\n var dataURL = this._canvas.toDataURL('image/jpeg', 0.90);\n return dataURL.replace(/^data:image\\/jpeg;base64,/, '');\n }\n }, {\n key: 'id',\n get: function get() {\n return this._id;\n }\n }, {\n key: 'isOpened',\n get: function get() {\n return this._isOpened;\n }\n }]);\n\n return Session;\n}();\n\nexports.default = Session;\n\n//////////////////\n// WEBPACK FOOTER\n// ./Session.js\n// module id = ./Session.js\n// module chunks = 0\n\n//# sourceURL=webpack:///./Session.js?")},"./index.js":function(module,exports,__webpack_require__){"use strict";eval('\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\n\nvar _RobbieVisioSdk = __webpack_require__("./RobbieVisioSdk.js");\n\nvar _RobbieVisioSdk2 = _interopRequireDefault(_RobbieVisioSdk);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nvar RobbieVisio = new _RobbieVisioSdk2.default();\nexports.default = RobbieVisio;\n\n//////////////////\n// WEBPACK FOOTER\n// ./index.js\n// module id = ./index.js\n// module chunks = 0\n\n//# sourceURL=webpack:///./index.js?')}});