DEV Community

Cover image for How to create YOLOv8-based object detection web service using Python, Julia, Node.js, JavaScript, Go and Rust

How to create YOLOv8-based object detection web service using Python, Julia, Node.js, JavaScript, Go and Rust

Andrey Germanov on May 13, 2023

Table of contents Introduction YOLOv8 deployment options Export YOLOv8 model to ONNX Explore object detection on image using ONNX     Pr...
Collapse
 
muis profile image
Muis Muhtadi • Edited

Amazing tutorial!.. especially I am interested in the Julia implementation.
I just found a little typo in the command to run web service using Julia which might confuse some newbie like me.
"juila src/object_detector.py" --> "julia src/object_detector.jl"

Collapse
 
andreygermanov profile image
Andrey Germanov

Fixed this. Thanks for pointing!

Collapse
 
muis profile image
Muis Muhtadi

Most welcome, but one more typo remains on the command i.e the extension of the object_detector should be .jl not .py.

This article and another article on Deep Learning with Julia are indispensable references for my study.

Collapse
 
moscamich profile image
Michele Moscaritolo

Impressive and outstanding! You wrote a wonderful, unique article. I'm sure your expertise will help many developers like it did help me.

minor suggestion add the support to the GPU, for example in Julia:
load_inference("yolov8m.onnx", execution_provider=:cuda)

And then you have what I believe is the Bible

Bravo! Standing ovation

Collapse
 
pcismyname profile image
Chidsanuphong Pengchai

This is what I'm looking for, tysm!

Collapse
 
aajadp profile image
aajad-p

Great explanation.. thank you sir,
Can you brief about SAM(segment anything model) in ONNX format and can use in any language.

Collapse
 
andreygermanov profile image
Andrey Germanov • Edited

Yes, in the next article. So, subscribe and stay tuned ;)

Collapse
 
yubolong profile image
YuboLong

I'm currently working on deploying yolov8 to java , your post-process code really helps me lot , thanks !

Collapse
 
camerayuhang profile image
camerayuhang

I have never, ever, ever seen such an incredibly amazing tutorial like this. This article is truly unique and wonderful.

Collapse
 
anto5040 profile image
Antonio

Thank you Andrey for this post. It has been really enlightening and helpful in my task. Keep up with the good work!

Collapse
 
zain18jan2000 profile image
Muhammad Zain Ul Haque

I just login here to say this is the best article with too much detail I have ever read in my life on computer vision. Respect for the author.

Collapse
 
andreygermanov profile image
Andrey Germanov

Thanks a lot!

Collapse
 
wenbindu profile image
dean.du

Great work, thank you so much. You have created an amazing article.

Collapse
 
fatmaboodai profile image
fatmaboodai

Hello I tried to use this tutorial but with the classifying model but i'm getting this error :
ERROR:object-detection:Exception on /detect [POST]
Traceback (most recent call last):
File "C:\Users\mega\Desktop\YoloPlayGround\YoloPlayGround\lib\site-packages\flask\app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\mega\Desktop\YoloPlayGround\YoloPlayGround\lib\site-packages\flask\app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\mega\Desktop\YoloPlayGround\YoloPlayGround\lib\site-packages\flask\app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\mega\Desktop\YoloPlayGround\YoloPlayGround\lib\site-packages\flask\app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\Users\mega\Desktop\YoloPlayGround\object-detection.py", line 24, in detect

boxes = detect_objects_on_image(buf.stream)
File "C:\Users\mega\Desktop\YoloPlayGround\object-detection.py", line 29, in detect_objects_on_image
output = run_model(input)
File "C:\Users\mega\Desktop\YoloPlayGround\object-detection.py", line 53, in run_model

outputs = model.run(["output0"], {"images":input})
File "C:\Users\mega\Desktop\YoloPlayGround\YoloPlayGround\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 220, in run
return self._sess.run(output_names, input_feed, run_options)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: images for the following indices
index: 2 Got: 640 Expected: 224
index: 3 Got: 640 Expected: 224

Collapse
 
andreygermanov profile image
Andrey Germanov • Edited

However, if you are interested in how to parse the YOLOv8 classification model output, then I can help you.

Here is the code, that you need to rewrite and use for this:

def detect_objects_on_image(buf):
    input = prepare_input(buf)
    output = run_model(input)
    return process_output(output)

def prepare_input(buf):
    img = Image.open(buf)
    img = img.resize((224, 224))
    img = img.convert("RGB")
    input = np.array(img)
    input = input.transpose(2, 0, 1)
    input = input.reshape(1, 3, 224, 224) / 255.0
    return input.astype(np.float32)

def run_model(input):
    model = ort.InferenceSession("yolov8m-cls.onnx", providers=['CPUExecutionProvider'])
    outputs = model.run(["output0"], {"images":input})
    return outputs[0]

def process_output(output):
    class_id = output[0].argmax()
    return imagenet_classes[class_id] # returns only a label of detected class

imagenet_classes = {}  # get ImageNet class labels from here: https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a
Enter fullscreen mode Exit fullscreen mode

But you can use this only in other applications, because the web application in this tutorial requires bounding boxes of detected objects, which are not available in classifying model's output.

Collapse
 
fatmaboodai profile image
fatmaboodai

Thank you so much i really appreciate it

Collapse
 
andreygermanov profile image
Andrey Germanov • Edited

Hello,

Unfortunately, this tutorial was written for object detection and can't be used for YOLOv8 classifying model, because this model is very different:

  1. It requires an image resized to 224x224 (this is the error, that you've got)
  2. It trained not on COCO dataset with 80 classes, but on ImageNet dataset with 1000 classes (80 class labels, provided in the tutorial, do not match 1000 class labels of ImageNet).
  3. It returns an output of (1, 1000) shape, that contains probabilities of each of 1000 ImageNet classes and does not contain any bounding boxes (output processing function should be different).

Moreover, the web application, that you create by following this tutorial used to draw bounding boxes of detected objects on the image, but YOLOv8 classifying model does not return bounding boxes, it returns only class ids for the whole image. That is why, it will not work for this application.

So, sorry, but it's wrong tutorial for this task.

Collapse
 
fatmaboodai profile image
fatmaboodai

Thank you so much

Collapse
 
pcismyname profile image
Chidsanuphong Pengchai

I have a question in pre-processing image, Is padding neccessary or important in this step

Collapse
 
andreygermanov profile image
Andrey Germanov

When I tried padding with YOLOv8, I didn't see a difference in quality of results.

Collapse
 
pcismyname profile image
Chidsanuphong Pengchai • Edited

thank you.

Collapse
 
devanshsahni profile image
DevanshSahni

Thankyou so much for such a great explanation!

Collapse
 
fatmaboodai profile image
fatmaboodai

Hello,
I was trying to push my project to github after following your tutorial and it’s says that the folder are too big
How were you are to push your project?

Collapse
 
andreygermanov profile image
Andrey Germanov

Perhaps the model file is too big for GitHub. I've experienced this sometimes. In this case, you need to push without model file.