Artificial Intelligence

Build and Deploy Object Detection using Yolov5, FastAPI, and Docker

Object detection is one of the most popular computer vision tasks which involves identifying and localizing objects in an image or video. Advances in deep neural networks have made it less difficult to achieve accurate and efficient object detection.

Photo by Ian Taylor on Unsplash

YOLOv5 (You only look once version 5) is a SOTA object detection model that is quite popular in the computer vision community due to its speed and detection accuracy. It is part of the group of object detection models developed by Ultralytics and it has different versions: small (s), medium (m), large (l), and extra large (x), with each providing progressively higher detection rates.

In this article, we explore the possibility of using Yolov5 for object detection, wrapping the detection model in web service using FastAPI, and deploying the service using Docker to provide an avenue for smooth integration with other applications.

Getting Started

To kickstart the development, we create a new project named: object-detection-yolov5-fastapi which can be found in this repo. We are using python 3.9 and poetry for dependency management. You can find the installed libraries in the poetry.lock file and the following commands can be used to install them in your project:

poetry shell
poetry install

Detection Model

In this project, we are not building (training) a custom object detection model, hence, the default or already trained Yolov5 from Ultralytics is used. As a result, we implement a class that allows users to pass an input image, and the detection results which include the bounding boxes, confidence score, and object class are returned, as shown below:

NB: You can add any other image pre-processing techniques such as denoising, normalization in the function image_preprocess

In order to properly format the outputs from the model, we made use of this helper function:

def results_to_json(results, model):
""" Converts yolo model output to json (list of dicts)
This code is from: https://github.com/WelkinU/yolov5-fastapi-demo/blob/main/server.py

"""
return [
[
{
"class": int(pred[5]),
"class_name": model.model.names[int(pred[5])],
"bbox": [int(x) for x in pred[:4].tolist()], # convert bbox results to int from float
"confidence": float(pred[4]),
}
for pred in result
]
for result in results.xyxy
]

Serving the model with FastAPI

Now that we have the model in place, to make it useable with other applications, for instance in a microservice architecture, we need to use a web framework and there are many python-based frameworks such as Flask, Django, FastAPI, Bottle and etc. However, we prefer FastAPI because it is fast (asynchronous), easy to implement, and naturally supports Pydantic. Here is the API that exposes the model:

import os
import logging
from io import BytesIO
from typing import List
from warnings import filterwarnings, simplefilter
import ssl

import torch
from fastapi import FastAPI, Request, File, UploadFile
from fastapi.responses import JSONResponse
from PIL import Image

from core.base_detector import ObjectDetector

filterwarnings("ignore")
simplefilter(action='ignore', category=FutureWarning)

try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context

if not os.path.exists('logs'):
os.mkdir('logs')

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logging.StreamHandler()
file_handler = logging.FileHandler('logs/api.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(name)s : %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

app = FastAPI()

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')


@app.post("/object_detect")
async def image_detect(request: Request,
input_file: UploadFile = File(...)):

if request.method == "POST":
json_result = []
try:

image = Image.open(BytesIO(await input_file.read()))

ob = ObjectDetector(image, model)
json_results = ob.object_detect()

logger.info("detection results", json_result)

return JSONResponse({"data": json_results,
"message": "object detected successfully",
"errors": None},
status_code=200)
except Exception as error:
logger.error(["process failed", error])
return JSONResponse({"message": "object detection failed",
"errors": "error"},
status_code=400)

The API mainly accepts a POST request method and an image path as the source of the input image, which is then passed to the ObjectDetector class. The response is a JSON consisting of the detected object details and the response status.

Deploying with Docker

Last but not least, we use deployed the detection service using docker, which is a popular tool for containerizing applications and simplifying deployment. We implement the Dockerfileand docker-compose.yml as follows:

FROM python:3.9-slim
ENV PYTHONPATH=/usr/lib/python3.9/site-packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends apt-utils libgl1 libglib2.0-0 \
python3-pip \
&& apt-get install psmisc \
&& apt-get clean \
&& apt-get autoremove
RUN mkdir /object-detection-yolov5
WORKDIR /object-detection-yolov5
COPY . /object-detection-yolov5
RUN pip3 install --upgrade pip
RUN pip install poetry
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi --no-root
RUN pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org python-multipart
EXPOSE 8000
CMD ["python", "app.py"]
version: "3"
services:
object-detector:
build:
context: .
dockerfile: dockerfile
container_name: yolov5-detector
ports:
- "8000:8000"
image: yolov:detector
volumes:
- ./:/object-detection-yolov5

Example Request:

Here is a sample of a curl request to the server:

curl --location '127.0.0.1:8000/object_detect' \
--form 'input_file=@"/Users/Downloads/download.jpeg"'

An example valid response will be:

{
"data": [
[
{
"class": 0,
"class_name": "person",
"bbox": [
134,
330,
187,
579
],
"confidence": 0.7723177671432495
},
{
"class": 0,
"class_name": "person",
"bbox": [
90,
373,
140,
587
],
"confidence": 0.7619432210922241
},
{
"class": 2,
"class_name": "car",
"bbox": [
492,
314,
566,
430
],
"confidence": 0.7516090273857117
},
{
"class": 2,
"class_name": "car",
"bbox": [
379,
321,
455,
460
],
"confidence": 0.7398971915245056
},
{
"class": 0,
"class_name": "person",
"bbox": [
267,
332,
315,
541
],
"confidence": 0.4890599846839905
},
{
"class": 0,
"class_name": "person",
"bbox": [
592,
322,
611,
389
],
"confidence": 0.45573562383651733
},
{
"class": 2,
"class_name": "car",
"bbox": [
444,
313,
477,
400
],
"confidence": 0.38912448287010193
},
{
"class": 0,
"class_name": "person",
"bbox": [
209,
324,
260,
553
],
"confidence": 0.30747783184051514
}
]
],
"message": "object detected successfully",
"errors": null,
"status": 200
}

And an example error response is:

{
"message": "object detection failed",
"errors": "error",
"status": 400
}

YOLOv5 is a SOTA object detection algorithm known for its speed and accuracy. FastAPI is a Python web framework that helps in quickly creating and serving APIs. Docker is a tool that simplifies the process of containerizing applications for easy deployment. This article takes the reader through the process of building and deploying an object detection system using YOLOv5, FastAPI, and Docker.


Build and Deploy Object Detection using Yolov5, FastAPI, and Docker was originally published in Artificial Intelligence in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.

https://ai.plainenglish.io/build-and-deploy-object-detection-using-yolov5-fastapi-and-docker-6465ca1b2583?source=rss—-78d064101951—4
By: Olasimbo Arigbabu, PhD
Title: Build and Deploy Object Detection using Yolov5, FastAPI, and Docker
Sourced From: ai.plainenglish.io/build-and-deploy-object-detection-using-yolov5-fastapi-and-docker-6465ca1b2583?source=rss—-78d064101951—4
Published Date: Sun, 11 Jun 2023 13:36:17 GMT

Did you miss our previous article…
https://e-bookreadercomparison.com/master-the-art-of-generative-ai-with-googles-free-courses/

Leave a Reply

Your email address will not be published. Required fields are marked *