? ? ? ? 有同學(xué)后臺(tái)私信我,想用YOLOv5實(shí)現(xiàn)目標(biāo)的分類計(jì)數(shù),因此本文將在之前目標(biāo)計(jì)數(shù)博客的基礎(chǔ)上添加一些代碼,實(shí)現(xiàn)分類計(jì)數(shù)。閱讀本文前請(qǐng)先看那篇博客,鏈接如下:
YOLOv5實(shí)現(xiàn)目標(biāo)計(jì)數(shù)_Albert_yeager的博客
1. 分類實(shí)現(xiàn)
? ? ? ? 以coco數(shù)據(jù)集為例,其類別如下(共80類)。注意,每個(gè)類別都對(duì)應(yīng)著一個(gè)序號(hào),如: 'person' 序號(hào)為0,? 'bicycle' 序號(hào)為1,? 'car'? 序號(hào)為2...這個(gè)在之后的調(diào)用中會(huì)用到。
? ? ? ? ?找到之前寫的的計(jì)數(shù)模塊(詳見之前的博客),將其替換為下面的代碼,即可實(shí)現(xiàn)分類計(jì)數(shù)功能,下面我將進(jìn)行詳細(xì)的講解。
# Write results+計(jì)數(shù)
# count=0
person_count = 0
tie_count = 0
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) # label format
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_img or view_img: # Add bbox to image
#c = int(cls)# integer class分類數(shù)
#label = '%s %.2f num: %d' % (names[int(cls)], conf, person_count)
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
##########################分類計(jì)數(shù)##########################
if int(cls) == 0:
person_count += 1
if int(cls) == 27:
tie_count += 1
# count = count+1
? ? ? ? ?添加的主要代碼為1、2、4,其中1、2是初始化兩個(gè)類別的個(gè)數(shù),這里我選擇人('person')和領(lǐng)帶('tie')作為兩個(gè)計(jì)數(shù)的類(可以根據(jù)需求添加自己的類)。3是顯示標(biāo)簽的格式,下面那一行是官方的,你也可以改成自己喜歡的樣子。
????????重點(diǎn)來了!4中的代碼是兩個(gè)判斷,int(cls)表示類別的序號(hào)。在coco的類別中,'person' 的序號(hào)為0,因此當(dāng) int(cls) ==?0 時(shí)也就是當(dāng)識(shí)別到人時(shí),人的計(jì)數(shù)器 person_count+1;'tie' 的序號(hào)為27,因此當(dāng) int(cls) ==?27 時(shí)也就是當(dāng)識(shí)別到領(lǐng)帶時(shí),領(lǐng)帶的計(jì)數(shù)器 tie_count+1。這樣就能實(shí)現(xiàn)分類計(jì)數(shù)。
? ? ? ? ?這里用的推斷模型是yolov5s.pt,是用coco數(shù)據(jù)集訓(xùn)練出來的,因此識(shí)別的類就是上面展示的80類,序號(hào)就是依次從0-79(這是一句廢話)。那么如果要對(duì)自己的數(shù)據(jù)集進(jìn)行分類計(jì)數(shù),那么就要用自己訓(xùn)練出來的模型進(jìn)行推斷,序號(hào)就是按訓(xùn)練部署時(shí)names數(shù)組中的序列,從0開始,依次遞增。
? ? ? ? 為了講得更清楚,我再舉一個(gè)例子(點(diǎn)贊關(guān)注一下唄(>﹏<) ,555~)
????????下面是我自己訓(xùn)練部署時(shí)的數(shù)據(jù)集文件內(nèi)容,可以看到我要識(shí)別五個(gè)目標(biāo),分別是HEWSN,那么根據(jù)names數(shù)組中的序列,‘H’對(duì)應(yīng)的序號(hào)為0,E對(duì)應(yīng)1,W對(duì)應(yīng)2,S對(duì)應(yīng)3,N對(duì)應(yīng)4。
? ? ? ? ?如果我想對(duì)W和S進(jìn)行分類計(jì)數(shù),那么上面的代碼應(yīng)該改成下面這樣,首先定義W_count,S_count兩個(gè)計(jì)數(shù)器,然后當(dāng)int(cls) == 2時(shí)W_count+1,int(cls) == 3時(shí)S_count+1。當(dāng)然,推斷模型記得改成自己的(更改 '--weights' 參數(shù)的默認(rèn)值)。
# Write results+計(jì)數(shù)
# count=0
W_count = 0
S_count = 0
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) # label format
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_img or view_img: # Add bbox to image
#c = int(cls)# integer class分類數(shù)
#label = '%s %.2f num: %d' % (names[int(cls)], conf, person_count)
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
##########################分類計(jì)數(shù)##########################
if int(cls) == 2:
W_count += 1
if int(cls) == 3:
S_count += 1
# count = count+1
2. 圖片/視頻識(shí)別顯示計(jì)數(shù)內(nèi)容
? ? ? ? 為了將計(jì)數(shù)結(jié)果顯示在圖像上,需要使用 cv2.putText() 函數(shù),具體添加方法如下:
???????在 “if save_img:” 后添加下面這幾行代碼即可,這里我就只打印人和領(lǐng)帶的計(jì)數(shù)了,大家可以根據(jù)自己的需求改。
##############################視頻識(shí)別顯示計(jì)數(shù)內(nèi)容####################################
text = 'person_num:%d ' % (person_count)
cv2.putText(im0, text, (180, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
text = 'tie_num:%d ' % (tie_count)
cv2.putText(im0, text, (180, 120), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 5)
####################################################################################
? ? ? ? 另外注意cv2.putText()函數(shù)的幾個(gè)參數(shù)意義,然后慢慢調(diào)參,讓打印出來的圖片美觀就行了。
cv2.putText(im0, text, (40, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
# 要繪制的圖像(im0)
# 要繪制的文本字符串(text)
# 文本的位置(x, y),窗口左上角為(0,0)
# 要使用的字體類型(font),這里用OpenCV的內(nèi)嵌字體
# 字體大小(font_scale),在此處為1
# 字體顏色(font_color),在此處為紅色(0, 0, 255)
# 字體線寬(thickness),在此處為4???
????????最后實(shí)現(xiàn)效果如下(視頻識(shí)別同理):
?????????
3. 實(shí)時(shí)檢測(cè)窗口打印計(jì)數(shù)內(nèi)容
????????為了將計(jì)數(shù)結(jié)果顯示在實(shí)時(shí)檢測(cè)的窗口中,同樣需要使用 cv2.putText() 函數(shù),具體的修改方法如下:
????????在 “if view_img:” 后添加下面這幾行代碼(這里我就只打印人的計(jì)數(shù)了,大家可以根據(jù)自己的需求改)。
##############################實(shí)時(shí)檢測(cè)窗口打印計(jì)數(shù)內(nèi)容#################################
text = 'person_num:%d ' % (person_count)
cv2.putText(im0, text, (180, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
####################################################################################
????????
? ? ? ? 希望這篇文章可以幫助到大家,其他評(píng)論區(qū)和私信問我問題的同學(xué)們也不要急,你們的問題我一直在研究,如果完成了我會(huì)第一時(shí)間發(fā)出來并通知你的(≧?≦)/文章來源:http://www.zghlxwxcb.cn/news/detail-474281.html
求學(xué)路上,你我共勉(??????)??文章來源地址http://www.zghlxwxcb.cn/news/detail-474281.html
到了這里,關(guān)于YOLOv5實(shí)現(xiàn)目標(biāo)分類計(jì)數(shù)并顯示在圖像上的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!