一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

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

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

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

如何在樹(shù)莓派 AI HAT+上進(jìn)行YOLO目標(biāo)檢測(cè)?

上海晶珩電子科技有限公司 ? 2025-07-19 08:34 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

大家好,接下來(lái)會(huì)為大家開(kāi)一個(gè)樹(shù)莓派5和YOLO的連載專題。

內(nèi)容包括四個(gè)部分:

在樹(shù)莓派5上使用YOLO進(jìn)行物體和動(dòng)物識(shí)別-入門指南

在樹(shù)莓派5上開(kāi)啟YOLO姿態(tài)估計(jì)識(shí)別之旅!

如何在樹(shù)莓派 AIHAT+上進(jìn)行YOLO目標(biāo)檢測(cè)?

如何在樹(shù)莓派 AI HAT+上進(jìn)行YOLO姿態(tài)估計(jì)?

今天是第三部分:如何在樹(shù)莓派 AI HAT+上進(jìn)行YOLO目標(biāo)檢測(cè)?

如果大家對(duì)這個(gè)專題感興趣,記得關(guān)注樹(shù)莓派開(kāi)發(fā)者,這樣你將會(huì)第一時(shí)間收到我們的內(nèi)容更新通知。

在本指南中,我們將探討如何使用樹(shù)莓派AI HAT設(shè)置YOLO目標(biāo)檢測(cè),更重要的是,學(xué)習(xí)如何在Python項(xiàng)目中應(yīng)用它。我們將了解如何安裝所需的硬件和固件,以及如何設(shè)置和使用目標(biāo)檢測(cè)Python管道。通過(guò)本指南,您將掌握整個(gè)設(shè)置過(guò)程,并獲得我們編寫的三個(gè)不同示例腳本。一個(gè)腳本在檢測(cè)到特定對(duì)象時(shí)“執(zhí)行某些操作”,另一個(gè)在檢測(cè)到特定數(shù)量的對(duì)象時(shí)執(zhí)行操作,最后一個(gè)在特定位置檢測(cè)到對(duì)象時(shí)執(zhí)行操作。

像我們其他大多數(shù)計(jì)算機(jī)視覺(jué)指南一樣,本指南也很有趣,讓我們開(kāi)始吧!


目錄:

所需材料

硬件組裝

安裝樹(shù)莓派操作系統(tǒng)

安裝AI HAT軟件和Python管道

運(yùn)行目標(biāo)檢測(cè)演示

示例代碼1:目標(biāo)檢測(cè)

示例代碼2:計(jì)數(shù)對(duì)象

示例代碼3:對(duì)象定位

運(yùn)行其他YOLO模型

接下來(lái)做什么?


所需材料

要跟隨本指南操作,您需要:

樹(shù)莓派5 - 2GB或更大內(nèi)存的型號(hào)均可。

AI HAT+板 - 本指南適用于13 TOPS和26 TOPS版本。TOPS是衡量AI加速器速度的指標(biāo),因此26 TOPS版本的AI HAT+速度大約是13 TOPS版本的兩倍。這意味著它可以以更高的FPS運(yùn)行更復(fù)雜、更強(qiáng)大的模型,比13 TOPS版本更出色。

引腳擴(kuò)展器(可選) - AI HAT+附帶了一個(gè)樹(shù)莓派引腳擴(kuò)展器,但通常它們不夠長(zhǎng),無(wú)法完全穿過(guò)HAT。如果您打算將硬件插入樹(shù)莓派或以其他方式使用引腳,則需要一個(gè)這樣的擴(kuò)展器來(lái)訪問(wèn)它們。

樹(shù)莓派攝像頭模塊 - 我們使用的是攝像頭模塊V3,但幾乎任何官方攝像頭模塊都可以使用。

攝像頭適配器線 - 樹(shù)莓派5使用的CSI攝像頭線尺寸與之前的型號(hào)不同,您的攝像頭可能附帶的是舊的更寬的線,因此值得仔細(xì)檢查一下。攝像頭模塊V3肯定需要一條適配器線。您還可以選擇更長(zhǎng)的線,如300mm和500mm!

散熱解決方案 - 對(duì)于樹(shù)莓派5本身,我們使用的是主動(dòng)散熱器。雖然AI HAT+可以在沒(méi)有散熱器的情況下運(yùn)行,但如果您長(zhǎng)時(shí)間運(yùn)行它,一個(gè)小型自粘散熱片可能是一項(xiàng)值得的投資。一點(diǎn)散熱措施可能大有裨益。

電源

Micro SD卡 - 至少16GB容量。

顯示器和Micro-HDMI轉(zhuǎn)HDMI線

鼠標(biāo)和鍵盤

*所需物品可以直接聯(lián)系我們進(jìn)行購(gòu)買。


硬件安裝

步驟1:安裝引腳擴(kuò)展器

170d00c0-6438-11f0-a486-92fbcf53809c.jpg

在樹(shù)莓派上安裝任何硬件之前,請(qǐng)確保已關(guān)閉電源并斷開(kāi)與任何電源的連接。

首先將GPIO引腳擴(kuò)展器安裝在樹(shù)莓派的引腳上。如果您使用的是更長(zhǎng)的引腳擴(kuò)展器,請(qǐng)?jiān)诖颂幨褂?。注意不要彎曲這些引腳,因?yàn)樗鼈兒荛L(zhǎng),很容易彎曲。

如果您在樹(shù)莓派上使用散熱片或散熱器,現(xiàn)在是安裝的時(shí)候了。

步驟2:安裝支柱

171bf5c6-6438-11f0-a486-92fbcf53809c.jpg

安裝隨AI HAT附帶的4個(gè)支架。支架附帶4個(gè)長(zhǎng)螺絲和4個(gè)短螺絲,使用哪一種都無(wú)妨。

步驟3:連接PCIe線

17280028-6438-11f0-a486-92fbcf53809c.jpg

要將HAT上的PCIe電纜安裝到樹(shù)莓派上,請(qǐng)先抬起樹(shù)莓派PCIe插槽上的棕色卡扣。將電纜插入插槽,確保其牢固且垂直地固定在插槽內(nèi)。然后將卡扣推回原位以固定電纜。

注意:避免過(guò)度彎曲或扭曲此電纜,因?yàn)樗赡茌^為脆弱。

步驟4:放置AI HAT

1736c1ee-6438-11f0-a486-92fbcf53809c.jpg

現(xiàn)在將HAT滑動(dòng)到針腳延長(zhǎng)器上,直到它平放在支架上。在此過(guò)程中請(qǐng)小心不要損壞PCIe電纜。

您的樹(shù)莓派在HAT下方可能會(huì)露出部分GPIO接口——這是正?,F(xiàn)象。

步驟5:安裝攝像頭

1745d4cc-6438-11f0-a486-92fbcf53809c.jpg

相機(jī)使用的連接器與PCIe連接器采用類似的卡扣式連接設(shè)計(jì)。在相機(jī)和樹(shù)莓派的連接器插槽上,先抬起卡扣,將電纜插入并確保其垂直對(duì)齊,然后將連接器按下固定到位。

步驟6:擰緊螺絲

175636be-6438-11f0-a486-92fbcf53809c.jpg

最后,用剩下的4顆螺絲將HAT固定好。如果你選擇使用自粘式散熱片在AI HAT上,將其放置在電路板中央的銀色處理單元上。

就這樣,我們完成了!


安裝樹(shù)莓派操作系統(tǒng)

176877e8-6438-11f0-a486-92fbcf53809c.png

