I7 · Computer Vision in Practice

Putting an Image Model on a Web Page

There are two main ways to put an image model on a web page. In-browser inference converts the model to a web format, such as TensorFlow.js or ONNX, and runs it in the visitor's browser using JavaScript with WebGL, WebGPU or WebAssembly, so images never leave the device. Server-side inference sends each image to a server running the model, for example behind a FastAPI endpoint, and returns the prediction. Either way, the web page must preprocess images exactly as during training and present predictions clearly, with confidence and limits.

15 min Intermediate · Sublevel Advanced Jump to the activity

What you need: The earlier topics of this module.

Definition

A model that lives only in your notebook helps nobody. Put it on a web page, and anyone with a phone can point a camera at a leaf and get an answer, without installing anything and often without even sending the photo anywhere.

There are two main ways to put an image model on a web page. In-browser inference converts the model to a web format, such as TensorFlow.js or ONNX, and runs it in the visitor's browser using JavaScript with WebGL, WebGPU or WebAssembly, so images never leave the device. Server-side inference sends each image to a server running the model, for example behind a FastAPI endpoint, and returns the prediction. Either way, the web page must preprocess images exactly as during training and present predictions clearly, with confidence and limits.

In simple words

You can either carry the chef to every customer's home (in-browser: the model goes to the user) or have every customer send their order to the restaurant kitchen (server: the image goes to the model). Each has trade-offs in speed, privacy and cost.

Overview

  • An image model can run in the browser, on a server or through a cloud API.
  • Browser models (TensorFlow.js, ONNX Runtime Web) keep images on the user's device.
  • Server models are called with an HTTP request and can be larger and more accurate.
  • The web page must preprocess images exactly as during training: same size, scaling and colour order.
  • Good apps show confidence and handle failures gracefully.
  • Privacy, consent and model size decide which approach to choose.

Why it matters

Deployment is where a project becomes useful. Web pages work on almost every device, need no app store and, with in-browser inference, protect privacy and keep working with a weak internet connection.

Key terms

Inference
Running a trained model to get predictions on new inputs.
TensorFlow.js
A JavaScript library for running and training models in the browser.
ONNX
An open format for models that many tools can run, including ONNX Runtime Web.
Preprocessing parity
Making the web page resize, order channels and scale pixels exactly as in training.
Quantisation
Storing weights with fewer bits, such as 8-bit integers, to make the model smaller and faster.
Latency
The time from submitting an image to seeing the result.

Types

There are several types of deployment for an image model on the web. Each trades privacy, speed and accuracy differently.

1. In-browser inference

Running the model in the user's browser with JavaScript or WebAssembly.

  • How it works: The model file is downloaded once; predictions run on the device, often using the GPU through WebGL or WebGPU.
  • Example: A Teachable Machine model exported to TensorFlow.js classifying webcam frames on a school website.
  • When to use it: Use it for privacy and offline use, with small models of a few megabytes.

2. Server-side API

Hosting the model on a server and calling it from the page.

  • How it works: A framework such as FastAPI or Flask receives the image, runs the model and returns JSON with labels and scores.
  • Example: A crop-disease website sending a leaf photo to a server and showing the diagnosis.
  • When to use it: Use it for large models, frequent updates and centralised logging.

3. Cloud ML services and serverless functions

Managed services that host models and scale automatically.

  • How it works: The provider runs the model; you pay per request; cold starts can add delay.
  • Example: Hosting a classifier on a managed endpoint used by a mobile app and a website.
  • When to use it: Use them when traffic varies and you prefer not to manage servers.

4. On-device mobile and edge deployment

Running optimised models in mobile apps or small devices.

  • How it works: Models are converted to TensorFlow Lite, Core ML or ONNX and often quantised to 8-bit integers for speed.
  • Example: A farmer's offline app identifying pests without mobile network coverage.
  • When to use it: Use it where internet is unreliable or responses must be instant.

5. Demo frameworks

Python tools that build a web interface around a model in a few lines.

  • How it works: Gradio and Streamlit create upload buttons, previews and result displays, and can be shared through links or Hugging Face Spaces.
  • Example: A Gradio demo of a handwritten-digit classifier for a class presentation.
  • When to use it: Use them for prototypes and showing work; build a proper app for production.

Explained in detail

Browser or server?

In the browserOn a server
PrivacyImages stay on the deviceImages are sent over the internet
SpeedNo network delay; depends on the visitor's deviceNetwork delay; servers can use powerful GPUs
Model sizeMust be small, usually a few MB to tens of MB
CostAlmost free: a static web page
Updates and secrecyModel files are downloadable by anyone

The in-browser recipe

