diff --git a/django_project/akangatu/__pycache__/__init__.cpython-36.pyc b/django_project/akangatu/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index 8de1afe..0000000 Binary files a/django_project/akangatu/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/django_project/akangatu/__pycache__/views.cpython-36.pyc b/django_project/akangatu/__pycache__/views.cpython-36.pyc deleted file mode 100644 index bf2af2b..0000000 Binary files a/django_project/akangatu/__pycache__/views.cpython-36.pyc and /dev/null differ diff --git a/django_project/akangatu/core/predictor.py b/django_project/akangatu/core/predictor.py index 9a1bc2c..c35ac3f 100644 --- a/django_project/akangatu/core/predictor.py +++ b/django_project/akangatu/core/predictor.py @@ -2,6 +2,8 @@ from .preprocessor import Preprocessor +preprocessor = Preprocessor() + class Predictor: _model = None @@ -15,7 +17,7 @@ def predict(self, pixel_array): if not self._model: self._load_model() - pixels = Preprocessor.reshape_data(pixel_array) + pixels = preprocessor.image_to_mnist(pixel_array) probs = self._model.predict(pixels) return probs.argmax(axis=1) diff --git a/django_project/akangatu/core/preprocessor.py b/django_project/akangatu/core/preprocessor.py index fa863df..f13cfb9 100644 --- a/django_project/akangatu/core/preprocessor.py +++ b/django_project/akangatu/core/preprocessor.py @@ -1,6 +1,39 @@ import numpy as np + +MNIST_SIZE = 28 +COLOR_SCALE = 255 + class Preprocessor: - def reshape_data(request): - pixel_array = np.array(request) - return pixel_array.reshape(-1, 1, 28, 28) / 255.0 + def image_to_mnist(self, image): + pixel_array = self._aggregate_to_mnist_array(image) + + return self._reshape_data(pixel_array) / COLOR_SCALE + + def _reshape_data(self, pixel_array): + return pixel_array.reshape(1, 1, MNIST_SIZE, MNIST_SIZE) + + def _get_group_sizes(self, image): + image_width = len(image[0]) + image_height = len(image) + + return image_height // MNIST_SIZE, image_width // MNIST_SIZE + + def _aggregate_to_mnist_array(self, image): + mnist_array = np.zeros([MNIST_SIZE, MNIST_SIZE]) + + for i in range(MNIST_SIZE): + for j in range(MNIST_SIZE): + mnist_array[i][j] = self._calculate_mnist_pixel(image,i,j) + + return mnist_array + + def _calculate_mnist_pixel(self, image,line,column): + group_height, group_width = self._get_group_sizes(image) + + rgb_sum = 0 + for i in range(group_height): + for j in range(group_width): + rgb_sum += sum(image[line*group_height+i][column*group_width+j])//len(image[line][column]) + + return rgb_sum / (group_width * group_height) diff --git a/django_project/akangatu/views.py b/django_project/akangatu/views.py index 3dbb460..981416e 100644 --- a/django_project/akangatu/views.py +++ b/django_project/akangatu/views.py @@ -2,6 +2,7 @@ from django_quicky import routing from django.views.decorators.csrf import csrf_exempt import json +import matplotlib.image as img from .models import Predictor @@ -25,11 +26,11 @@ def predict(request): if request.method.upper() != 'POST': return HttpResponseNotAllowed(['POST']) # List of allowed ones - json_data = request.POST.dict() or json.loads(request.body.decode('utf-8')) + image_file = next(iter(request.FILES.values())) - print(json_data) + image = img.imread(image_file) - pred = predictor_acessor.predict(json_data['pixels']) + pred = predictor_acessor.predict(image) answer = {'label': int(pred[0]) } diff --git a/django_project/django_project/__pycache__/__init__.cpython-36.pyc b/django_project/django_project/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index a40e53a..0000000 Binary files a/django_project/django_project/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/django_project/django_project/__pycache__/settings.cpython-36.pyc b/django_project/django_project/__pycache__/settings.cpython-36.pyc deleted file mode 100644 index 3eb234e..0000000 Binary files a/django_project/django_project/__pycache__/settings.cpython-36.pyc and /dev/null differ diff --git a/django_project/django_project/__pycache__/urls.cpython-36.pyc b/django_project/django_project/__pycache__/urls.cpython-36.pyc deleted file mode 100644 index e573d2d..0000000 Binary files a/django_project/django_project/__pycache__/urls.cpython-36.pyc and /dev/null differ diff --git a/django_project/django_project/__pycache__/wsgi.cpython-36.pyc b/django_project/django_project/__pycache__/wsgi.cpython-36.pyc deleted file mode 100644 index 0b4e6b2..0000000 Binary files a/django_project/django_project/__pycache__/wsgi.cpython-36.pyc and /dev/null differ diff --git a/requirements.txt b/requirements.txt index 49cb872..af3703e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,9 @@ Keras==2.2.4 Keras-Applications==1.0.6 Keras-Preprocessing==1.0.5 Markdown==3.0.1 +matplotlib==3.0.2 numpy==1.15.4 +Pillow==5.3.0 protobuf==3.6.1 pytz==2018.7 PyYAML==3.13