diff --git a/serverPredictor/serverPredictor.py b/serverPredictor/serverPredictor.py index ae2062e..91af587 100644 --- a/serverPredictor/serverPredictor.py +++ b/serverPredictor/serverPredictor.py @@ -32,6 +32,7 @@ class Predict(Resource): def post(self): print("new predict") start = time.time() + print(request.files['file']) filer = request.files['file']#open the uploaded image, and transform to the numpy array #process the img @@ -50,6 +51,8 @@ class Predict(Resource): logging.info(" [result]: " + r) roundtrip = time.time() - start logging.info(" [roundtriptime]: " + str(roundtrip) + "s") + print("Response: ") + print({'result': r}) return({'result': r}) api.add_resource(Predict, '/predict') diff --git a/smartphoneApp/www/js/app.js b/smartphoneApp/www/js/app.js index ef65b94..06284bb 100644 --- a/smartphoneApp/www/js/app.js +++ b/smartphoneApp/www/js/app.js @@ -1,5 +1,7 @@ var urlapi = "http://127.0.0.1:3200/"; +var selectedImg=""; + angular.module('app', [ 'ionic', 'app.main', diff --git a/smartphoneApp/www/js/main.js b/smartphoneApp/www/js/main.js index abbabea..4547256 100644 --- a/smartphoneApp/www/js/main.js +++ b/smartphoneApp/www/js/main.js @@ -1,10 +1,12 @@ angular.module('app.main', []) -.controller('MainCtrl', function($scope, $http) { +.controller('MainCtrl', function($scope, $http, fileReader) { $scope.response = ""; - $scope.model_file={}; - + $scope.imageSrc=""; $scope.uploadFile = function() { + var blob = dataURItoBlob(selectedImg); + $scope.img_file= blob; + console.log("$scope.img_file"); console.log($scope.img_file); var fd = new FormData(); @@ -31,50 +33,94 @@ angular.module('app.main', []) }); }; - /*$scope.takePhoto = function() { - alert("a"); - console.log("take photo"); - var options = { - quality: 100, - destinationType: Camera.DestinationType.DATA_URL, - sourceType: Camera.sourceType, - allowEdit: true, - encodingType: Camera.EncodingType.PNG, - targetWidth: 500, - targetHeight: 500, - popoverOptions: CameraPopoverOptions, - saveToPhotoAlbum: false, - correctOrientation:true - }; + function dataURItoBlob(dataURI) { + // convert base64/URLEncoded data component to raw binary data held in a string + var byteString; + if (dataURI.split(',')[0].indexOf('base64') >= 0) + byteString = atob(dataURI.split(',')[1]); + else + byteString = unescape(dataURI.split(',')[1]); - $cordovaCamera.getPicture(options).then(function(imageData) { - //$scope.user.newAvatar = "data:image/jpeg;base64," + imageData; - $scope.img.imgdata = "data:image/jpeg;base64," + imageData; - $scope.img.img = imageData; - }, function(err) { - console.log(err); - }); - };*/ -}) -.directive('fileModel', [ - '$parse', - function($parse) { - return { - restrict: 'A', - link: function(scope, element, attrs) { - var model = $parse(attrs.fileModel); - var modelSetter = model.assign; + // separate out the mime component + var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; - element.bind('change', function() { - scope.$apply(function() { - if (attrs.multiple) { - modelSetter(scope, element[0].files); - } else { - modelSetter(scope, element[0].files[0]); - } - }); - }); - } - }; + // write the bytes of the string to a typed array + var ia = new Uint8Array(byteString.length); + for (var i = 0; i < byteString.length; i++) { + ia[i] = byteString.charCodeAt(i); + } + + return new Blob([ia], {type:mimeString}); } -]); + +}) +.directive("ngFileSelect", function(fileReader, $timeout) { + return { + scope: { + ngModel: '=' + }, + link: function($scope, el) { + function getFile(file) { + fileReader.readAsDataUrl(file, $scope) + .then(function(result) { + $timeout(function() { + $scope.ngModel = result; + selectedImg = result; + }); + }); + } + + el.bind("change", function(e) { + var file = (e.srcElement || e.target).files[0]; + getFile(file); + }); + } + }; + }) +.factory("fileReader", function($q, $log) { + var onLoad = function(reader, deferred, scope) { + return function() { + scope.$apply(function() { + deferred.resolve(reader.result); + }); + }; + }; + + var onError = function(reader, deferred, scope) { + return function() { + scope.$apply(function() { + deferred.reject(reader.result); + }); + }; + }; + + var onProgress = function(reader, scope) { + return function(event) { + scope.$broadcast("fileProgress", { + total: event.total, + loaded: event.loaded + }); + }; + }; + + var getReader = function(deferred, scope) { + var reader = new FileReader(); + reader.onload = onLoad(reader, deferred, scope); + reader.onerror = onError(reader, deferred, scope); + reader.onprogress = onProgress(reader, scope); + return reader; + }; + + var readAsDataURL = function(file, scope) { + var deferred = $q.defer(); + + var reader = getReader(deferred, scope); + reader.readAsDataURL(file); + + return deferred.promise; + }; + + return { + readAsDataUrl: readAsDataURL + }; +}); diff --git a/smartphoneApp/www/templates/main.html b/smartphoneApp/www/templates/main.html index 7ea6eb0..9a71834 100644 --- a/smartphoneApp/www/templates/main.html +++ b/smartphoneApp/www/templates/main.html @@ -1,6 +1,6 @@ -