首先,我們需要將樹(shù)莓派操作系統(tǒng)安裝到Micro SD卡上。使用樹(shù)莓派Imager,選擇樹(shù)莓派5作為設(shè)備,選擇Raspberry Pi OS(64位)作為操作系統(tǒng),并選擇您的MicroSD卡作為存儲(chǔ)設(shè)備。

注意:將樹(shù)莓派操作系統(tǒng)安裝到MicroSD卡上將擦除卡上的所有數(shù)據(jù)。

下載操作系統(tǒng)并安裝可能需要幾分鐘時(shí)間。安裝完成后,將其插入樹(shù)莓派并啟動(dòng)。您的樹(shù)莓派將進(jìn)行首次安裝,請(qǐng)確保將其連接到互聯(lián)網(wǎng)。

安裝AI HAT軟件和Python管道

讓我們開(kāi)始安裝運(yùn)行AI HAT所需的固件和軟件。打開(kāi)一個(gè)新的終端窗口,首先使用以下命令更新您的樹(shù)莓派:

sudo aptupdate&&sudo aptfull-upgrade

在這些步驟中,系統(tǒng)可能會(huì)詢問(wèn)您是否確認(rèn)安裝某些內(nèi)容,只需輸入“y”并回車即可。

1778b860-6438-11f0-a486-92fbcf53809c.jpg

現(xiàn)在使用以下命令安裝HAT固件:

sudoapt install hailo-all

此安裝過(guò)程可能需要5到10分鐘才能完成。安裝完成后,重新啟動(dòng)樹(shù)莓派。如果您想成為高級(jí)用戶,可以通過(guò)在終端中輸入以下命令來(lái)重新啟動(dòng):

reboot

現(xiàn)在我們將安裝Hailo的Python管道軟件和示例,但什么是管道呢?

與AI HAT硬件本身進(jìn)行通信極其復(fù)雜,所需的代碼也相當(dāng)復(fù)雜。我們將設(shè)置并安裝一個(gè)目標(biāo)檢測(cè)管道,它只是一組代碼和軟件,使我們能夠更輕松地與HAT進(jìn)行交互。它本質(zhì)上會(huì)將我們更簡(jiǎn)單、更易讀的代碼轉(zhuǎn)換為幕后所有的復(fù)雜操作,以使HAT運(yùn)行。

17894504-6438-11f0-a486-92fbcf53809c.jpg

要安裝管道及其所需的庫(kù),首先通過(guò)在終端中輸入以下命令來(lái)復(fù)制它們的GitHub存儲(chǔ)庫(kù):

gitclonehttps://github.com/hailo-ai/hailo-rpi5-examples.git

這將在樹(shù)莓派的主文件夾中下載一個(gè)名為“hailo-rpi5-examples”的文件夾,這將是我們將要在其中工作的重要位置。

1793184a-6438-11f0-a486-92fbcf53809c.jpg

在安裝管道之前,我們需要告訴終端通過(guò)更改目錄命令從該文件夾中工作:

cdhailo-rpi5-examples

終端中帶有文件位置的藍(lán)色文本表示您已成功運(yùn)行此命令?,F(xiàn)在我們將運(yùn)行shell腳本安裝程序:

./install.sh

此安裝過(guò)程可能需要10到20分鐘,因?yàn)樗€會(huì)安裝我們將要使用的所有YOLO模型。

安裝完成后,再次重新啟動(dòng)樹(shù)莓派。


運(yùn)行目標(biāo)檢測(cè)演示

讓我們運(yùn)行一些示例代碼!在之前的步驟中,我們從Hailo下載了一些示例管道以及使用這些管道的示例Python腳本。在本教程中,我們將使用目標(biāo)檢測(cè)管道 - 它被稱為“detection_pipeline.py”,位于hailo_rpi5-examples/basic_pipelines下。

17a1df60-6438-11f0-a486-92fbcf53809c.jpg

運(yùn)行這些Python腳本的最簡(jiǎn)單方法是通過(guò)終端。首先使用更改目錄命令更改終端的工作位置,這與我們之前使用的命令相同:

cdhailo-rpi5-examples

安裝步驟還創(chuàng)建了一個(gè)虛擬環(huán)境(也稱為Venv)。這本質(zhì)上是一個(gè)隔離的虛擬工作空間,我們可以在其中安裝軟件包并進(jìn)行實(shí)驗(yàn),而不會(huì)影響樹(shù)莓派操作系統(tǒng)的其他部分。我們需要使用的所有軟件包都已安裝在此Venv中,我們可以通過(guò)在終端中輸入以下命令來(lái)告訴終端進(jìn)入該環(huán)境:

sourcesetup_env.sh

您可以通過(guò)查看右側(cè)圖像中括號(hào)內(nèi)左側(cè)的名稱來(lái)確認(rèn)您正在Venv中工作。如果您已進(jìn)入Venv并且看到了更改目錄命令的藍(lán)色文本,那么您現(xiàn)在就可以運(yùn)行Python腳本了。如果您關(guān)閉了終端或重新啟動(dòng)了樹(shù)莓派,則需要再次運(yùn)行這些命令以返回到此狀態(tài)。

我們將運(yùn)行名為“detection.py”的演示Python代碼,該代碼位于“basic_pipelines”文件夾中,因此運(yùn)行此代碼的命令將是:

python basic_pipelines/detection.py

您應(yīng)該看到一個(gè)新窗口出現(xiàn),顯示人們過(guò)馬路的視頻,以及YOLO目標(biāo)檢測(cè)模型在幀中識(shí)別對(duì)象,如右側(cè)圖像所示。恭喜!您已成功在AI HAT上設(shè)置并運(yùn)行了計(jì)算機(jī)視覺(jué)。

17aea16e-6438-11f0-a486-92fbcf53809c.jpg

我們看到的這個(gè)視覺(jué)輸出很直觀。YOLO會(huì)嘗試在它認(rèn)為對(duì)象所在的位置周圍繪制一個(gè)邊界框,然后標(biāo)記它識(shí)別出的對(duì)象,并用百分比來(lái)評(píng)估識(shí)別的置信度。默認(rèn)的YOLO模型是在COCO數(shù)據(jù)集上訓(xùn)練的,該數(shù)據(jù)集僅包含81個(gè)可檢測(cè)的對(duì)象。

https://gist.github.com/AruniRC/7b3dadd004da04c80198557db5da4bda

這可能看起來(lái)不多,但其中許多是日常生活中常見(jiàn)的廣泛對(duì)象類別,如“運(yùn)動(dòng)球”和“瓶子”。

要以我們的攝像頭作為輸入視頻源來(lái)運(yùn)行Python代碼,我們需要將其指定為一個(gè)參數(shù)或選項(xiàng)。我們可以通過(guò)輸入以下命令來(lái)獲取目標(biāo)檢測(cè)管道的所有可用選項(xiàng)列表:

python basic_pipelines/detection.py --help

17c2d652-6438-11f0-a486-92fbcf53809c.jpg

這里有一些有用的選項(xiàng)可供探索,您應(yīng)該找個(gè)時(shí)間嘗試一下,但我們感興趣的是使用“--input”選項(xiàng)更改源。在這里我們可以看到,我們可以指定一個(gè)文件或攝像頭作為輸入,并且我們可以使用以下命令運(yùn)行帶有攝像頭模塊的檢測(cè)腳本:

python basic_pipelines/detection.py --input rpi

