Created
July 27, 2022 03:16
-
-
Save dpotikan/7faf9c34c0df16a8744ad88956c68450 to your computer and use it in GitHub Desktop.
VGG16 Image Classification (Flask Web Application) - Thanks to https://youtu.be/0nr6TPKlrN0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template, request | |
| from tensorflow.keras.preprocessing.image import load_img | |
| from tensorflow.keras.preprocessing.image import img_to_array | |
| from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions | |
| from tensorflow.keras.applications.vgg16 import VGG16 | |
| # from tensorflow.keras.applications.resnet50 import ResNet50 | |
| app = Flask(__name__) | |
| model = VGG16() | |
| @app.route('/', methods=['GET']) | |
| def hello_world(): | |
| # return 'Hello world' | |
| return render_template('index.html') | |
| @app.route('/', methods=['POST']) | |
| def predict(): | |
| imagefile = request.files['imagefile'] | |
| image_path = "./images/" + imagefile.filename | |
| imagefile.save(image_path) | |
| image = load_img(image_path, target_size=(224,224)) | |
| image = img_to_array(image) | |
| image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) | |
| image = preprocess_input(image) | |
| yhat = model.predict(image) | |
| label = decode_predictions(yhat) | |
| label = label[0][0] | |
| classification_text = f'{label[1]} ({label[2]*100})' | |
| return render_template('index.html', prediction=classification_text) | |
| if __name__ == '__main__': | |
| app.run(port=3000, debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <!-- CSS only --> | |
| <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous"> | |
| <title>Image Classifier</title> | |
| </head> | |
| <body> | |
| <h1 class="text-center">Image Classifier</h1> | |
| <form class="p-3 text-center" action="/" method="POST" enctype="multipart/form-data"> | |
| <input class="form-control" type="file" name="imagefile" id=""> | |
| <input class="btn btn-primary mt-3" type="submit" value="Predict Image"> | |
| </form> | |
| {% if prediction %} | |
| <p class="text-center"> Image is a {{prediction}}</p> | |
| {% endif %} | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment