| from flask import Flask, render_template, request |
| from tensorflow.keras.models import load_model |
| from tensorflow.keras.preprocessing import image |
| import numpy as np |
| import os |
| import uuid |
| import tensorflow as tf |
| import random |
|
|
| |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' |
| tf.random.set_seed(42) |
| np.random.seed(42) |
| random.seed(42) |
|
|
| app = Flask(__name__) |
|
|
| |
| model = load_model("model/cat_dog_neither_classifier_new.h5", compile=False) |
| |
|
|
| class_names = ['cat', 'dog', 'neither'] |
| UPLOAD_FOLDER = 'static/uploads' |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
|
|
| def preprocess_image(img_path): |
| img = image.load_img(img_path, target_size=(224, 224)) |
| img_array = image.img_to_array(img) / 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
| return img_array |
|
|
| @app.route('/', methods=['GET']) |
| def index(): |
| return render_template('upload.html') |
|
|
| @app.route('/predict', methods=['POST']) |
| def predict(): |
| if 'file' not in request.files: |
| return "No file part", 400 |
|
|
| file = request.files['file'] |
| if file.filename == '': |
| return "No selected file", 400 |
|
|
| filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1] |
| img_path = os.path.join(UPLOAD_FOLDER, filename) |
| file.save(img_path) |
|
|
| |
| processed = preprocess_image(img_path) |
|
|
| |
| prediction = model.predict(processed)[0] |
| prediction /= np.sum(prediction) |
|
|
| class_index = int(np.argmax(prediction)) |
| confidence = round(float(np.max(prediction)) * 100, 2) |
| final_class = class_names[class_index] |
|
|
| return render_template( |
| 'result.html', |
| prediction=final_class, |
| confidence=confidence, |
| img_path='/' + img_path |
| ) |
|
|
| if __name__ == '__main__': |
| import os |
| |
| |
| port = int(os.environ.get("PORT", 7860)) |
| app.run(host='0.0.0.0', port=port, debug=False) |
|
|