Create A Web App For An Object Detection Using Python And YOLOv8

Hello Learners…

Welcome to my blog…

Table Of Content:

  • Introduction
  • What is object detection?
  • What is YOLO?
  • What is YOLOv8
  • Create an object detection web app using python and yolov8
  • Summary
  • References

Introduction:

In this post, We create a web application for object detection on images using python streamlit and a state-of-the-art object detection model YOLOv8. using this we can perform some basic object detection tasks. also, we can train our own object detection model on our data.

Here I saw how we can use the pre-trained yolov8 model and create a simple web app so everyone can use it through the web app.

What is object detection?

Object detection is a part of computer vision in which we detect some objects or we can say things like cat, dog, kite, person, banana cell phone, etc, in a given image or video.

Object detection creates a rectangle or square on objects in the given image or video.

Here are examples of object detection,

generated by the author
Object Detection Using Python And YOLOv8
generated by the author

What is YOLO?

YOLO(You Only Look Once) is an algorithm for real-time object detection on images, videos, and webcams, it uses the neural network for object detection. It is very popular in terms of speed and accuracy.

what is YOLOv8?

yoloV8 is known as  Ultralytics YOLOv8  because it is developed by  Ultralytics.

YOLOv8 is the latest version of YOLO with better speed and accuracy. It can be trained on large datasets.

YOLOv8 is used to detect, segment, and classify objects.

YOLOv8 can detect below objects :

[‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘airplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘backpack’, ‘umbrella’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘apple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘dining table’, ‘toilet’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’]

Create an object detection web app using Python and YOLOv8

To create a web app using python and YOLOv8, first install the required libraries.

We install streamlit for the web app and ultralytics for object detection using the pip command:

pip install streamlit
pip install ultralytics

After installation of both libraries writes code for the web app.

Here we create an object detection for images only.

Create a streamlit_app.py file and paste the below code into this file.

Code:

import streamlit as st
from ultralytics import YOLO
import os

st.title("Object Detection Using YOLOv8")

def main():
    try:
        img_upload = st.file_uploader("Choose a iamge file",type=["jpg","jpeg","png"])
        if st.button("Process"):

            if img_upload is not None:
                st.write("File Uploaded")
                
                col1, col2 = st.columns(2)

                with col1:
                    st.header("Original Image")
                    st.image(img_upload)

                with open(img_upload.name, 'wb') as f: 
                    f.write(img_upload.getvalue())
                
            
        
                model = YOLO('yolov8x.pt')
                results = model(img_upload.name, save=True)
             
                predict_len=len(os.listdir(os.path.join("runs","detect")))
                print(predict_len)

                if predict_len==1:
                    with col2:
                        st.header("Detected Image")
                        st.image(os.path.join("runs","detect","predict",img_upload.name))

                else:

                    with col2:
                        st.header("Detected Image")
                        st.image(os.path.join("runs","detect","predict"+str(predict_len),img_upload.name))
                st.write("Detected Image path:>> ",os.path.join("runs","detect","predict"+str(predict_len),img_upload.name))
           
            else:
                st.error({"error":"please select your image "})
    except Exception as e:
        st.error({"error":str(e)})

main()

Now run the file

streamlit run streamlit_app.py

We get an URL: http://localhost:8502/

It will open automatically in our default browser. if not opened automatically then we open this URL in the browser.

Now we can see the below interface

screenshot by the author

Here we accept jpg, jpeg, and png formats.

Now select your image and wait for the results..

Object Detection Using Python And YOLOv8
screenshot by the author

Summary:

This is a simple demo of object detection using YOLOv8.

References:

1 thought on “Create A Web App For An Object Detection Using Python And YOLOv8”

Leave a Comment