chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

怎樣使用YOLOv8構(gòu)建目標計數(shù)GUI呢?

新機器視覺 ? 來源:新機器視覺 ? 2023-11-30 11:03 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

標檢測是在圖片或視頻中定位物體的過程。其中一個著名的目標檢測框架是YOLO(You Only Look Once)。在某些情況下,我們不僅需要定位圖片或視頻中的物體,還需要了解每個物體的運動,就像在這篇文章中,我們想要計數(shù)通過某些位置的物體。這就是我們需要不僅僅是檢測的地方,我們需要稱為目標跟蹤的東西的地方。

此外,大多數(shù)情況下,我們通常只是通過執(zhí)行腳本或命令行中的代碼來使用它,如果我們可以將我們的模型部署為GUI或應(yīng)用程序,以便用戶更輕松地使用它,那將更好。在這篇文章中,將分享如何使用框架YOLOv8和PySimpleGUI構(gòu)建一個目標計數(shù)GUI。

什么是YOLOv8?

YOLOv8,即You Only Look Once version 8,是由UItralytics開發(fā)的圖像處理框架,YOLOv8可以執(zhí)行目標檢測和跟蹤、實例分割、圖像分類和姿態(tài)估計任務(wù)。

5a8106ce-8ebc-11ee-939d-92fbcf53809c.png

YOLOv8的用法

5a922490-8ebc-11ee-939d-92fbcf53809c.png

YOLOv8與先前版本的性能比較

使用YOLOv8進行目標跟蹤

有很多方法可以使用YOLOv8進行目標跟蹤。我喜歡Python腳本方法,因為我可以有更多的控制權(quán),使用此方法需要執(zhí)行以下幾個步驟,您還可以從Ultralytics的文檔中查看此鏈接。

1. 創(chuàng)建虛擬環(huán)境

python -m venv yologui

2. 激活虛擬環(huán)境

yologuiScriptsactivate

3. 安裝YOLOv8的依賴項

pip install -r https://raw.githubusercontent.com/ultralytics/ultralytics/main/requirements.txt

4. 安裝YOLOv8

pip install ultralytics

5. 創(chuàng)建一個yolo腳本

# https://docs.ultralytics.com/modes/track/#python-examples
import cv2
from ultralytics import YOLO


# Load the YOLOv8 model
model = YOLO('yolov8n.pt')


# Open the camera
cap = cv2.VideoCapture(0)


# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()


    if success:
        # Run YOLOv8 tracking on the frame, persisting tracks between frames
        results = model.track(frame, persist=True)
        # Visualize the results on the frame
        annotated_frame = results[0].plot()
        # Display the annotated frame
        cv2.imshow("YOLOv8 Tracking", annotated_frame)
        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break


# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

執(zhí)行腳本,您應(yīng)該可以得到由YOLOv8進行的目標跟蹤:

python yolo.py

這是結(jié)果,當滿意時按“Q”退出。

5a9e3ed8-8ebc-11ee-939d-92fbcf53809c.png

目標追蹤結(jié)果

您可以看到我們的腳本可以記住左側(cè)圖片中的泰迪熊與右側(cè)圖片中的泰迪熊是相同的。實際上,他是一只狗....

添加目標跟蹤的計數(shù)邏輯

目標跟蹤為我們提供了比目標檢測更多的選項,其中之一是我們可以使用它來計算進入或離開框架中某個區(qū)域或線的獨特目標的數(shù)量。

5aa37cfe-8ebc-11ee-939d-92fbcf53809c.jpg

跟蹤物體A和B的運動

要計算通過我們的線左右經(jīng)過的物體數(shù)量,我們需要從我們的目標跟蹤腳本中提取目標的id和位置。Ultralytics的模型已經(jīng)為我們提供了該屬性,通過使用以下命令來處理跟蹤結(jié)果。

# Using Model
results= model.track(frame, persist=True)


# Extrack result
result = results[0].cpu().boxes
detect_id = result.id.tolist() if result.id != None else []
detect_xyxy = result.xyxy.tolist() if result.xyxy != None else []
frame_counting_buffer = dict(zip(detect_id, detect_xyxy))

因此,我們可以獲取每個目標的位置,接下來是進行一些計算,以了解目標是從右到左還是從左到右經(jīng)過。在這種情況下,我只關(guān)注目標的中心和X軸。以下是在X軸上進行目標計數(shù)的完整腳本。

# https://docs.ultralytics.com/modes/track/#python-examples
import cv2
from ultralytics import YOLO