Train or fine-tune a small model such as MobileNet, then convert it: Keras models with the tensorflowjs converter, PyTorch models by exporting to ONNX. Teachable Machine exports a ready TensorFlow.js model with a code snippet. On the page, JavaScript loads the model files once, captures a frame from the camera or an uploaded photo, resizes it to the model's input size, often 224 × 224, puts the channels in the right order, scales pixel values exactly as in training, runs the model, applies softmax and shows the top predictions with their probabilities. Quantising weights from 32-bit floats to 8-bit integers makes the download about four times smaller, usually with only a small loss of accuracy.

Designing the page responsibly

  • Show the confidence and a clear message such as "not sure, please consult an expert" when it is low.
  • Explain what the model can recognise and what it cannot, for example "tomato leaves only".
  • Tell users whether images leave their device, and ask permission before using the camera.
  • Make the page work on low-cost phones and slow connections: small models, cached files and simple layouts.
  • A mismatch in preprocessing, such as forgetting to divide by 255 or swapping channel order, silently ruins accuracy; test the web version against the original on the same images.

Server-side deployment follows a similar pattern in Python. A small web service, often written with FastAPI or Flask, loads the model once when it starts, receives an uploaded image, applies the same preprocessing, runs the model and returns the top predictions as JSON. The web page sends the image with a fetch request and displays the answer. Servers must limit image size, reject files that are not images, delete uploads after use unless users agree otherwise, and protect the service with rate limits so that it is not overwhelmed or abused.

How it works

  1. Choose a small, accurate model and convert it to TensorFlow.js or ONNX.
  2. Write the page to load the model and capture camera or uploaded images.
  3. Reproduce the training preprocessing exactly.
  4. Show the top predictions, confidence and a clear message for uncertain cases.
  5. Test on several phones and compare with the original model's outputs.

In code

web_pipeline.py
import json
import numpy as np
from PIL import Image

LABELS = ["healthy", "early blight", "late blight"]

def preprocess(photo):                             # must match training exactly
    img = photo.convert("RGB").resize((224, 224))
    x = np.asarray(img, dtype=np.float32) / 255.0  # scale to 0-1
    return x[None, ...]                            # add a batch dimension

def softmax(z):
    e = np.exp(z - z.max())
    return e / e.sum()

photo = Image.new("RGB", (640, 480), (70, 140, 60))     # stand-in for a camera frame
x = preprocess(photo)
print("Model input shape:", x.shape, " value range:", round(float(x.min()), 2), "to", round(float(x.max()), 2))

logits = np.array([2.1, 0.3, -0.8])                # what the model might return
probs = softmax(logits)
result = [{"label": l, "confidence": round(float(p), 2)} for l, p in sorted(zip(LABELS, probs), key=lambda t: -t[1])]
message = "confident" if probs.max() >= 0.7 else "not sure: please ask an expert"
print(json.dumps({"top": result, "message": message}))

params = 2_500_000                                  # a small MobileNet-sized model
print(f"Download size: float32 {params * 4 / 1e6:.0f} MB, int8 quantised {params / 1e6:.1f} MB")

Output

Model input shape: (1, 224, 224, 3)  value range: 0.24 to 0.55
{"top": [{"label": "healthy", "confidence": 0.82}, {"label": "early blight", "confidence": 0.14}, {"label": "late blight", "confidence": 0.05}], "message": "confident"}
Download size: float32 10 MB, int8 quantised 2.5 MB

Try it yourself

From trained model to web page

Put the steps in order.

  1. 1Test on real phones and compare with the original model
  2. 2Load the model in the page's JavaScript
  3. 3Run the model and show top predictions with confidence
  4. 4Capture an image and preprocess it exactly as in training
  5. 5Convert it to TensorFlow.js or ONNX, optionally quantised
  6. 6Train or fine-tune a small model

Browser or server?

Where should the model run for each project?

  • A 2 GB medical model needing a powerful GPU

  • A model whose weights must stay secret

  • A plant-disease checker for farmers with patchy mobile data

  • A webcam game controlled by hand gestures

Common mistakes

  • Preprocessing differently in the browser than in training.
  • Showing a single label with no confidence or warning.
  • Shipping a large model that low-cost phones cannot load.

Check yourself

Question 1

1. What is a main privacy benefit of in-browser inference?

Question 2

2. The web version gives much worse results than in testing. A likely cause is…

Question 3

3. Quantising weights from 32-bit floats to 8-bit integers makes the model about…

Summary

  • Models can run in the browser with TensorFlow.js or ONNX, or on a server.
  • Browser inference protects privacy and works offline; servers handle large, secret models.
  • Match preprocessing exactly, keep models small and show confidence and limits.

Finished reading?

Sources: TensorFlow.js documentation · ONNX Runtime Web documentation