├── .github └── workflows │ ├── conda-tests.yml │ └── tests.yml ├── LICENSE ├── __pycache__ └── app.cpython-310.pyc ├── app.css ├── app.py ├── images └── cat.jpg ├── img ├── Python Image Classification _ Flask - Personal - Microsoft​ Edge 4_4_2022 3_32_20 PM.png ├── choose.png ├── flask-import.png ├── keras-modules.png ├── pic.png ├── predict.png └── terminal.png ├── readme.md └── templates └── app.html /.github/workflows/conda-tests.yml: -------------------------------------------------------------------------------- 1 | name: Conda CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build-windows: 7 | runs-on: windows-latest 8 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a single version of Python 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Tests 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | permissions: 13 | contents: read 14 | 15 | jobs: 16 | build: 17 | 18 | runs-on: ubuntu-latest 19 | 20 | steps: 21 | - uses: actions/checkout@v3 22 | - name: Set up Python 3.10 23 | uses: actions/setup-python@v3 24 | with: 25 | python-version: "3.10" 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Gerald Maduabuchi 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 | -------------------------------------------------------------------------------- /__pycache__/app.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/__pycache__/app.cpython-310.pyc -------------------------------------------------------------------------------- /app.css: -------------------------------------------------------------------------------- 1 | .form-style { 2 | padding-top: 25rem; 3 | width: 50rem; 4 | margin-left: 50rem; 5 | } 6 | input[type=file] { 7 | color:red; 8 | padding-bottom: 3rem; 9 | } 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # Importing flask module 2 | 3 | from flask import Flask, render_template, request 4 | 5 | # Importing Keras Modules 6 | 7 | from keras.preprocessing.image import img_to_array 8 | from keras.preprocessing.image import load_img 9 | from keras.applications.vgg16 import preprocess_input 10 | from keras.applications.vgg16 import VGG16 11 | from keras.applications.vgg16 import decode_predictions 12 | 13 | # Start App 14 | app = Flask(__name__) 15 | 16 | #The Model 17 | model = VGG16() 18 | 19 | # Get Route 20 | @app.route('/', methods=['GET']) 21 | def hello_world(): 22 | #Render Template 23 | return render_template('app.html') 24 | 25 | # Receive input and post to model 26 | @app.route('/', methods=['POST']) 27 | def predict(): 28 | fileImage = request.files['fileImage'] 29 | image_path = "./images/" + fileImage.filename 30 | fileImage.save(image_path) 31 | 32 | #Load Image 33 | image = load_img(image_path, target_size=(224, 224)) 34 | image = img_to_array(image) 35 | image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) 36 | 37 | #Process Image 38 | image = preprocess_input(image) 39 | 40 | #Make Predictions 41 | yhat = model.predict(image) 42 | label = decode_predictions(yhat) 43 | label = label[0][0] 44 | 45 | #Make Classifications 46 | classification = '%s (%.2f%%)' % (label[1], label[2]*100) 47 | 48 | return render_template('app.html', prediction=classification) 49 | 50 | # Intitalise Port 51 | if __name__ == '__main__': 52 | app.run(port=3000, debug=False) 53 | 54 | -------------------------------------------------------------------------------- /images/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/images/cat.jpg -------------------------------------------------------------------------------- /img/Python Image Classification _ Flask - Personal - Microsoft​ Edge 4_4_2022 3_32_20 PM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/Python Image Classification _ Flask - Personal - Microsoft​ Edge 4_4_2022 3_32_20 PM.png -------------------------------------------------------------------------------- /img/choose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/choose.png -------------------------------------------------------------------------------- /img/flask-import.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/flask-import.png -------------------------------------------------------------------------------- /img/keras-modules.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/keras-modules.png -------------------------------------------------------------------------------- /img/pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/pic.png -------------------------------------------------------------------------------- /img/predict.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/predict.png -------------------------------------------------------------------------------- /img/terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grayoj/image-classifier/e5ece5c182a4652dbe2242b31fadbccb8f9aca58/img/terminal.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Image Classification in Python 2 | ![CI](https://github.com/grayoj/image-classifier/actions/workflows/tests.yml/badge.svg) 3 | [![Total alerts](https://img.shields.io/lgtm/alerts/g/grayoj/image-classifier.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/grayoj/image-classifier/alerts/) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/grayoj/image-classifier.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/grayoj/image-classifier/context:python) [![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/grayoj/image-classifier.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/grayoj/image-classifier/context:javascript) 4 | 5 | 6 | **Implementing image classification in Flask using Keras.** 7 | 8 | The VGG16 is a convolution neural network model architecture that is the best classifier for images till today. This project implements the classifier through Keras. This appplication was written in Python. It also utilizes the Flask Microframework, as the server to render a template, and pass the submitted file to the prediction model. It also uses Keras modules to perform image classification. 9 | 10 | ## Requirements 11 | 12 | * **Python 3.5+** 13 | * **Pip (For installing modules)** 14 | * **Flask** Download 15 | * **Tensorflow** 16 | * **Keras Modules** 17 | 18 | ## Installation 19 | 20 | ### (Plain Set Up) 21 | 22 | Go ahead an clone this specific repository. You could do this through the command below: 23 | 24 | ```git 25 | git clone https://github.com/grayoj/Python-Image-Classification.git 26 | ``` 27 | Then navigate to the directory. If you use VSCode, you could avoid interacting with the terminal. 28 | 29 | Run the following commands in the directory: 30 | 31 | ```git 32 | python -m flask run 33 | ``` 34 | 35 | ### (Full Set Up) 36 | 37 | Go ahead an clone this specific repository. You could do this through the command below: 38 | 39 | ```git 40 | git clone https://github.com/grayoj/Python-Image-Classification.git 41 | ``` 42 | 43 | To install Python, download here. If you already have Python 3.5 installed, you may proceed to the next steps below: 44 | 45 | You will notice this line of code in the ``app.py`` file: 46 | 47 | 48 | 49 | To ensure the modules would be imported on your system, into the project, run the following command: 50 | 51 | ```powershell 52 | pip install flask 53 | ``` 54 | 55 | That would install flask on your local machine. 56 | Next step is to install the Keras Modules, and packages required. Run the following command: 57 | 58 | > pip install keras 59 | 60 | If you use Pylance, it should validate the imports above in the ``app.py``. i.e show no errors, of modules missing. Modules being installed: 61 | 62 | 63 | 64 | You would have to install Tensorflow as well. 65 | 66 | > pip install tensorflow 67 | 68 | You are set. Now let's dial in to a localhost port. 69 | 70 | > python -m flask run 71 | 72 | Viola, the application should load sucessfully. If there are any errors, ensure you installed the modules properly. 73 | 74 | Http://127.0.0.1:5000 75 | 76 | Don't worry if you notice a sudden download process. Tensorflow would begin to download dependencies for the VGG16 Covolutional Neural network model. 77 | 78 | 79 | 80 | 81 | ## Prediction in action 82 | 83 | Now, the fun part. This should have loaded open: 84 | 85 | 86 | 87 | Let's see whether our model can predict what this animal is: 88 | The picture used is in the repository. You could use other images, and have fun. 89 | 90 | 91 | 92 | Now let's input it 93 | 94 | 95 | 96 | Click on predict 97 | 98 | 99 | 100 | Our model predicted a tiger cat! Epic. 101 | 102 | ## More Information 103 | 104 | Reach out to me if you have questions or suggestions. Would love to connect. 105 | 106 | **** 107 | Twitter: @geraldabuchi 108 | 109 | # Python-Image-Classification - MIT License 110 | Flask, Keras, Tensorflow, VGG16 111 | -------------------------------------------------------------------------------- /templates/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Python Image Classification | Flask 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 |
16 | 17 |
18 | 19 |
20 | 21 | 22 | {% if prediction %} 23 | The image you uploaded is a {{prediction}} 24 | {% endif %} 25 |
26 | 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------