# Load the YOLOv8 model
model = YOLO('yolov8n.pt')


# Counting config
line_position = 50
text_size = 30
text_x_position = 50
text_y_position = 0


# Open the camera
cap = cv2.VideoCapture(0)


# Get Camera Parameter
width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)


# Counting prep
x_line= line_position*width/100
pt1 = (int(x_line), 0)
pt2 = (int(x_line), int(height))
counting_buffer = {}
counting_result = {'left-to-right' : 0, 'right-to-left' : 0}


# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()
    if success:
        # Run YOLOv8 tracking on the frame, persisting tracks between frames
        results = model.track(frame, persist=True, verbose=False)


        # Visualize the results on the frame
        annotated_frame = results[0].plot()


        # Get Data for counting
        result = results[0].cpu().boxes
        detect_id = result.id.tolist() if result.id != None else []
        detect_xyxy = result.xyxy.tolist() if result.xyxy != None else []
        frame_counting_buffer = dict(zip(detect_id, detect_xyxy))
        # Process
        for i in frame_counting_buffer :
            # Prep count buffer
            counting_buffer[i] = counting_buffer.get(i,[])
            if len(counting_buffer[i]) >= 2 : counting_buffer[i] = counting_buffer[i][-1:]
            # Append avg x axis to buffer
            avg_x = (frame_counting_buffer[i][0] + frame_counting_buffer[i][2])/2
            counting_buffer[i].append(avg_x)
            # Count logic
            if len(counting_buffer[i]) >= 2 : 
                if (counting_buffer[i][0] > x_line) & (counting_buffer[i][1] < x_line) :
                    counting_result['right-to-left'] += 1
                elif (counting_buffer[i][0] < x_line) & (counting_buffer[i][1] > x_line) :
                    counting_result['left-to-right'] += 1


        # Create Line
        cv2.line(annotated_frame, pt1= pt1, pt2= pt2 , color= (0,0,255), thickness= 2) 
        # Put Counting to picture
        text_position = text_y_position
        for i in counting_result : 
            text_position += text_size
            info_text = f"{i} : {counting_result[i]}"
            annotated_frame = cv2.putText(annotated_frame
                                          , info_text
                                          , (int(width*text_x_position/100), text_position)
                                          , cv2.FONT_HERSHEY_SIMPLEX
                                          , 1, (0,0,255), 1, cv2.LINE_AA)


        # Display the annotated frame
        cv2.imshow("YOLOv8 Tracking", annotated_frame)


        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break


# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

然后,我們將得到一個如圖所示的目標計數(shù)腳本,當滿意時按Q。

5aa84892-8ebc-11ee-939d-92fbcf53809c.jpg

什么是PySimpleGUI?

PySimpleGUI是一個用于制作GUI的Python庫,具有跨平臺性且非常簡單易用。

5ab38662-8ebc-11ee-939d-92fbcf53809c.jpg

從PySimpleGUI的頁面獲取圖片

GUI的框架

創(chuàng)建GUI時,有一些比編碼更重要的東西,即設(shè)計階段。我們在編碼之前應(yīng)該有一些草圖設(shè)計。在這種情況下,我畫了一個目標計數(shù)GUI的初步設(shè)計,就像這張圖片一樣。

5abf1aae-8ebc-11ee-939d-92fbcf53809c.jpg

GUI初步設(shè)計

設(shè)計階段后,接下來就是編碼:

1. 安裝PySimpleGUI。

pip install pysimplegui

2. 創(chuàng)建一個GUI腳本

# https://www.pysimplegui.org/en/latest/#jump-start
import PySimpleGUI as sg


# Create Layouy of the GUI
layout = [  
            [sg.Text('GUI Object Counting with Yolo V8')],
            [sg.Text('Enter Model Name', size= (15)), sg.InputText(key='model_name')],
            [sg.Text('X-line for Counting', size= (15)), sg.InputText(key= 'line_position')],
            [sg.Button('Run'), sg.Button('Stop'), sg.Button('Close')],
            [sg.Text('PICTURE')],
            [sg.Text('Left-to-Right', size= (15), key='out1'), sg.Text("0", key='out1-v')],
            [sg.Text('Right-to-Left', size= (15), key='out2'), sg.Text("0", key='out2-v')]
            ]


# Create the Window
window = sg.Window('GUIYoloV8-Counting', layout)
# Event Loop to process "events"
while True:
    event, values = window.read()
    # When press Run
    if event == 'Run' : 
        print(values)
    # When close window or press Close
    if event in (sg.WIN_CLOSED, 'Close'): break


# Close window
window.close()

正如您所見,PySimpleGUI的腳本非常簡單。之后,我們所要做的就是運行此GUI腳本。

python gui.py

之后,您應(yīng)該會得到一個像圖中這樣的簡單GUI,嘗試玩耍并在滿意時關(guān)閉。

5acd02f4-8ebc-11ee-939d-92fbcf53809c.jpg

合并

在我們有了目標計數(shù)和GUI框架之后,是時候?qū)⑺鼈兒喜⒌揭黄鹆?。我們可以將兩個腳本合并成一個名為main.py的新腳本,如下所示:

# https://www.pysimplegui.org/en/latest/#jump-start
import PySimpleGUI as sg
import cv2
# https://docs.ultralytics.com/modes/track/#python-examples
from ultralytics import YOLO


# Create Layouy of the GUI
layout = [  
            [sg.Text('GUI Object Counting with Yolo V8')],
            [sg.Text('Enter Model Name', size= (15)), sg.InputText(key='model_name')],
            [sg.Text('X-line for Counting', size= (15)), sg.InputText(key= 'line_position')],
            [sg.Button('Run'), sg.Button('Stop'), sg.Button('Close')],
            [sg.Image(filename='', key='image')],
            [sg.Text('Right-to-Left', size= (15), key='out1'), sg.Text("0", key='right-to-left')],
            [sg.Text('Left-to-Right', size= (15), key='out2'), sg.Text("0", key='left-to-right')]
            ]


# Create the Window
window = sg.Window('GUIYoloV8-Counting', layout)
run_model, verbose = False, False
# Event Loop to process "events"
while True:
    event, values = window.read(timeout=0)
    # When press Run
    if event == 'Run' : 
        # Load the YOLOv8 model
        model = YOLO(values['model_name'])
        # Counting config
        line_position = 50
        text_size = 30
        text_x_position = 50
        text_y_position = 0
        # Open the camera
        cap = cv2.VideoCapture(0)
        # Get Camera Parameter
        width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        # Counting prep
        x_line= line_position*width/100
        pt1 = (int(x_line), 0)
        pt2 = (int(x_line), int(height))
        counting_buffer = {}
        counting_result = {'left-to-right' : 0, 'right-to-left' : 0}
        # Run Signal
        run_model = True
    # When close window or press Close
    elif event in ('Stop', sg.WIN_CLOSED, 'Close'):
        if run_model : 
            run_model = False # Stop running
            cap.release() # Release video
            if event != sg.WIN_CLOSED : window['image'].update(filename='') # Destroy picture
        # When close window or press Close
        if event in (sg.WIN_CLOSED, 'Close'): break
    # Run Model
    if run_model : 
        # Read a frame from the video
        success, frame = cap.read()
        if success:
            # Run YOLOv8 tracking on the frame, persisting tracks between frames
            results = model.track(frame
                                  , persist=True
                                  , verbose=False
                                  )


            # Visualize the results on the frame
            annotated_frame = results[0].plot()


            # Get Data for counting
            result = results[0].cpu().boxes
            detect_id = result.id.tolist() if result.id != None else []
            detect_xyxy = result.xyxy.tolist() if result.xyxy != None else []
            frame_counting_buffer = dict(zip(detect_id, detect_xyxy))
            # Process
            for i in frame_counting_buffer :
                # Prep count buffer
                counting_buffer[i] = counting_buffer.get(i,[])
                if len(counting_buffer[i]) >= 2 : counting_buffer[i] = counting_buffer[i][-1:]
                # Append avg x axis to buffer
                avg_x = (frame_counting_buffer[i][0] + frame_counting_buffer[i][2])/2
                counting_buffer[i].append(avg_x)
                # Count logic
                if len(counting_buffer[i]) >= 2 : 
                    if (counting_buffer[i][0] > x_line) & (counting_buffer[i][1] < x_line) :
                        counting_result['right-to-left'] += 1
                    elif (counting_buffer[i][0] < x_line) & (counting_buffer[i][1] > x_line) :
                        counting_result['left-to-right'] += 1
            # Create Line
            cv2.line(annotated_frame, pt1= pt1, pt2= pt2 , color= (0,0,255), thickness= 2) 
            # Put Counting to picture
            text_position = text_y_position
            for i in counting_result : 
                text_position += text_size
                info_text = f"{i} : {counting_result[i]}"
                annotated_frame = cv2.putText(  annotated_frame
                                            , info_text
                                            , (int(width*text_x_position/100)
                                            , text_position)
                                            , cv2.FONT_HERSHEY_SIMPLEX
                                            , 1
                                            , (0,0,255)
                                            , 1
                                            , cv2.LINE_AA)
            # Show Image
            imgbytes = cv2.imencode('.png', annotated_frame)[1].tobytes()
            window['image'].update(data=imgbytes)
            window['right-to-left'].update(str(counting_result['right-to-left']))
            window['left-to-right'].update(str(counting_result['left-to-right']))
        else: 
            # Break the loop if not read
            cap.release()
            run_model = False
        
# Close window
window.close()

然后,我們可以運行主腳本。

python main.py

然后,我們就可以得到以下圖片所示的目標計數(shù)GUI。這一次,我將使用一個定制模型來識別我從Ultralytics HUB創(chuàng)建的Pompompurin。

5ae3aeaa-8ebc-11ee-939d-92fbcf53809c.jpg

初始階段

5af6f8de-8ebc-11ee-939d-92fbcf53809c.jpg

按下“RUN”后,我的新模型知道它現(xiàn)在是一只狗... 接下來,為了讓我們的用戶更輕松地使用,我們可以創(chuàng)建一個包含以下代碼的“cmd”文件。

python main.py
pause

現(xiàn)在,用戶可以通過打開cmd文件來使用我們的GUI/應(yīng)用程序。






審核編輯:劉清

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • GUI
    GUI
    +關(guān)注

    關(guān)注

    3

    文章

    679

    瀏覽量

    41245
  • python
    +關(guān)注

    關(guān)注

    56

    文章

    4827

    瀏覽量

    86775

原文標題:使用YOLOv8構(gòu)建目標計數(shù)GUI

文章出處:【微信號:vision263com,微信公眾號:新機器視覺】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    labview調(diào)用yolov8/11目標檢測、分割、分類

    labview使用2020版本64位編輯,調(diào)用yolov8/11的onnx模型案例。 源碼: 通過網(wǎng)盤分享的文件:Labview_cls.zip等4個文件 鏈接: https
    發(fā)表于 04-21 19:37

    YOLOv8水果檢測示例代碼換成640輸入圖像出現(xiàn)目標框繪制錯誤的原因 ?

    官網(wǎng)中的YOLOv8 水果檢測關(guān)于圖片推理的示例源代碼: from libs.YOLO import YOLOv8 import os,sys,gc import ulab.numpy as np
    發(fā)表于 06-18 06:37

    yolov8怎么在wsl中搭建?

    純小白,yolov8怎么在wsl中搭建?一直報錯且無法安裝pip包
    發(fā)表于 07-11 07:37

    使用YOLOv8目標檢測和實例分割的演示

    YOLOv8是來自Ultralytics的最新的基于YOLO的對象檢測模型系列,提供最先進的性能。
    的頭像 發(fā)表于 02-06 10:11 ?8428次閱讀

    YOLOv8自定義數(shù)據(jù)集訓(xùn)練到模型部署推理簡析

    如果你只是想使用而不是開發(fā),強烈推薦通過pip安裝方式獲取YOLOv8包!YOLOv8安裝命令行
    的頭像 發(fā)表于 03-24 09:27 ?5147次閱讀

    在AI愛克斯開發(fā)板上用OpenVINO?加速YOLOv8目標檢測模型

    《在 AI 愛克斯開發(fā)板上用 OpenVINO 加速 YOLOv8 分類模型》介紹了在 AI 愛克斯開發(fā)板上使用 OpenVINO 開發(fā)套件部署并測評 YOLOv8 的分類模型,本文將介紹在 AI 愛克斯開發(fā)板上使用 OpenVINO 加速
    的頭像 發(fā)表于 05-12 09:08 ?1803次閱讀
    在AI愛克斯開發(fā)板上用OpenVINO?加速<b class='flag-5'>YOLOv8</b><b class='flag-5'>目標</b>檢測模型

    YOLOv8版本升級支持小目標檢測與高分辨率圖像輸入

    YOLOv8版本最近版本又更新了,除了支持姿態(tài)評估以外,通過模型結(jié)構(gòu)的修改還支持了小目標檢測與高分辨率圖像檢測。原始的YOLOv8模型結(jié)構(gòu)如下。
    的頭像 發(fā)表于 05-16 11:14 ?1.4w次閱讀
    <b class='flag-5'>YOLOv8</b>版本升級支持小<b class='flag-5'>目標</b>檢測與高分辨率圖像輸入

    AI愛克斯開發(fā)板上使用OpenVINO加速YOLOv8目標檢測模型

    《在AI愛克斯開發(fā)板上用OpenVINO加速YOLOv8分類模型》介紹了在AI愛克斯開發(fā)板上使用OpenVINO 開發(fā)套件部署并測評YOLOv8的分類模型,本文將介紹在AI愛克斯開發(fā)板上使用OpenVINO加速YOLOv8
    的頭像 發(fā)表于 05-26 11:03 ?1852次閱讀
    AI愛克斯開發(fā)板上使用OpenVINO加速<b class='flag-5'>YOLOv8</b><b class='flag-5'>目標</b>檢測模型

    目標檢測算法再升級!YOLOv8保姆級教程一鍵體驗

    YOLO作為一種基于圖像全局信息進行預(yù)測的目標檢測系統(tǒng),始終保持著極高的迭代更新率,從YOLOv5到YOLOv8,本次升級主要包括結(jié)構(gòu)算法、命令行界面、PythonAPI等。具體到YOLOv8
    的頭像 發(fā)表于 02-28 11:16 ?3450次閱讀
    <b class='flag-5'>目標</b>檢測算法再升級!<b class='flag-5'>YOLOv8</b>保姆級教程一鍵體驗

    解鎖YOLOv8修改+注意力模塊訓(xùn)練與部署流程

    很多人也想跟修改YOLOv5源碼一樣的方式去修改YOLOv8的源碼,但是在github上面卻發(fā)現(xiàn)找到的YOLOv8項目下面TAG分支是空的
    的頭像 發(fā)表于 08-11 14:14 ?5391次閱讀
    解鎖<b class='flag-5'>YOLOv8</b>修改+注意力模塊訓(xùn)練與部署流程

    如何修改YOLOv8的源碼

    很多人也想跟修改YOLOv5源碼一樣的方式去修改YOLOv8的源碼,但是在github上面卻發(fā)現(xiàn)找到的YOLOv8項目下面TAG分支是空的,然后就直接從master/main下面把源碼克隆出來一通
    的頭像 發(fā)表于 09-04 10:02 ?2632次閱讀
    如何修改<b class='flag-5'>YOLOv8</b>的源碼

    基于YOLOv8的自定義醫(yī)學(xué)圖像分割

    YOLOv8是一種令人驚嘆的分割模型;它易于訓(xùn)練、測試和部署。在本教程中,我們將學(xué)習如何在自定義數(shù)據(jù)集上使用YOLOv8。但在此之前,我想告訴你為什么在存在其他優(yōu)秀的分割模型時應(yīng)該使用YOLOv8
    的頭像 發(fā)表于 12-20 10:51 ?1281次閱讀
    基于<b class='flag-5'>YOLOv8</b>的自定義醫(yī)學(xué)圖像分割

    使用NVIDIA JetPack 6.0和YOLOv8構(gòu)建智能交通應(yīng)用

    進行視頻數(shù)據(jù)的接收與存儲;借助 YOLOv8 和 DeepStream AI 感知服務(wù)實現(xiàn)實時目標檢測和車輛追蹤;車輛移動的時空分析。在構(gòu)建好這一流程后,將利用 API 生成分析報告。
    的頭像 發(fā)表于 08-23 16:49 ?1017次閱讀
    使用NVIDIA JetPack 6.0和<b class='flag-5'>YOLOv8</b><b class='flag-5'>構(gòu)建</b>智能交通應(yīng)用

    RK3576 yolov8訓(xùn)練部署教程

    本章展示yolov8模型的在EASY EAI Orin nano的部署過程。
    的頭像 發(fā)表于 04-02 16:04 ?468次閱讀
    RK3576 <b class='flag-5'>yolov8</b>訓(xùn)練部署教程

    RV1126 yolov8訓(xùn)練部署教程

    YOLOv8 是 ultralytics 公司在 2023 年 1月 10 號開源的基于YOLOV5進行更新的 下一個重大更新版本,目前支持圖像分類、物體檢測和實例分割任務(wù),鑒于Yolov5的良好表現(xiàn),
    的頭像 發(fā)表于 04-16 14:53 ?495次閱讀
    RV1126 <b class='flag-5'>yolov8</b>訓(xùn)練部署教程