├── .gitattributes ├── LICENSE ├── core ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-38.pyc │ ├── settings.cpython-38.pyc │ ├── urls.cpython-38.pyc │ ├── views.cpython-38.pyc │ └── wsgi.cpython-38.pyc ├── asgi.py ├── settings.py ├── urls.py ├── views.py └── wsgi.py ├── db.sqlite3 ├── manage.py ├── requirements.txt └── templates └── index.html /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Very Academy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__init__.py -------------------------------------------------------------------------------- /core/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/settings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__pycache__/settings.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/urls.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__pycache__/urls.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/views.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__pycache__/views.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/wsgi.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/core/__pycache__/wsgi.cpython-38.pyc -------------------------------------------------------------------------------- /core/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for core project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /core/settings.py: -------------------------------------------------------------------------------- 1 | import tensorflow.compat.v1 as tf 2 | from keras.applications import vgg16 3 | from tensorflow.python.keras.backend import set_session 4 | import os 5 | #### 6 | # Backward compatible 7 | tf.disable_v2_behavior() 8 | #### 9 | 10 | SESS = tf.compat.v1.Session() 11 | GRAPH1 = tf.get_default_graph() 12 | # Sets the global TensorFlow session. 13 | set_session(SESS) 14 | 15 | IMAGE_MODEL = vgg16.VGG16(weights="imagenet") 16 | 17 | from pathlib import Path 18 | 19 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 20 | BASE_DIR = Path(__file__).resolve().parent.parent 21 | 22 | 23 | # Quick-start development settings - unsuitable for production 24 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 25 | 26 | # SECURITY WARNING: keep the secret key used in production secret! 27 | SECRET_KEY = '=5!%)x5sb969tjinw@owstukfubvb_h6f^+(sr)!=kb-w=(8tm' 28 | 29 | # SECURITY WARNING: don't run with debug turned on in production! 30 | DEBUG = True 31 | 32 | ALLOWED_HOSTS = [] 33 | 34 | 35 | # Application definition 36 | 37 | INSTALLED_APPS = [ 38 | 'django.contrib.admin', 39 | 'django.contrib.auth', 40 | 'django.contrib.contenttypes', 41 | 'django.contrib.sessions', 42 | 'django.contrib.messages', 43 | 'django.contrib.staticfiles', 44 | ] 45 | 46 | MIDDLEWARE = [ 47 | 'django.middleware.security.SecurityMiddleware', 48 | 'django.contrib.sessions.middleware.SessionMiddleware', 49 | 'django.middleware.common.CommonMiddleware', 50 | 'django.middleware.csrf.CsrfViewMiddleware', 51 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 52 | 'django.contrib.messages.middleware.MessageMiddleware', 53 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 54 | ] 55 | 56 | ROOT_URLCONF = 'core.urls' 57 | 58 | TEMPLATES = [ 59 | { 60 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 61 | 'DIRS': [os.path.join(BASE_DIR, 'templates/')], 62 | 'APP_DIRS': True, 63 | 'OPTIONS': { 64 | 'context_processors': [ 65 | 'django.template.context_processors.debug', 66 | 'django.template.context_processors.request', 67 | 'django.contrib.auth.context_processors.auth', 68 | 'django.contrib.messages.context_processors.messages', 69 | ], 70 | }, 71 | }, 72 | ] 73 | 74 | WSGI_APPLICATION = 'core.wsgi.application' 75 | 76 | 77 | # Database 78 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 79 | 80 | DATABASES = { 81 | 'default': { 82 | 'ENGINE': 'django.db.backends.sqlite3', 83 | 'NAME': BASE_DIR / 'db.sqlite3', 84 | } 85 | } 86 | 87 | 88 | # Password validation 89 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 90 | 91 | AUTH_PASSWORD_VALIDATORS = [ 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 100 | }, 101 | { 102 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 103 | }, 104 | ] 105 | 106 | 107 | # Internationalization 108 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 109 | 110 | LANGUAGE_CODE = 'en-us' 111 | 112 | TIME_ZONE = 'UTC' 113 | 114 | USE_I18N = True 115 | 116 | USE_L10N = True 117 | 118 | USE_TZ = True 119 | 120 | 121 | # Static files (CSS, JavaScript, Images) 122 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 123 | 124 | STATIC_URL = '/static/' 125 | 126 | MEDIA_ROOT = os.path.join(BASE_DIR, "media") 127 | MEDIA_URL = "/media/" -------------------------------------------------------------------------------- /core/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path 3 | from django.conf import settings 4 | from django.conf.urls.static import static 5 | 6 | from . import views 7 | 8 | urlpatterns = [ 9 | path('', views.index, name="home"), 10 | path('admin/', admin.site.urls), 11 | ] 12 | 13 | if settings.DEBUG: 14 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -------------------------------------------------------------------------------- /core/views.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from django.conf import settings 3 | from django.core.files.storage import default_storage 4 | from django.shortcuts import render 5 | from keras.applications import vgg16 6 | from keras.applications.imagenet_utils import decode_predictions 7 | from keras.preprocessing.image import img_to_array, load_img 8 | from tensorflow.python.keras.backend import set_session 9 | 10 | def index(request): 11 | if request.method == "POST": 12 | # 13 | # Django image API 14 | # 15 | file = request.FILES["imageFile"] 16 | file_name = default_storage.save(file.name, file) 17 | file_url = default_storage.path(file_name) 18 | 19 | # 20 | # https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/load_img 21 | # 22 | image = load_img(file_url, target_size=(224, 224)) 23 | numpy_array = img_to_array(image) 24 | image_batch = np.expand_dims(numpy_array, axis=0) 25 | processed_image = vgg16.preprocess_input(image_batch.copy()) 26 | 27 | # 28 | # get the predicted probabilities 29 | # 30 | with settings.GRAPH1.as_default(): 31 | set_session(settings.SESS) 32 | predictions = settings.IMAGE_MODEL.predict(processed_image) 33 | 34 | # 35 | # Output/Return data 36 | # 37 | label = decode_predictions(predictions, top=10) 38 | return render(request, "index.html", {"predictions": label}) 39 | 40 | else: 41 | return render(request, "index.html") 42 | 43 | return render(request, "index.html") -------------------------------------------------------------------------------- /core/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for core project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT_Django_TensorFlow_Image_Classification_Basic/c50e84d9a38f7f57e548547186abc4d058318df2/db.sqlite3 -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==0.12.0 2 | asgiref==3.3.1 3 | astunparse==1.6.3 4 | cachetools==4.2.1 5 | certifi==2020.12.5 6 | chardet==4.0.0 7 | Django==3.1.7 8 | flatbuffers==1.12 9 | gast==0.3.3 10 | google-auth==1.28.0 11 | google-auth-oauthlib==0.4.3 12 | google-pasta==0.2.0 13 | grpcio==1.32.0 14 | h5py==2.10.0 15 | idna==2.10 16 | Keras==2.4.3 17 | Keras-Preprocessing==1.1.2 18 | Markdown==3.3.4 19 | numpy==1.19.5 20 | oauthlib==3.1.0 21 | opt-einsum==3.3.0 22 | Pillow==8.1.2 23 | protobuf==3.15.6 24 | pyasn1==0.4.8 25 | pyasn1-modules==0.2.8 26 | pytz==2021.1 27 | PyYAML==5.4.1 28 | requests==2.25.1 29 | requests-oauthlib==1.3.0 30 | rsa==4.7.2 31 | scipy==1.6.2 32 | six==1.15.0 33 | sqlparse==0.4.1 34 | tensorboard==2.4.1 35 | tensorboard-plugin-wit==1.8.0 36 | tensorflow==2.4.1 37 | tensorflow-estimator==2.4.0 38 | termcolor==1.1.0 39 | typing-extensions==3.7.4.3 40 | urllib3==1.26.4 41 | Werkzeug==1.0.1 42 | wrapt==1.12.1 43 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 |
6 | {{predictions}} --------------------------------------------------------------------------------