<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

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

3天內不再提示

手把手教你使用LabVIEW OpenCV dnn實現物體識別(Object Detection)含源碼

LabVIEW深度學習實戰 ? 來源:wangstoudamire ? 作者:wangstoudamire ? 2023-03-10 15:58 ? 次閱讀

前言

今天和大家一起分享如何使用LabVIEW調用pb模型實現物體識別,本博客中使用的智能工具包可到主頁置頂博客LabVIEW AI視覺工具包(非NI Vision)下載與安裝教程中下載

一、物體識別算法原理概述

1、物體識別的概念

物體識別也稱 目標檢測 ,目標檢測所要解決的問題是目標在哪里以及其狀態的問題。但是,這個問題并不是很容易解決。形態不合理,對象出現的區域不確定,更不用說對象也可以是多個類別。**

**在這里插入圖片描述

目標檢測用的比較多的主要是RCNN,spp- net,fast- rcnn,faster- rcnn;YOLO系列,如YOLOV3和YOLOV4;除此之外還有SSD,ResNet等。

2、Yolo算法原理概述

Yolo的識別原理簡單清晰。對于輸入的圖片,將整張圖片分為7×7(7為參數,可調)個方格。當某個物體的中心點落在了某個方格中,該方格則負責預測該物體。每個方格會為被預測物體產生2(參數,可調)個候選框并生成每個框的置信度。最后選取置信度較高的方框作為預測結果。

在這里插入圖片描述

二、opencv調用darknet物體識別模型(yolov3/yolov4)

相關源碼及模型在darknt文件夾下

在這里插入圖片描述

使用darknet訓練yolo的模型,生成weights文件。使用opencv調用生成的模型

1、darknet模型的獲取

文件含義:

  • **cfg文件:模型描述文件 **
  • weights文件:模型權重文件

Yolov3獲取鏈接:

https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg

https://pjreddie.com/media/files/yolov3.weights

Yolov4獲取鏈接:

https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.cfg

https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights

2、python調用darknet模型實現物體識別

(1)dnn模塊調用darknet模型

net = cv2.dnn.readNetFromDarknet("yolov3/yolov3.cfg", "yolov3/yolov3.weights")

(2)獲取三個輸出端的LayerName

使用getUnconnectedOutLayer獲取三個只有輸入,沒有輸出的層的名字,Yolov3的三個輸出端層名為:['yolo_82', 'yolo_94', 'yolo_106']

def getOutputsNames(net):
    # Get the names of all the layers in the network
    layersNames = net.getLayerNames()
    # Get the names of the output layers, i.e. the layers with unconnected outputs
    return [layersNames[i - 1] for i in net.getUnconnectedOutLayers()]

**(3)圖像預處理 **

使用blobFromImage將圖像轉為image

Size=(416,416)或(608,608)

Scale=1/255

Means=[0,0,0]

blob = cv2.dnn.blobFromImage(frame, 1/255, (416, 416), [0,0,0], 1, crop=False)

(4)推理

使用net.forward(multiNames)獲取多個層的結果,其中getOutputsNames(net)=['yolo_82', 'yolo_94', 'yolo_106']

net.setInput(blob)
outs = net.forward(getOutputsNames(net))

**(5)后處理(postrocess) **

獲取的結果(outs)里面有三個矩陣(out),每個矩陣的大小為85*n,n表示檢測到了n個物體,85的排列順序是這樣的:

  • 第0列代表物體中心x在圖中的位置(0~1)
  • 第1列表示物體中心y在圖中的位置(0~1)
  • 第2列表示物體的寬度
  • 第3列表示物體的高度
  • 第4列是置信概率,值域為[0-1],用來與閾值作比較決定是否標記目標
  • 第5~84列為基于COCO數據集的80分類的標記權重,最大的為輸出分類。使用這些參數保留置信度高的識別結果(confidence>confThreshold)
def postprocess(frame, outs):
    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]
    classIds = []
    confidences = []
    boxes = []
    classIds = []
    confidences = []
    boxes = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            if confidence > confThreshold:
                center_x = int(detection[0] * frameWidth)
                center_y = int(detection[1] * frameHeight)
                width = int(detection[2] * frameWidth)
                height = int(detection[3] * frameHeight)
                left = int(center_x - width / 2)
                top = int(center_y - height / 2)
                classIds.append(classId)
                confidences.append(float(confidence))
                boxes.append([left, top, width, height])
    print(boxes)
    print(confidences)

**(6)后處理(postrocess) **

使用NMSBoxes函數過濾掉重復識別的區域。

indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)  
    for i in indices:
        box = boxes[i]
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]
        drawPred(classIds[i], confidences[i], left, top, left + width, top + height)

(7)畫出檢測到的對象