現(xiàn)在我們的代碼已經(jīng)運(yùn)行并從攝像頭中檢測(cè)對(duì)象了,讓我們快速了解一下幕后發(fā)生了什么,以便我們學(xué)習(xí)如何在自定義項(xiàng)目中應(yīng)用此目標(biāo)檢測(cè)。這里有很多復(fù)雜的過(guò)程,有數(shù)千行代碼在運(yùn)行,但其中大部分都是在管道中幕后運(yùn)行的。這對(duì)我們來(lái)說(shuō)很幸運(yùn),因?yàn)檫@意味著我們只需要與“detection.py”文件交互,該文件更加簡(jiǎn)潔且易于人類閱讀(我們稱之為高級(jí)代碼)。

讓我們?cè)赥honny中打開(kāi)detection.py并探索其中發(fā)生了什么。代碼首先導(dǎo)入所有所需的軟件包和庫(kù)以運(yùn)行,其中兩個(gè)是同一文件夾中的Python腳本,“detection_pipeline”和“hailo_rpi_common”。如果您希望修改此設(shè)置中不在detection.py中的更深層次的行為,它很可能位于這些文件中。如果您希望為項(xiàng)目導(dǎo)入任何庫(kù)或軟件包,請(qǐng)像往常一樣在此處添加。

importgigi.require_version('Gst','1.0')fromgi.repositoryimportGst,GLibimportosimportnumpyasnpimportcv2importhailofromhailo_apps_infra.hailo_rpi_commonimport( get_caps_from_pad, get_numpy_from_buffer, app_callback_class,)fromhailo_apps_infra.detection_pipelineimportGStreamerDetectionApp

然后,我們?cè)谀_本頂部定義了一個(gè)類,其中嵌套了一個(gè)名為“__init__”的函數(shù)。通常在編寫代碼時(shí),我們將“初始化”代碼放在頂部,并且只運(yùn)行一次。這個(gè)類就是放置此“初始化”代碼的地方。因此,如果我們想定義一個(gè)常量變量、設(shè)置一個(gè)引腳或聲明一個(gè)函數(shù),則必須在此部分中完成。Hailo在此部分提供了一個(gè)創(chuàng)建變量和函數(shù)的示例:

classuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() self.new_variable =42 # New variable example defnew_function(self): # New function example return"The meaning of life is: "

然后我們有一個(gè)名為“app_callback”的函數(shù)。每次AI HAT處理一幀時(shí),此函數(shù)的內(nèi)容都會(huì)運(yùn)行,可以將其視為我們?cè)诖a中通常使用的while true循環(huán)。此循環(huán)中的前十幾行代碼對(duì)我們來(lái)說(shuō)興趣不大。它們主要是在管理管道,并在最后創(chuàng)建了一個(gè)名為“detections”的對(duì)象,該對(duì)象保存了來(lái)自YOLO模型的所有目標(biāo)檢測(cè)信息。

defapp_callback(pad, info, user_data): # Get the GstBuffer from the probe info buffer = info.get_buffer() # Check if the buffer is valid ifbufferisNone: returnGst.PadProbeReturn.OK # Using the user_data to count the number of frames user_data.increment() string_to_print =f"Frame count:{user_data.get_count()}\n" # Get the caps from the pad format, width, height = get_caps_from_pad(pad) # If the user_data.use_frame is set to True, we can get the video frame from the buffer frame =None ifuser_data.use_frameandformatisnotNoneandwidthisnotNoneandheightisnotNone: # Get video frame frame = get_numpy_from_buffer(buffer,format, width, height) # Get the detections from the buffer roi = hailo.get_roi_from_buffer(buffer) detections = roi.get_objects_typed(hailo.HAILO_DETECTION)

現(xiàn)在我們到了這段代碼的核心部分,開(kāi)始處理detections對(duì)象。在這個(gè)for循環(huán)內(nèi)部是一系列步驟。對(duì)于每個(gè)檢測(cè)到的對(duì)象,它首先獲取其標(biāo)簽(名稱)、邊界框的坐標(biāo)(圍繞對(duì)象的框)和檢測(cè)的置信度。這三個(gè)信息片段是您的項(xiàng)目所需的關(guān)鍵拼圖,因?yàn)樗鼈兪菙z像頭所看到內(nèi)容的輸出。在此演示代碼中,它檢查檢測(cè)到的事物是否是人,然后將計(jì)數(shù)器增加1,并統(tǒng)計(jì)圖像中的人數(shù)(盡管它并沒(méi)有對(duì)此進(jìn)行任何操作)。

detection_count=0 fordetection in detections: label= detection.get_label() bbox= detection.get_bbox() confidence= detection.get_confidence() iflabel =="person": string_to_print+= f"Detection: {label} {confidence:.2f}\n" detection_count+=1

這段代碼的其余部分使用openCV顯示相關(guān)信息,然后將檢測(cè)數(shù)據(jù)打印到shell。

ifuser_data.use_frame: #Note:using imshow will not work here, as the callback function is not running in the main thread # Let's print the detection count to the frame cv2.putText(frame, f"Detections: {detection_count}", (10,30), cv2.FONT_HERSHEY_SIMPLEX,1, (0,255,0),2) # Example of how to use the new_variable and new_function from the user_data # Let's print the new_variable and the result of the new_function to the frame cv2.putText(frame, f"{user_data.new_function()} {user_data.new_variable}", (10,60), cv2.FONT_HERSHEY_SIMPLEX,1, (0,255,0),2) # Convert the frame to BGR frame= cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) user_data.set_frame(frame) print(string_to_print) returnGst.PadProbeReturn.OK

有了這個(gè)腳本,您現(xiàn)在就有了開(kāi)始使用AI HAT進(jìn)行自己的目標(biāo)檢測(cè)項(xiàng)目的工具。對(duì)于一些創(chuàng)客來(lái)說(shuō),這可能已經(jīng)足夠了,但我們還根據(jù)detection.py編寫了三個(gè)更精煉和健壯的示例代碼。這些腳本旨在讓您能夠啟動(dòng)項(xiàng)目,同時(shí)還提供了更多關(guān)于如何根據(jù)需要修改detections.py的示例。


示例代碼1:目標(biāo)檢測(cè)

此腳本的最終目標(biāo)是在檢測(cè)到特定對(duì)象時(shí)“執(zhí)行某些操作”。以下是完整代碼:

importgigi.require_version('Gst','1.0')fromgi.repositoryimportGst, GLibimportosimportnumpyasnpimportcv2importhailofromhailo_apps_infra.hailo_rpi_commonimport( get_caps_from_pad, get_numpy_from_buffer, app_callback_class,)fromhailo_apps_infra.detection_pipelineimportGStreamerDetectionAppfromgpiozeroimportAngularServo# Inheritance from the app_callback_classclassuser_app_callback_class(app_callback_class): def__init__(self): super().__init__()
# Initialize state variables for debouncing self.detection_counter =0 # Count consecutive frames with detections self.no_detection_counter =0 # Count consecutive frames without detections
# State tracking, is it active or not? self.is_it_active =False
self.servo = AngularServo(18, min_pulse_width=0.0006, max_pulse_width=0.0023)
defapp_callback(pad, info, user_data): # Get the GstBuffer from the probe info buffer = info.get_buffer() # Check if the buffer is valid ifbufferisNone: returnGst.PadProbeReturn.OK
# Using the user_data to count the number of frames user_data.increment()
# Get the caps from the pad format, width, height = get_caps_from_pad(pad) # If the user_data.use_frame is set to True, we can get the video frame from the buffer frame =None ifuser_data.use_frameandformatisnotNoneandwidthisnotNoneandheightisnotNone: frame = get_numpy_from_buffer(buffer,format, width, height)
# Get the detections from the buffer roi = hailo.get_roi_from_buffer(buffer) detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
# Track if we've seen objects of interest this frame object_detected =False detection_string =""
# Parse the detections fordetectionindetections: label = detection.get_label() confidence = detection.get_confidence()
# Check for objects of interest with confidence threshold ifconfidence >0.4: # Adjust confidence threshold as needed iflabel =="person": object_detected =True detection_string +=f"Detection:{label}{confidence:.2f}\n" # Debouncing logic ifobject_detected: user_data.detection_counter +=1 user_data.no_detection_counter =0
# Only activate after given amount of consecutive frames with detections ifuser_data.detection_counter >=4andnotuser_data.is_it_active: # Move the Servo or do what ever you want to do user_data.servo.angle =90 # Update the is it active variable so this doesnt keep repeating user_data.is_it_active =True print("OBJECT DETECTED!") else: user_data.no_detection_counter +=1 user_data.detection_counter =0
# Only deactivate after 5 consecutive frames without detections ifuser_data.no_detection_counter >=5anduser_data.is_it_active: # Move the Servo or do what ever you want to do user_data.servo.angle =0 user_data.is_it_active =False print("Object Gone.") # Print detections if any ifdetection_string: print(detection_string, end='')
returnGst.PadProbeReturn.OKif__name__ =="__main__": # Create an instance of the user app callback class user_data = user_app_callback_class() app = GStreamerDetectionApp(app_callback, user_data) app.run()

要運(yùn)行此代碼,請(qǐng)?jiān)谕粋€(gè)basic_pipelines文件夾中創(chuàng)建一個(gè)新的Python文件。對(duì)于此示例,我們將其命名為“watcher.py”。要運(yùn)行代碼,命令與之前相同,但必須使用新名稱:

python basic_pipelines/watcher.py --input rpi

17cf6174-6438-11f0-a486-92fbcf53809c.jpg

此代碼目前設(shè)置為在檢測(cè)到人時(shí)移動(dòng)伺服。一個(gè)實(shí)際應(yīng)用示例是解決辦公室里的一個(gè)實(shí)際問(wèn)題。在我的辦公桌前,我經(jīng)常戴著耳機(jī),這使得當(dāng)人們從我身后進(jìn)入辦公室時(shí)很容易嚇到我。當(dāng)這段代碼檢測(cè)到指向我身后門的攝像頭中有人時(shí),它會(huì)旋轉(zhuǎn)一個(gè)伺服,創(chuàng)建一個(gè)視覺(jué)警報(bào),表明有人進(jìn)來(lái)了!

讓我們看看代碼的一些關(guān)鍵部分,了解我們添加了什么以及如何根據(jù)您的需求進(jìn)行定制。

我們首先導(dǎo)入完全相同的庫(kù),但添加了gpiozero庫(kù),其中包含我們將使用的易于使用的伺服控制。

fromgpiozeroimportAngularServo

然后在只運(yùn)行一次的類中,我們創(chuàng)建了幾個(gè)變量。有兩個(gè)用于去抖動(dòng)的計(jì)數(shù)器變量,以及一個(gè)用于跟蹤伺服當(dāng)前狀態(tài)的“is_it_active”變量。我們還設(shè)置了連接到引腳18的伺服。非常重要的一點(diǎn)是,當(dāng)我們?cè)谶@一部分創(chuàng)建變量時(shí),必須使用“self.”前綴。如果我們希望一個(gè)變量在app_callback()(“while true循環(huán)”)中可訪問(wèn),我們需要有這個(gè)前綴。

classuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() # Initialize state variables for debouncing self.detection_counter =0 # Count consecutive frames with detections self.no_detection_counter =0 # Count consecutive frames without detections # State tracking, is it active or not? self.is_it_active =False self.servo = AngularServo(18, min_pulse_width=0.0006, max_pulse_width=0.0023)

app_callback()的前半部分保持不變,因?yàn)樗诠芾砉艿?,我們不想更改它。之后,我們首先?chuàng)建一個(gè)名為“object_detected”和“detection_string”的變量。由于我們是在循環(huán)內(nèi)部創(chuàng)建它們,因此不需要“self.”前綴,但它們將在每個(gè)循環(huán)周期結(jié)束時(shí)被擦除。

# Track if we've seen objects of interest this frame object_detected =False detection_string ="" # Parse the detections fordetectionindetections: label = detection.get_label() confidence = detection.get_confidence() # Check for objects of interest with confidence threshold ifconfidence >0.4: # Adjust confidence threshold as needed iflabel =="person": object_detected =True detection_string +=f"Detection:{label}{confidence:.2f}\n"

然后我們進(jìn)入for detections循環(huán),遍歷當(dāng)前幀中檢測(cè)到的每個(gè)對(duì)象。在這里,我們像往常一樣獲取標(biāo)簽和置信度。然后,我們將檢查置信度是否大于0.4(40%),以及它是否被識(shí)別為人 - 您可以根據(jù)需要調(diào)整置信度和要檢測(cè)的對(duì)象。如果它是人且置信度足夠,我們將object_detected設(shè)置為true。在這一部分,我們基本上分析了攝像頭看到的每個(gè)對(duì)象,并檢查其中是否至少有一個(gè)人。

# Parse the detections fordetectionindetections: label = detection.get_label() confidence = detection.get_confidence() # Check for objects of interest with confidence threshold ifconfidence >0.4: # Adjust confidence threshold as needed iflabel =="person": object_detected =True detection_string +=f"Detection:{label}{confidence:.2f}\n"

接下來(lái)的這一部分真正使這段代碼變得健壯。如果我們檢測(cè)到了人,我們將detection_counter增加1,并將no_detection_counter重置為0,因?yàn)檫@個(gè)計(jì)數(shù)器是在計(jì)算我們有多少幀沒(méi)有看到人。非常重要的一點(diǎn)是,當(dāng)我們使用在類部分中聲明的detection_counter時(shí),必須使用“user_data.”前綴。因此,當(dāng)我們?cè)陬惒糠稚戏絼?chuàng)建變量時(shí)使用“self.” - 當(dāng)我們想在app_callback()中使用它時(shí),必須使用“user_data.”。

在下一行中,我們檢查detection_counter是否大于4,如果是,我們移動(dòng)伺服(再次注意,當(dāng)我們移動(dòng)伺服時(shí),必須使用“user_data.”)。在這里,您可以放置自定義代碼,以便在檢測(cè)到對(duì)象時(shí)“執(zhí)行某些操作”。您可以發(fā)送電子郵件、打開(kāi)燈、旋轉(zhuǎn)電機(jī),任何您想要的操作!

但我們?yōu)槭裁匆B續(xù)計(jì)算4幀后再執(zhí)行操作呢?這是因?yàn)槲覀冋跒榘粹o添加類似去抖動(dòng)的功能。假設(shè)這段代碼沒(méi)有去抖動(dòng),我們用它來(lái)監(jiān)視房子外面,以便在檢測(cè)到狗時(shí)自動(dòng)打開(kāi)狗門。假設(shè)系統(tǒng)工作得很好,但有一天一只貓接近了門。Yolo會(huì)非??煽康刈R(shí)別出它是一只貓 - 但計(jì)算機(jī)視覺(jué)并不完美。有一幀它可能會(huì)錯(cuò)誤地將貓識(shí)別為狗,這會(huì)在我們不希望的時(shí)候打開(kāi)狗門。大多數(shù)時(shí)候這不是問(wèn)題,但我們?cè)跍y(cè)試中發(fā)現(xiàn),在某些極端情況下,這可能會(huì)成為一個(gè)相當(dāng)大的問(wèn)題。

