Build a Python microservice that performs real-time image recognition using TensorFlow.
- User
Build a Python microservice that performs real-time image recognition using TensorFlow.
- Assistant
Building the microservice
I’m thinking about how to create a Python microservice that does real-time image recognition using TensorFlow. Since the user didn't give specific constraints, I'll assume they want it built with a REST API, likely using FastAPI for its modern features. We'll base it on TensorFlow 2.x, using a pre-trained model, maybe MobileNet, while aiming for low latency. I'll also want to consider performance aspects like loading the model efficiently and possibly using a GPU. I’ll include a Dockerfile, requirements, and a sample request.Designing the image recognition service
I need to ensure the code works with TensorFlow 2.x, using either
tf.keras.applications.mobilenet_v2or EfficientNet for image recognition. I'll provide label mapping with ImageNet labels, ideally embedding them for offline use. Since I want to keep it concise, I can show code that fetches them usingutil.get_file. My microservice will feature a POST endpoint at/predict, accepting images as multipart/form-data or base64. I’ll choose FastAPI and uvicorn for the framework, with the main steps outlined clearly inapp.py, along with a Dockerfile and requirements.txt.Structuring the microserviceI need to think about whether to consider asynchronous endpoints for my FastAPI microservice. While model inference can be CPU-bound, I might not need the complexity of async, especially since the user didn't specifically ask for it. I could mention options like using a GPU for better performance, but that’s optional.
For my response, I’ll structure it clearly: First, I’ll introduce the concept of building a microservice using FastAPI and TensorFlow with a pre-trained ImageNet classifier. Then, I’ll provide the file structure needed for the project.Crafting the code for the service
I'm writing code for
app.pyto create my real-time image recognition service using FastAPI and TensorFlow. First, I’m importing necessary libraries, such as FastAPI for the app and PIL for image processing.Next, I’ll define the FastAPI app and a global model variable. I'll load the MobileNetV2 model on startup, optionally warming it up. The image preparation function will convert and resize images for processing. I’ll set up the prediction endpoint to handle uploads, validate file types, and return predictions in the specified format.Finalizing the microservice setup
I'm wrapping up the code and structure for my image recognition microservice. In
requirements.txt, I'll specifyfastapi,uvicorn[standard],tensorflow,pillow, and optionallynumpy. For the Dockerfile, I’ll use a slim Python image, set the working directory, and install the necessary system libraries before copying and installing dependencies.I’ll also provide performance tips, such as using TFLite for faster inference and running model predictions in a separate thread for concurrency. Finally, I could add a health check endpoint to monitor the service, but it's optional. I’ll ensure to keep the explanation and code sections concise and clear. Below is a complete, minimal example of a TensorFlow‑based Python microservice that does real‑time image recognition (image classification) using a pre‑trained model.
It uses:
- TensorFlow 2.x (Keras applications)
- FastAPI for the HTTP API
- Uvicorn as the ASGI server
1. Project structure
image-recognition-service/ ├─ app.py ├─ requirements.txt └─ Dockerfile (optional, for containerization)
2.
app.py– FastAPI microservice# app.py import io from typing import List import numpy as np from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.concurrency import run_in_threadpool from pydantic import BaseModel from PIL import Image import tensorflow as tf from tensorflow.keras.applications.mobilenet_v2 import ( MobileNetV2, preprocess_input, decode_predictions, ) app = FastAPI(title="Real-time Image Recognition Service") model = None input_size = (224, 224) # MobileNetV2 default @app.on_event("startup") def load_model(): """ Load the TensorFlow model into memory once when the service starts. """ global model model = MobileNetV2(weights="imagenet") # Optional: warm-up for lower first-request latency dummy = np.zeros((1, input_size[0], input_size[1], 3), dtype=np.float32) _ = model.predict(dummy) def prepare_image(image: Image.Image) -> np.ndarray: """ Convert a PIL Image to a preprocessed NumPy array suitable for MobileNetV2. """ image = image.convert("RGB") image = image.resize(input_size) arr = np.array(image, dtype=np.float32) arr = np.expand_dims(arr, axis=0) arr = preprocess_input(arr) return arr class Prediction(BaseModel): label: str # e.g. "n02124075" description: str # e.g. "Egyptian cat" probability: float class PredictResponse(BaseModel): predictions: List[Prediction] @app.get("/health") def health(): if model is None: raise HTTPException(status_code=503, detail="Model not loaded") return {"status": "ok"} @app.post("/predict", response_model=PredictResponse) async def predict(file: UploadFile = File(...), top_k: int = 5): """ Accepts an image file and returns top-K ImageNet predictions. """ if model is None: raise HTTPException(status_code=503, detail="Model not loaded") if file.content_type not in ( "image/jpeg", "image/jpg", "image/png", "image/webp", "image/bmp", ): raise HTTPException( status_code=400, detail=f"Unsupported file type: {file.content_type}", ) contents = await file.read() try: image = Image.open(io.BytesIO(contents)) except Exception: raise HTTPException(status_code=400, detail="Could not decode image") input_tensor = prepare_image(image) # Run TensorFlow in a threadpool so it doesn't block the event loop preds = await run_in_threadpool(model.predict, input_tensor) decoded = decode_predictions(preds, top=top_k)[0] response_predictions = [ Prediction( label=class_id, description=class_name, probability=float(prob), ) for (class_id, class_name, prob) in decoded ] return PredictResponse(predictions=response_predictions) if __name__ == "__main__": import uvicorn uvicorn.run( "app:app", host="0.0.0.0", port=8000, reload=True, # turn off in production )
3.
requirements.txtfastapi uvicorn[standard] tensorflow pillow numpyIf you’re on a machine with GPU support, you can use
tensorflow-gpu(or the moderntensorflowthat ships GPU support, depending on your environment).
4. Running locally
-
Create and activate a virtualenv (recommended):
python -m venv venv source venv/bin/activate # Linux/macOS # or venv\Scripts\activate # Windows -
Install dependencies:
pip install -r requirements.txt -
Start the service:
python app.py -
Call the API (example using
curl):curl -X POST "http://localhost:8000/predict?top_k=3" \ -F "file=@/path/to/your/image.jpg"
Example JSON response:
{ "predictions": [ { "label": "n02124075", "description": "Egyptian cat", "probability": 0.8532146811 }, { "label": "n02123045", "description": "tabby", "probability": 0.0678192005 }, { "label": "n02123159", "description": "tiger cat", "probability": 0.0291232671 } ] }You can view interactive docs at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
5. Optional: Dockerfile
# Dockerfile FROM python:3.11-slim WORKDIR /app # Install system deps needed by Pillow/TensorFlow (adjust if needed) RUN apt-get update && apt-get install -y \ build-essential \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 \ && rm -rf /var/lib/apt/lists/* COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY app.py . EXPOSE 8000 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]Build and run:
docker build -t image-recognition-service . docker run --rm -p 8000:8000 image-recognition-service
6. Adapting to your own model
To use a custom TensorFlow model instead of
MobileNetV2:-
Replace
MobileNetV2(...)inload_model()with loading your saved model, for example:model = tf.keras.models.load_model("path/to/your/model") input_size = (height, width) # set to your model's input size -
Replace
preprocess_inputanddecode_predictionswith your own preprocessing and label decoding logic. -
Adjust the
Predictionschema if your labels/outputs differ.
If you tell me what kind of images or domain you’re targeting (e.g., products, medical images, traffic signs), I can tailor the model loading and preprocessing code to that scenario.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.