Implementing a REST endpoint to perform object detection on a single image
Before working with WebSockets, we’ll start simple and implement, using FastAPI, a classic HTTP endpoint to accept image uploads and perform object detection on them. As you’ll see, the main difference from the previous example is in how we acquire the image: instead of reading it from the disk, we get it from a file upload that we have to convert into a Pillow image object.
Besides, we’ll also use the exact same pattern we saw in Chapter 12, Creating an Efficient Prediction API Endpoint with FastAPI – that is, having a dedicated class for our prediction model, which will be loaded during the lifespan handler.
The first thing we do in this implementation is to define Pydantic models in order to properly structure the output of our prediction model. You can see this as follows:
chapter13_api.py
class Object(BaseModel): box: tuple[float, float, float...