Step-by-step guide: Upload images to your web app and run an ML model for fast, effective AI insights.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// HTML form for image upload
import os
from flask import Flask, request, redirect, url_for, render_template // Importing necessary modules
from werkzeug.utils import secure\_filename // Utility to secure the file name before saving
app = Flask(name)
app.config["UPLOAD_FOLDER"] = "uploads" // Define a folder to save the uploaded files
app.config["ALLOWED_EXTENSIONS"] = {"png", "jpg", "jpeg", "gif"} // Acceptable image formats
def allowed_file(filename):
// Check if the file extension is allowed
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"]
@app.route("/upload", methods=["POST"])
def upload_image():
if "image" not in request.files:
return "No file part in the request", 400 // Bad request if file key is missing
file = request.files["image"]
if file.filename == "":
return "No selected file", 400 // Bad request for empty file name
if file and allowed_file(file.filename):
filename = secure_filename(file.filename) // Prevent directory traversal
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(file_path) // Save file to server
// Once saved, proceed to process the image with the ML model
result = run_ml_model(file_path) // Passing file path to ML model function
return render_template("result.html", result=result) // Render result on a new page
else:
return "File type not permitted", 400 // Return error for disallowed file types
// For local testing set
if name == "main":
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True) // Ensure the uploads folder exists
app.run(debug=True)
from PIL import Image
import numpy as np
def preprocess_image(image_path):
// Open the image using PIL
img = Image.open(image_path)
// Resize the image to 224x224 pixels if required by the ML model
img = img.resize((224, 224))
// Convert image to a numpy array for further processing
img_array = np.array(img)
// Optional: Normalize the pixel values to the range [0, 1]
img_array = img_array / 255.0
// Expand dimensions if the model expects a batch (e.g., shape becomes (1, 224, 224, 3))
img_array = np.expand_dims(img_array, axis=0)
return img_array
import tensorflow as tf
// Pre-load the model when the server starts to avoid reloading on each request
model = tf.keras.models.load_model("my_trained_model.h5") // Assuming your model is saved in Keras format
def run_ml_model(image_path):
// Preprocess the image
processed_img = preprocess_image(image_path)
// Run model inference to predict the content of the image
predictions = model.predict(processed_img)
// Process predictions, e.g., convert probabilities to class labels
// This example assumes a classification task
predicted_class = tf.argmax(predictions, axis=1).numpy()[0]
return predicted_class
// Example Flask route rendering the result using a template
@app.route("/result")
def show_result():
// The 'result.html' template should be designed to display the prediction outcome
return render_template("result.html", result="Insert your prediction here")
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â