這段去抖動(dòng)代碼通過(guò)確保YOLO連續(xù)4次檢測(cè)到目標(biāo)對(duì)象后再“執(zhí)行某些操作”,有助于消除這個(gè)問(wèn)題,您可以根據(jù)項(xiàng)目需求更改這個(gè)數(shù)字。

# Debouncing logic ifobject_detected: user_data.detection_counter +=1 user_data.no_detection_counter =0 # Only activate after given amount of consecutive frames with detections ifuser_data.detection_counter >=4and not user_data.is_it_active: # Move the Servo or do what ever you want to do user_data.servo.angle =90 # Update the is it active variable so this doesnt keep repeating user_data.is_it_active = True print("OBJECT DETECTED!")

我們也有相反的情況。在這個(gè)else分支中(如果我們沒(méi)有檢測(cè)到人),我們將detection_counter設(shè)置為0,并將no_detection_counter增加1。然后我們檢查no_detection_counter是否大于5,如果是,我們將移動(dòng)伺服返回。在這里,您可以放置自定義代碼,以便在未檢測(cè)到目標(biāo)對(duì)象時(shí)“執(zhí)行其他操作”。

這段代碼需要在5幀內(nèi)未檢測(cè)到對(duì)象后才會(huì)“執(zhí)行某些操作”,我們這樣做的原因與之前類似。假設(shè)一只狗正走向狗門,當(dāng)它走過(guò)時(shí),YOLO在一幀中將其識(shí)別為馬。這會(huì)在可憐的狗經(jīng)過(guò)時(shí)關(guān)上門。因此,我們通過(guò)確保在目標(biāo)對(duì)象消失前有5幀未檢測(cè)到它來(lái)解決這個(gè)問(wèn)題。

值得注意的是,這段去抖動(dòng)代碼會(huì)使代碼響應(yīng)速度降低,因?yàn)楸仨氃诜磻?yīng)前經(jīng)過(guò)4或5幀。代碼很可能以30fps運(yùn)行,因此這大約會(huì)有1/6秒的延遲。如果您想移除這個(gè)功能,可以將這些值設(shè)置為1。

這段代碼中還有一件事值得探索。在這些去抖動(dòng)檢查的末尾,我們還檢查了我們創(chuàng)建的is_it_active變量:

ifuser_data.detection_counter >=4and not user_data.is_it_active:

這本質(zhì)上確保了每次檢測(cè)到對(duì)象時(shí),我們只“執(zhí)行一次該操作”。如果對(duì)象離開(kāi)并再次返回,它將再次“執(zhí)行該操作”,但只執(zhí)行一次。假設(shè)我們不是移動(dòng)伺服,而是在檢測(cè)到人時(shí)發(fā)送電子郵件。按照當(dāng)前的設(shè)置,如果檢測(cè)到人,它將發(fā)送一封電子郵件,并且只有當(dāng)人離開(kāi)并再次返回時(shí)才會(huì)發(fā)送另一封。但是假設(shè)我們沒(méi)有檢查這個(gè)is_it_active變量。代碼會(huì)檢測(cè)到一個(gè)人,并且每次分析幀時(shí)都會(huì)發(fā)送一封電子郵件(每秒30封)。如果需要,您可以從代碼中移除這部分。


示例代碼2:計(jì)數(shù)對(duì)象

此腳本使用與上一段代碼相同的去抖動(dòng)邏輯,但除了“執(zhí)行某些操作”外,我們?cè)跈z測(cè)到特定數(shù)量的對(duì)象時(shí)“執(zhí)行某些操作”。以下是完整代碼:

importgigi.require_version('Gst','1.0')fromgi.repositoryimportGst, GLibimportosimportnumpyasnpimportcv2importhailofromhailo_apps_infra.hailo_rpi_commonimport( get_caps_from_pad, get_numpy_from_buffer, app_callback_class,)fromhailo_apps_infra.detection_pipelineimportGStreamerDetectionAppfromgpiozeroimportLEDclassuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() # Configuration self.target_object ="cup" # Object type to detect
# Debouncing variables self.detection_counter =0 # Consecutive frames with exact match self.no_detection_counter =0 # Consecutive frames without match
# State tracking, is it active or not? self.is_it_active =False
self.green_led = LED(18) self.red_led = LED(14)
self.red_led.off() self.green_led.on()defapp_callback(pad, info, user_data): # Get the GstBuffer from the probe info buffer = info.get_buffer() # Check if the buffer is valid ifbufferisNone: returnGst.PadProbeReturn.OK
# Using the user_data to count the number of frames user_data.increment()
# Get the caps from the pad format, width, height = get_caps_from_pad(pad) # If the user_data.use_frame is set to True, we can get the video frame from the buffer frame =None ifuser_data.use_frameandformatisnotNoneandwidthisnotNoneandheightisnotNone: frame = get_numpy_from_buffer(buffer,format, width, height)
# Get the detections from the buffer roi = hailo.get_roi_from_buffer(buffer) detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
# Count objects in this frame object_count =0 detection_string =""
# Parse the detections fordetectionindetections: label = detection.get_label() confidence = detection.get_confidence()
# Check for target objects with confidence threshold ifconfidence >0.4: iflabel == user_data.target_object: object_count +=1 detection_string +=f"{label.capitalize()}detected! Confidence:{confidence:.2f}\n"
# Debouncing logic for number of items ifobject_count >=3: user_data.detection_counter +=1 user_data.no_detection_counter =0
# Only activate after sufficient consistent frames ifuser_data.detection_counter >=4andnotuser_data.is_it_active: # Turn on red led, or do what ever else you want to do user_data.red_led.on() user_data.green_led.off()
user_data.is_it_active =True print(f"NUMBER OF OBJECTS DETECTED!") else: user_data.no_detection_counter +=1 user_data.detection_counter =0
# Only deactivate after sufficient non-matching frames ifuser_data.no_detection_counter >=5anduser_data.is_it_active: # Turn on green LED or what ever else you wish to do user_data.red_led.off() user_data.green_led.on()
user_data.is_it_active =False print(f"No longer detecting number of objects.") # Print detections if any ifdetection_string: print(f"Current{user_data.target_object}count:{object_count}") print(detection_string, end='')
returnGst.PadProbeReturn.OKif__name__ =="__main__": # Create an instance of the user app callback class user_data = user_app_callback_class() app = GStreamerDetectionApp(app_callback, user_data) app.run()

此代碼目前設(shè)置為控制這個(gè)塔燈中的一對(duì)LED - 與前一個(gè)示例完全相同。此示例應(yīng)用的一個(gè)實(shí)際應(yīng)用是,我們將其設(shè)置為智能監(jiān)控系統(tǒng)。樹(shù)脂3D打印部件在最后固化步驟之前是有毒的,我們希望在打印時(shí)讓人們遠(yuǎn)離它。因此,此設(shè)置將尋找人,如果檢測(cè)到人并且他們位于打印機(jī)附近,它將打開(kāi)警告燈。

17dfd680-6438-11f0-a486-92fbcf53809c.jpg

這段代碼在很大程度上與之前的示例代碼相似,但有一些添加。首先,我們從gpiozero庫(kù)中導(dǎo)入LED,這是向GPIO引腳發(fā)送數(shù)字信號(hào)的最簡(jiǎn)單方法。

fromgpiozeroimportLED

