Machine Vision Practical Toolset NO.18—Using YOLO8 to detect objects in real time, the performance is bursting

foreword

After installing the pytorch framework, you can play some open source deep learning frameworks, such as YOLO8, which is based on the pytorch framework. For how to install the pytorch framework, you can refer to the previous article link "pytorch deep learning framework CUDA version environment installation record "
After some transformation, I used YOLO8 to make a real-time online video object detection program. The results show that yolo8 has very good real-time performance and can be used in real-time scenes such as robots.
insert image description here

Install YOLO8

There are a lot of them on the Internet, and the installation is very simple. After installing the pytorch framework, you can use it directly: the
pip install ultralyticscommand can be installed.
See the author’s github address for details. I ignored it, it doesn't seem to have much effect on the operation

Install the model of YOLO8

It can be downloaded directly from the Internet, there are many channels, and
the link to the model download address . In this example, yolov8n.pt is used to download the model to a local folder.
insert image description here

program source code

I have done a yolo3 tool code before, which is basically the same. It uses a python UI framework, PySimpleGUI, which is very easy to use. For the introduction of this UI, you can refer to the blog post "Python Robot Vision Programming - Getting Started (Part 2) " . Then there is the opencv library. All the code of the program is as follows:

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 27 10:11:05 2023

@author: JAMES FEI <https://blog.csdn.net/kanbide>
Copyright (C) 2021 FEI PANFENG, All rights reserved.
THIS SOFTEWARE, INCLUDING DOCUMENTATION,IS PROTECTED BY COPYRIGHT CONTROLLED 
BY FEI PANFENG ALL RIGHTS ARE RESERVED.
"""

from ultralytics import YOLO
import cv2
import time
import PySimpleGUI as sg
import numpy as np

# Load a model
model = YOLO("yolov8n.pt")  # load an official model

#cap=cv2.imread("test.png")
cap=cv2.VideoCapture(0)
# Predict with the model and initail
results = model("test.png")  # predict on an image
#标签
lables=results[0].names
def resizeoutput(output,maxw=600,maxh=500):
    H=output.shape[0]    
    W=output.shape[1]
    ratio=None
    if W>=H:
        if W>maxw:
            gsizew=maxw
            gsizeh=int(H/W*maxw)
            ratio=maxw/W
        else:
            gsizew=W
            gsizeh=H
            ratio=1
    else:
        if H>maxh:
            gsizeh=maxh
            gsizew=int(W/H*maxh)
            ratio=maxh/H
        else:
            gsizew=W
            gsizeh=H
            ratio=1
    pic = cv2.resize(output, (gsizew, gsizeh), interpolation=cv2.INTER_LINEAR)
    return pic,ratio 

def getboxs(yoloresult,lables=lables):
    #获取识别框信息
    BOXS=[]
    outputs=yoloresult[0].boxes    
    class_IDs=outputs.cls.tolist()
    layables=[]
    for i in class_IDs:
        layables.append(lables[int(i)])
        
    confidences=outputs.conf.tolist()
    boxes=outputs.xyxy.tolist()
    if len(boxes):
        BOXS=[boxes,layables,confidences]    
    
    return BOXS
def drawbox(img,box,filte=0.5):    
    if type(box)==type([]):    
        if len(box)>0:
            for i in range(len(box[0])):        
                x,y,x1,y1=box[0][i]
                x=int(x)
                y=int(y)
                x1=int(x1)
                y1=int(y1)
                name=box[1][i]
                confi=box[2][i]
                if confi>=filte:
                    text = "{}: {:.4f}".format(name, confi)
                    cv2.putText(img, text, (x, y - 5), cv2.FONT_ITALIC, 0.5, [0, 255, 0], 2)
                    cv2.rectangle(img, (x, y), (x1, y1), (255,255,0), 2)

def video_viewer(cap,model=model):
    """
    视频显示器
    """
    layout= [   [sg.Text(size=(15,1),  key='-OUTPUT-')],
                [sg.Image(filename='', key='-IMAGE-')],
                [sg.Button('Exit')]
                ]
    win = sg.Window('YOLO视频检测', layout)
         
    while True:
        event, values = win.read(timeout=100)
        ret, frame = cap.read()
        if ret:
            frame,ra=resizeoutput(frame,maxh=400)
            results = model(frame) 
            boxs=getboxs(results)         
            drawbox(frame,boxs)
        imgbytes = cv2.imencode('.png', frame)[1].tobytes()
        win['-IMAGE-'].update(data=imgbytes)
        #win['-OUTPUT-'].update("video window:"+str(task0.is_alive()))
        if event is None or event == 'Exit':
            win.active  = False
            win.close()
            del win
            cap.release()
            break  
video_viewer(cap)

insert image description here

Summarize

You can use YOLO8 to develop some fun things in the future, please look forward to it! focus on!

Guess you like

Origin blog.csdn.net/kanbide/article/details/130403405