����JFIF��H�H����Exif��MM�*���� ��3����V�����3������3�(��������������������3�����
Server IP : 162.0.232.25 / Your IP : 18.219.107.243 Web Server : LiteSpeed System : Linux premium276.web-hosting.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : kwacuqig ( 988) PHP Version : 8.2.26 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/kwacuqig/fsiapp/ |
Upload File : |
import os from flask import Flask, render_template, request, jsonify from werkzeug.utils import secure_filename import tensorflow as tf from PIL import Image import numpy as np app = Flask(__name__) # Load the trained model model = tf.keras.models.load_model('model/model.h5') # Define the allowed extensions for uploaded files ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png'} # Function to check if the file extension is allowed def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS # Route for the home page @app.route('/') def index(): return render_template('index-2.html') # Route to handle image classification @app.route('/predict', methods=['POST']) def predict(): # Check if an image was uploaded if 'file' not in request.files: return jsonify({'error': 'No file uploaded'}) file = request.files['file'] # Check if the file has an allowed extension if file.filename == '' or not allowed_file(file.filename): return jsonify({'error': 'Invalid file extension'}) # Save the uploaded file filename = secure_filename(file.filename) filepath = os.path.join('static/uploads', filename) file.save(filepath) compress_image(filepath) # Open and preprocess the image img = Image.open(filepath) img = img.resize((224, 224)) img_array = np.array(img) / 255.0 img_array = np.expand_dims(img_array, axis=0) # Make the prediction prediction = model.predict(img_array) predicted_class = np.argmax(prediction) # Map the predicted class index to the actual class labels class_labels = {0 : 'Chambo', 1 : 'Makakana', 2: 'Mcheni', 3: 'Ndunduma', 4: 'Utaka'} predicted_label = class_labels[predicted_class] # Return the predicted label return jsonify({'result': predicted_label, 'path': filepath}) def compress_image(image_path): # Open the image img = Image.open(image_path) # Compress the image compressed_image = img.copy() compressed_image.save(image_path, optimize=True, quality=20) return image_path if __name__ == '__main__': app.run(debug=True)