然后在類部分中,我們創(chuàng)建了一個(gè)要計(jì)數(shù)的目標(biāo)對(duì)象的變量,在此示例中我們將其設(shè)置為杯子 - 再次根據(jù)您的需要進(jìn)行更改。然后我們有與上次相同的計(jì)數(shù)變量。但在此部分中,我們?cè)O(shè)置了LED并初始化紅色為關(guān)閉狀態(tài),綠色為開(kāi)啟狀態(tài)。注意我們?nèi)匀槐仨毷褂谩皊elf.”前綴。

classuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() # Configuration self.target_object ="cup" # Object type to detect # Debouncing variables self.detection_counter =0 # Consecutive frames with exact match self.no_detection_counter =0 # Consecutive frames without match # State tracking, is it active or not? self.is_it_active =False self.green_led = LED(18) self.red_led = LED(14) self.red_led.off() self.green_led.on()

我們的代碼在很大程度上是相同的,直到我們到達(dá)檢測(cè)循環(huán)。在這里,對(duì)于每個(gè)置信度高于0.4且標(biāo)記為我們目標(biāo)對(duì)象的對(duì)象,我們將計(jì)數(shù)器增加1。

# Check for target objects with confidence threshold ifconfidence >0.4: iflabel == user_data.target_object: object_count+=1 detection_string+= f"{label.capitalize()} detected! Confidence: {confidence:.2f}\n"

然后,如果有等于或多于3個(gè)這樣的對(duì)象,我們?cè)黾訖z測(cè)計(jì)數(shù)器并運(yùn)行與之前相同的去抖動(dòng)代碼,并將根據(jù)相同的邏輯控制LED。

# Debouncing logic for number of items ifobject_count >=3: user_data.detection_counter +=1 user_data.no_detection_counter =0 # Only activate after sufficient consistent frames ifuser_data.detection_counter >=4and not user_data.is_it_active: # Turn on red led, or do what ever else you want to do user_data.red_led.on() user_data.green_led.off() user_data.is_it_active = True print(f"NUMBER OF OBJECTS DETECTED!") else: user_data.no_detection_counter +=1 user_data.detection_counter =0 # Only deactivate after sufficient non-matching frames ifuser_data.no_detection_counter >=5and user_data.is_it_active: # Turn on green LED or what ever else you wish to do user_data.red_led.off() user_data.green_led.on() user_data.is_it_active = False print(f"No longer detecting number of objects.")


示例代碼3:對(duì)象定位

此腳本使用與前一段代碼相同的去抖動(dòng)邏輯,但在這里我們跟蹤被檢測(cè)對(duì)象的中心,如果它移動(dòng)到指定位置,我們“執(zhí)行某些操作”。以下是完整代碼:

importgigi.require_version('Gst','1.0')fromgi.repositoryimportGst, GLibimportosimportnumpyasnpimportcv2importhailofromhailo_apps_infra.hailo_rpi_commonimport( get_caps_from_pad, get_numpy_from_buffer, app_callback_class,)fromhailo_apps_infra.detection_pipelineimportGStreamerDetectionAppfromgpiozeroimportLEDclassuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() # Configuration self.target_object ="person" # Object type to detect
# Target zone configuration (normalized coordinates 0-1) self.zone_x_min =0.4 # Left boundary of target zone self.zone_x_max =0.6 # Right boundary of target zone self.zone_y_min =0.3 # Top boundary of target zone self.zone_y_max =0.7 # Bottom boundary of target zone
# Debouncing variables self.in_zone_frames =0 # Consecutive frames with object in zone self.out_zone_frames =0 # Consecutive frames without object in zone
# State tracking self.is_it_active =False self.green_led = LED(18) self.red_led = LED(14)
self.red_led.off() self.green_led.on()
defapp_callback(pad, info, user_data): # Get the GstBuffer from the probe info buffer = info.get_buffer() ifbufferisNone: returnGst.PadProbeReturn.OK
user_data.increment()
# Get the caps from the pad format, width, height = get_caps_from_pad(pad) frame =None ifuser_data.use_frameandformatisnotNoneandwidthisnotNoneandheightisnotNone: frame = get_numpy_from_buffer(buffer,format, width, height)
# Get the detections from the buffer roi = hailo.get_roi_from_buffer(buffer) detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
object_in_zone =False detection_string =""
# Parse the detections fordetectionindetections: label = detection.get_label() confidence = detection.get_confidence()
ifconfidence >0.4andlabel == user_data.target_object: # Get bounding box coordinates bbox = detection.get_bbox()
# Call the coordinate methods x_min = bbox.xmin() y_min = bbox.ymin() box_width = bbox.width() box_height = bbox.height()
# Calculate max coordinates x_max = x_min + box_width y_max = y_min + box_height
# Calculate center point (these are normalized 0-1) center_x = x_min + (box_width /2) center_y = (y_min + (box_height /2) -0.22) *1.83 # Debug print for coordinates detection_string += (f"{label.capitalize()}detected!\n" f"Position: center=({center_x:.2f},{center_y:.2f})\n" f"Bounds: xmin={x_min:.2f}, ymin={y_min:.2f}, xmax={x_max:.2f}, ymax={y_max:.2f}\n" f"Confidence:{confidence:.2f}\n")
# Check if object's center is in the target zone if(user_data.zone_x_min <= center_x <= user_data.zone_x_max?and?? ? ? ? ? ? ? ? (user_data.zone_y_min -?0.22) *?1.83?<= center_y <= (user_data.zone_y_max -?0.22) *?1.83):? ? ? ? ? ? ? ? object_in_zone =?True? ? ? ? ? ? ? ? detection_string +=?f"Object is in target zone!\n"
# Debouncing logic for zone detection ifobject_in_zone: user_data.in_zone_frames +=1 user_data.out_zone_frames =0
ifuser_data.in_zone_frames >=4andnotuser_data.is_it_active: # Turn on red led, or do what ever else you want to do user_data.red_led.on() user_data.green_led.off()
user_data.is_it_active =True print(f"{user_data.target_object.capitalize()}detected in target zone - Servo activated!") else: user_data.out_zone_frames +=1 user_data.in_zone_frames =0
ifuser_data.out_zone_frames >=5anduser_data.is_it_active: user_data.red_led.off() user_data.green_led.on()
user_data.is_it_active =False print(f"No{user_data.target_object}in target zone - Servo deactivated!") # Print detections if any ifdetection_string: print(detection_string, end='')
returnGst.PadProbeReturn.OKif__name__ =="__main__": user_data = user_app_callback_class() app = GStreamerDetectionApp(app_callback, user_data) app.run()

此代碼目前設(shè)置為控制這個(gè)塔燈中的一對(duì)LED。另一個(gè)實(shí)際問(wèn)題的解決方案 - 每次我起身拿飲料時(shí),經(jīng)常會(huì)帶回一個(gè)新杯子,它們開(kāi)始擠滿我的桌子。這段代碼將檢測(cè)我的桌子上是否有3個(gè)或更多杯子,如果有,將打開(kāi)紅燈。如果杯子少于3個(gè),它將打開(kāi)綠燈。

17ed8c1c-6438-11f0-a486-92fbcf53809c.jpg

我們從與之前相同的類設(shè)置部分開(kāi)始,但添加了一些額外的變量以在圖像上繪制一個(gè)框,該框具有最小和最大的x和y值。如果對(duì)象位于此框內(nèi),我們將“執(zhí)行某些操作”。該框由小數(shù)定義,x軸上0為圖像左側(cè),1.0為右側(cè),0.5為中間。y軸從0(頂部)到1.0(底部)。以下是可以繪制的兩個(gè)框的示例,您可以更改這些x和y最小和最大變量以設(shè)置自定義檢測(cè)框。