def drawPred(classId, conf, left, top, right, bottom):
    # Draw a bounding box.
    cv.rectangle(frame, (left, top), (right, bottom), (0, 0, 255))
     
    label = '%.2f' % conf
         
    # Get the label for the class name and its confidence
    if classes:
        assert(classId < len(classes))
        label = '%s:%s' % (classes[classId], label)

    #Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))

(8)完整源碼及檢測結果(cv_call_yolo.py)

import cv2
cv=cv2
import numpy as np
import time
net = cv2.dnn.readNetFromDarknet("yolov3/yolov3.cfg", "yolov3/yolov3.weights")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
?
confThreshold = 0.5  #Confidence threshold
nmsThreshold = 0.4   #Non-maximum suppression threshold
frame=cv2.imread("dog.jpg")
classesFile = "coco.names";
classes = None
with open(classesFile, 'rt') as f:
    classes = f.read().rstrip('\\\\\\\\\\\\\\\\n').split('\\\\\\\\\\\\\\\\n')
?
def getOutputsNames(net):
    # Get the names of all the layers in the network
    layersNames = net.getLayerNames()
    # Get the names of the output layers, i.e. the layers with unconnected outputs
    return [layersNames[i - 1] for i in net.getUnconnectedOutLayers()]
print(getOutputsNames(net))
# Remove the bounding boxes with low confidence using non-maxima suppression
?
def postprocess(frame, outs):
    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]
    classIds = []
    confidences = []
    boxes = []
    # Scan through all the bounding boxes output from the network and keep only the
    # ones with high confidence scores. Assign the box's class label as the class with the highest score.
    classIds = []
    confidences = []
    boxes = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            if confidence > confThreshold:
                center_x = int(detection[0] * frameWidth)
                center_y = int(detection[1] * frameHeight)
                width = int(detection[2] * frameWidth)
                height = int(detection[3] * frameHeight)
                left = int(center_x - width / 2)
                top = int(center_y - height / 2)
                classIds.append(classId)
                confidences.append(float(confidence))
                boxes.append([left, top, width, height])

    # Perform non maximum suppression to eliminate redundant overlapping boxes with
    # lower confidences.
    print(boxes)
    print(confidences)  
    indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold) 
    for i in indices:
        #print(i)
        #i = i[0]
        box = boxes[i]
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]
        drawPred(classIds[i], confidences[i], left, top, left + width, top + height)
?
    # Draw the predicted bounding box
def drawPred(classId, conf, left, top, right, bottom):
    # Draw a bounding box.
    cv.rectangle(frame, (left, top), (right, bottom), (0, 0, 255))
    label = '%.2f' % conf    
    # Get the label for the class name and its confidence
    if classes:
        assert(classId < len(classes))
        label = '%s:%s' % (classes[classId], label)
    #Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
blob = cv2.dnn.blobFromImage(frame, 1/255, (416, 416), [0,0,0], 1, crop=False)
t1=time.time()
net.setInput(blob)
outs = net.forward(getOutputsNames(net))
print(time.time()-t1)
postprocess(frame, outs)
t, _ = net.getPerfProfile()
label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
cv2.imshow("result",frame)
?

在這里插入圖片描述

3、LabVIEW調用darknet模型實現物體識別yolo_example.vi

(1)LabVIEW調用yolov3的方式及步驟和python類似,源碼如下所示:

在這里插入圖片描述

將待識別圖片與yolo_example.vi置于同一路徑下,即可進行物體識別

(2)識別結果如下:

在這里插入圖片描述

4、LabVIEW實現實時攝像頭物體識別(yolo_example_camera.vi)

(1)使用GPU加速

使用順序結構檢測神經網絡推理的時間

在這里插入圖片描述

比較使用GPU和不使用GPU兩種情況下的推理速度

普通模式 :net.serPerferenceBackend(0),net.serPerferenceTarget(0)**

** Nvidia GPU模式 :net.serPreferenceBackend(5), net.serPerferenceTarget(6)**

**在這里插入圖片描述

**注:普通的c++、python、LabVIEW版本的opencv,即便選了GPU模式也沒用,程序仍然運行在CPU上,需要安裝CUDA和CUDNN后重新從源碼編譯opencv **

(2)程序源碼如下:

在這里插入圖片描述

(3)物體識別結果如下:

在這里插入圖片描述

注意,使用如上程序,可以點擊STOP按鈕,停止本次物體識別,也可勾選使用GPU進行加速

(4)使用GPU加速結果:

在這里插入圖片描述

三、tensorflow的物體識別模型調用

相關源碼及模型在tf1文件夾下

在這里插入圖片描述

1、下載預訓練模型并生成pbtxt文件

(1)下載ssd_mobilenet_v2_coco,下載地址如下:

http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz

(2)解壓后的文件內容

在這里插入圖片描述

(3)根據pb模型生成pbtxt文件

運行 tf_text_graph_ssd.py以生成pptxt文件

在cmd中運行:

**python tf_text_graph_ssd.py --input ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb --config ssd_mobilenet_v1_coco_2017_11_17/ssd_mobilenet_v1_coco.config --output ssd_mobilenet_v1_coco_2017_11_17.pbtxt **

2、LabVIEW調用tensorflow模型推理并實現物體識別(callpb.vi)

(1)程序源碼如下:

在這里插入圖片描述

(2)運行結果如下:

在這里插入圖片描述

四、項目源碼及模型下載

鏈接: https://pan.baidu.com/s/1zwbLQe0VehGhsqNIHyaFRw?pwd=8888

**提取碼:8888 **

總結拓展

**可以使用Yolov3訓練自己的數據集,具體訓練方法可參考博客

可實現案例:口罩佩戴識別、肺炎分類、CT等,如口罩佩戴檢測

在這里插入圖片描述

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • LabVIEW
    +關注

    關注

    1930

    文章

    3620

    瀏覽量

    318324
  • 目標檢測
    +關注

    關注

    0

    文章

    186

    瀏覽量

    15520
  • OpenCV
    +關注

    關注

    29

    文章

    612

    瀏覽量

    40890
  • 物體識別
    +關注

    關注

    0

    文章

    14

    瀏覽量

    7453
  • dnn
    dnn
    +關注

    關注

    0

    文章

    56

    瀏覽量

    8975
收藏 人收藏

    評論

    相關推薦

    【原創】小草手把手教你LabVIEW視頻系列匯總帖(12.22更新)

    為了讓大家更好的查找小草手把手教你LabVIEW視頻教學系列,小編特為大家匯總如下:【視頻教學】小草手把手LabVIEW編程—LED滾動屏【
    發表于 12-08 10:10

    【匯總篇】小草手把手教你 LabVIEW 串口儀器控制

    `課程推薦>>《每天1小時,龍哥手把手教您LabVIEW視覺設計》[hide]小草手把手教你 LabVIEW 串口儀器控制—生成
    發表于 02-04 10:45

    【視頻匯總】小草大神手把手教你Labview技巧及源代碼分享

    點擊學習>>《龍哥手把手教你LabVIEW視覺設計》視頻教程原創視頻教程小草手把手教你LabVIEW
    發表于 05-26 13:48

    手把手教你LabVIEW儀器控制

    手把手教你LabVIEW儀器控制,串口學習
    發表于 12-11 12:00

    labview測試tensorflow深度學習SSD模型識別物體

    文件調用labview深度學習推理函數完成識別以上是識別動物和人等物體labview識別效果。
    發表于 08-16 17:21

    手把手教你如何一步一步實現人臉識別的門禁系統

    是一個人臉識別的門禁系統開源源碼及論文,基本功能實現,但其教程較簡略且有欠缺。本教程將從零開始,手把手教你如何一步一步
    發表于 12-14 06:44

    手把手教你構建一個完整的工程

    手把手教你構建一個完整的工程
    發表于 08-03 09:54 ?33次下載
    <b class='flag-5'>手把手</b><b class='flag-5'>教你</b>構建一個完整的工程

    手把手教你學DSP28335_張卿杰

    手把手教你學DSP28335張卿杰百度云分享手把手教你學DSP28335張卿杰百度云分享
    發表于 01-11 11:45 ?173次下載

    小草手把手教你LabVIEW儀器控制V1.0

    小草手把手教你LabVIEW儀器控制V1.0 ,感興趣的小伙伴們可以看看。
    發表于 08-03 17:55 ?94次下載

    手把手教你如何開始DSP編程

    手把手教你如何開始DSP編程。
    發表于 04-09 11:54 ?12次下載
    <b class='flag-5'>手把手</b><b class='flag-5'>教你</b>如何開始DSP編程

    手把手教你LabVIEW視覺設計

    手把手教你LabVIEW視覺設計手把手教你LabVIEW視覺設計
    發表于 03-06 01:41 ?2839次閱讀

    手把手教你學DSP-基于TMS320X281x

    顧衛剛手把手教你學DSP
    發表于 05-24 16:06 ?27次下載

    手把手教你使用LabVIEW OpenCV DNN實現手寫數字識別(含源碼

    LabVIEW中如何使用OpenCV DNN模塊實現手寫數字識別
    的頭像 發表于 03-08 16:10 ?1177次閱讀

    手把手教你使用LabVIEW OpenCV dnn實現圖像分類(含源碼

    使用LabVIEW OpenCV dnn實現圖像分類
    的頭像 發表于 03-09 13:37 ?860次閱讀

    手把手教你學FPGA仿真

    電子發燒友網站提供《手把手教你學FPGA仿真.pdf》資料免費下載
    發表于 10-19 09:17 ?1次下載
    <b class='flag-5'>手把手</b><b class='flag-5'>教你</b>學FPGA仿真
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>