detection_counter變量也已更改為in_zone_frames和out_zone_frames。這只是名稱更改,使用方式完全相同。

classuser_app_callback_class(app_callback_class): def__init__(self): super().__init__() # Configuration self.target_object ="person" # Object type to detect # Target zone configuration (normalized coordinates 0-1) self.zone_x_min =0.4 # Left boundary of target zone self.zone_x_max =0.6 # Right boundary of target zone self.zone_y_min =0.3 # Top boundary of target zone self.zone_y_max =0.7 # Bottom boundary of target zone # Debouncing variables self.in_zone_frames =0 # Consecutive frames with object in zone self.out_zone_frames =0 # Consecutive frames without object in zone # State tracking self.is_it_active =False self.green_led = LED(18) self.red_led = LED(14) self.red_led.off() self.green_led.on()

17fa00aa-6438-11f0-a486-92fbcf53809c.jpg

其余代碼幾乎相同,但在我們的檢測(cè)循環(huán)中,如果標(biāo)簽是target_object且置信度高于0.4,我們從YOLO獲取邊界框,找到其中心,然后檢查其是否在我們的最小和最大x和y值內(nèi)。如果是,則我們將object_in_zone設(shè)置為true,并運(yùn)行與前兩個(gè)代碼片段完全相同的去抖動(dòng)邏輯。

ifconfidence >0.4andlabel == user_data.target_object: # Get bounding box coordinates bbox = detection.get_bbox() # Call the coordinate methods x_min = bbox.xmin() y_min = bbox.ymin() box_width = bbox.width() box_height = bbox.height() # Calculate max coordinates x_max = x_min + box_width y_max = y_min + box_height # Calculate center point (these are normalized 0-1) center_x = x_min + (box_width /2) center_y = (y_min + (box_height /2) -0.22) *1.83 # Debug print for coordinates detection_string += (f"{label.capitalize()}detected!\n" f"Position: center=({center_x:.2f},{center_y:.2f})\n" f"Bounds: xmin={x_min:.2f}, ymin={y_min:.2f}, xmax={x_max:.2f}, ymax={y_max:.2f}\n" f"Confidence:{confidence:.2f}\n") # Check if object's center is in the target zone if(user_data.zone_x_min <= center_x <= user_data.zone_x_max?and?? ? ? ? ? ? ? ? (user_data.zone_y_min -?0.22) *?1.83?<= center_y <= (user_data.zone_y_max -?0.22) *?1.83):? ? ? ? ? ? ? ? object_in_zone =?True? ? ? ? ? ? ? ? detection_string +=?f"Object is in target zone!\n"


運(yùn)行其他YOLO模型

我們?yōu)楹我x擇其他模型呢?實(shí)際上,YOLO在不同版本和模型尺寸上有許多變體。較新的YOLO模型通常功能更強(qiáng)大,且處理需求更低。未來(lái),你或許能在這里找到Y(jié)OLO11以及其他新模型,但選擇其他模型的主要原因在于更改模型尺寸。YOLO有5種尺寸——納米級(jí)、小型、中型、大型和超大型。模型越大,其處理能力越強(qiáng)、精度越高、檢測(cè)距離越遠(yuǎn),但會(huì)犧牲幀率(FPS)。默認(rèn)情況下,13 TOPS的設(shè)備將運(yùn)行小型模型,26 TOPS的設(shè)備將運(yùn)行中型模型,這兩種情況下都能實(shí)現(xiàn)流暢的30 FPS。

AI HAT+可以運(yùn)行多種神經(jīng)網(wǎng)絡(luò)模型,但有一個(gè)前提條件——必須先將模型轉(zhuǎn)換為HEF格式,并且需要針對(duì)特定型號(hào)的擴(kuò)展板進(jìn)行轉(zhuǎn)換(為13 TOPS擴(kuò)展板轉(zhuǎn)換的模型可能在26 TOPS擴(kuò)展板上表現(xiàn)不佳)。將模型轉(zhuǎn)換為HEF格式需要遵循一定流程,Hailo公司提供了相關(guān)指南,但請(qǐng)注意,這是一個(gè)相當(dāng)復(fù)雜的過(guò)程。

https://github.com/hailo-ai/hailo-rpi5-examples/blob/main/doc/retraining-example.md

而且,這可能是一個(gè)極其漫長(zhǎng)的過(guò)程,即使配備了最高端的GPU,也可能需要數(shù)小時(shí),而使用低端GPU甚至CPU進(jìn)行轉(zhuǎn)換,則可能需要數(shù)天或數(shù)周時(shí)間。

不過(guò),幸運(yùn)的是,我們可以從互聯(lián)網(wǎng)上獲取預(yù)轉(zhuǎn)換的模型,其中最好的來(lái)源之一就是Hailo的模型庫(kù)。該庫(kù)提供了適用于13 TOPS(Hailo-8)和26 TOPS(Hailo-8L)擴(kuò)展板的模型集合。雖然可以使用多種模型,但我們將重點(diǎn)關(guān)注YOLO系列,因?yàn)槲覀儼l(fā)現(xiàn)該流程更有可能與它們兼容。

要運(yùn)行模型,只需下載其編譯版本,該版本應(yīng)位于表格的最右側(cè)(你可能需要向右滾動(dòng)才能看到)。請(qǐng)仔細(xì)檢查下載的是“.hef”格式的文件。下載完成后,將其拖放到“hailo-rpi5-examples”文件夾中的“resources”文件夾內(nèi),與其他YOLO模型放在一起。在我們的示例中,我們下載了名為“yolov8l.hef”的大型模型。

如果模型已放入文件夾中,你可以通過(guò)在運(yùn)行命令中添加該模型作為選項(xiàng)來(lái)運(yùn)行它,此時(shí)需要使用模型名稱。例如,我們的命令如下:

python basic_pipelines/detection.py --input rpi --hef resources/yolov8l.hef

如果你獲得了其他已轉(zhuǎn)換為HEF格式的YOLO模型,操作流程也是一樣的。


接下來(lái)該做什么?

現(xiàn)在,我們已經(jīng)設(shè)置好了樹(shù)莓派和AI HAT,并使用了一些示例代碼運(yùn)行了目標(biāo)檢測(cè)功能,這些代碼可以用于你的項(xiàng)目中?,F(xiàn)在,唯一剩下的就是弄清楚如何實(shí)現(xiàn)你項(xiàng)目中想要完成的“某件事”。我們提供了一些關(guān)于樹(shù)莓派的一般指南,幫助你入門,例如,如何使用繼電器控制直流電機(jī)和步進(jìn)電機(jī)、伺服電機(jī),甚至電磁閥(你可以使用繼電器控制幾乎任何東西)。

使用樹(shù)莓派和繼電器控制電磁閥:

https://core-electronics.com.au/guides/raspberry-pi/control-servo-raspberry-pi/


原文地址:

https://core-electronics.com.au/guides/raspberry-pi/yolo-object-detection-on-the-raspberry-pi-ai-hat-writing-custom-python/#KBNAKNI

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

    關(guān)注

    88

    文章

    35194

    瀏覽量

    280251
  • 目標(biāo)檢測(cè)
    +關(guān)注

    關(guān)注

    0

    文章

    227

    瀏覽量

    16028
  • 樹(shù)莓派
    +關(guān)注

    關(guān)注

    121

    文章

    2016

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評(píng)論

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

    完整指南:如何使用樹(shù)莓5、Hailo AI Hat、YOLO、Docker進(jìn)行自定義數(shù)據(jù)集訓(xùn)練?

    今天,我將展示如何使用令人印象深刻的HailoAIHat在樹(shù)莓5上訓(xùn)練、編譯和部署自定義模型。注意:文章內(nèi)的鏈接可能需要科學(xué)上網(wǎng)。HailoAIHat根據(jù)你的設(shè)置,在樹(shù)莓5的CPU
    的頭像 發(fā)表于 06-28 08:23 ?1306次閱讀
    完整指南:如何使用<b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>5、Hailo <b class='flag-5'>AI</b> <b class='flag-5'>Hat</b>、<b class='flag-5'>YOLO</b>、Docker<b class='flag-5'>進(jìn)行</b>自定義數(shù)據(jù)集訓(xùn)練?

    MCC推出用于樹(shù)莓的MCC 118電壓測(cè)量HAT模塊

    HAT)提供8通道模擬電壓輸入,基于樹(shù)莓的數(shù)據(jù)采集/數(shù)據(jù)記錄系統(tǒng)。每張MCC 118最大采樣率為100kS/s,可以進(jìn)行單電壓點(diǎn)和電壓波形采集。最多可在單塊
    發(fā)表于 08-30 10:28

    MCC基于樹(shù)莓HAT模塊

    ),數(shù)模轉(zhuǎn)換器(DAC)或條件數(shù)字輸入和輸出(DIO)。但是,可以通過(guò)USB端口或支持SPI和I2C的GPIO的40-pin接頭擴(kuò)展這些功能。直接和樹(shù)莓GPIO進(jìn)行堆棧式連接的設(shè)備稱為HAT
    發(fā)表于 09-05 11:45

    樹(shù)莓MCC118

    on Top),用于市場(chǎng)上最流行的單板計(jì)算機(jī)樹(shù)莓(Raspberry Pi)HAT是種附加板,帶有40W GPIO(通用輸入/輸出)連接器,符合Raspberry PiHAT規(guī)范。MCC 118
    發(fā)表于 01-21 09:22

    【POE HAT擴(kuò)展板試用連載】樹(shù)莓3B+電路板POE供電應(yīng)用

    項(xiàng)目名稱:樹(shù)莓3B+電路板POE供電應(yīng)用試用計(jì)劃:申請(qǐng)理由本人嵌入式領(lǐng)域有十余年的開(kāi)發(fā)經(jīng)驗(yàn),去年購(gòu)買了一塊樹(shù)莓3B+的電路板用來(lái)設(shè)計(jì)一款廚余垃圾處理機(jī)器的控制器,計(jì)劃利用這塊擴(kuò)展板
    發(fā)表于 01-09 11:49

    樹(shù)莓ReSpeaker 2 Mics Pi HAT電路原理圖免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是樹(shù)莓ReSpeaker 2 Mics Pi HAT電路原理圖免費(fèi)下載。
    發(fā)表于 06-16 14:22 ?0次下載

    樹(shù)莓ReSpeaker 2 Mics Pi HAT的PCB圖免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是樹(shù)莓ReSpeaker 2 Mics Pi HAT的PCB圖免費(fèi)下載。
    發(fā)表于 06-16 08:00 ?0次下載
    <b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>ReSpeaker 2 Mics Pi <b class='flag-5'>HAT</b>的PCB圖免費(fèi)下載

    目標(biāo)檢測(cè)YOLO的重要性!

    YOLO是什么? 它是One-stage目標(biāo)檢測(cè)的代表,整個(gè)框架非常簡(jiǎn)單。與RCNN算法不一樣,是以不同方式處理對(duì)象檢測(cè)。 YOLO算法的最
    的頭像 發(fā)表于 06-10 15:45 ?4458次閱讀

    樹(shù)莓新推AI HAT+:26 TOPS高性能版本震撼登場(chǎng)

     在成功推出樹(shù)莓AI套件與AI攝像頭后,樹(shù)莓再次擴(kuò)大其A
    的頭像 發(fā)表于 11-07 13:44 ?1335次閱讀

    樹(shù)莓派上部署YOLOv5進(jìn)行動(dòng)物目標(biāo)檢測(cè)的完整流程

    卓越的性能。本文將詳細(xì)介紹如何在性能更強(qiáng)的計(jì)算機(jī)上訓(xùn)練YOLOv5模型,并將訓(xùn)練好的模型部署到樹(shù)莓4B上,通過(guò)樹(shù)莓的攝像頭
    的頭像 發(fā)表于 11-11 10:38 ?3744次閱讀
    在<b class='flag-5'>樹(shù)莓</b>派上部署YOLOv5<b class='flag-5'>進(jìn)行</b>動(dòng)物<b class='flag-5'>目標(biāo)</b><b class='flag-5'>檢測(cè)</b>的完整流程

    如何將 M.2 HAT+ 與 Raspberry Pi 5 一起使用?

    邊緣連接器。您可以連接任何使用2230或2242尺寸的設(shè)備。M.2HAT+最大可提供3A的電源輸出。M.2HAT+使用了樹(shù)莓HAT+規(guī)范
    的頭像 發(fā)表于 03-25 09:48 ?307次閱讀
    如何將 M.2 <b class='flag-5'>HAT+</b> 與 Raspberry Pi 5 一起使用?

    樹(shù)莓分類器:用樹(shù)莓識(shí)別不同型號(hào)的樹(shù)莓!

    在本教程系列的第一部分中,您將學(xué)習(xí)如何使用樹(shù)莓AI攝像頭來(lái)檢測(cè)不同的樹(shù)莓型號(hào)。本系列由Dav
    的頭像 發(fā)表于 06-13 16:39 ?427次閱讀
    <b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>分類器:用<b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>識(shí)別不同型號(hào)的<b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>!

    樹(shù)莓5上使用YOLO進(jìn)行物體和動(dòng)物識(shí)別-入門指南

    大家好,接下來(lái)會(huì)為大家開(kāi)一個(gè)樹(shù)莓5和YOLO的專題。內(nèi)容包括四個(gè)部分:在樹(shù)莓5上使用YOLO
    的頭像 發(fā)表于 07-17 17:16 ?169次閱讀
    在<b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>5上使用<b class='flag-5'>YOLO</b><b class='flag-5'>進(jìn)行</b>物體和動(dòng)物識(shí)別-入門指南

    樹(shù)莓5上開(kāi)啟YOLO姿態(tài)估計(jì)識(shí)別之旅!

    大家好,接下來(lái)會(huì)為大家開(kāi)一個(gè)樹(shù)莓5和YOLO的連載文章。內(nèi)容包括四個(gè)部分:在樹(shù)莓5上使用YOLO
    的頭像 發(fā)表于 07-18 15:31 ?124次閱讀
    在<b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b>5上開(kāi)啟<b class='flag-5'>YOLO</b>姿態(tài)估計(jì)識(shí)別之旅!

    何在樹(shù)莓 AI HAT+上進(jìn)行YOLO姿態(tài)估計(jì)?

    YOLO目標(biāo)檢測(cè)?如何在樹(shù)莓AIHAT+上進(jìn)行
    的頭像 發(fā)表于 07-20 20:34 ?28次閱讀
    如<b class='flag-5'>何在</b><b class='flag-5'>樹(shù)莓</b><b class='flag-5'>派</b> <b class='flag-5'>AI</b> <b class='flag-5'>HAT+</b><b class='flag-5'>上進(jìn)行</b><b class='flag-5'>YOLO</b>姿態(tài)估計(jì)?