<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          教程 | OpenCV深度神經(jīng)網(wǎng)絡實現(xiàn)人體姿態(tài)評估

          共 16350字,需瀏覽 33分鐘

           ·

          2021-09-19 07:02

          點擊上方小白學視覺”,選擇加"星標"或“置頂

          重磅干貨,第一時間送達

          OpenCV DNN模塊介紹


          OpenCV自從發(fā)布了DNN模塊之后,就開始以開掛的方式支持各種深度學習預訓練模型的調(diào)用,DNN模塊的全稱為深度神經(jīng)網(wǎng)絡,但是并不是所有深度學習模型導出到OpenCV DNN模塊中都可以使用,只有那些OpenCV聲明支持的層與網(wǎng)絡模型才會被DNN模塊接受,當期OpenCV支持的模型與層類型可以在下面鏈接中找到相關文檔

          https://github.com/opencv/opencv/wiki/Deep-Learning-in-OpenCV


          模型下載


          OpenCV3.4.x的版本開始支持在OpenCV DNN模塊中使用openopse的深度學習模型,實現(xiàn)人體單人姿態(tài)評估, 首先需要下載人體姿態(tài)評估的預訓練模型?;贑OCO數(shù)據(jù)集訓練的模型下載地址如下:

          http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/coco/pose_iter_440000.caffemodel

          基于MPI數(shù)據(jù)集訓練的模型下載地址如下:

          http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/mpi/pose_iter_160000.caffemodel


          代碼實現(xiàn)


          下面只需要如下幾步就可以實現(xiàn)基于OpenCV的單人姿態(tài)評估:

          1.定義COCO數(shù)據(jù)集支持的18點人體位置與關系位置

          BODY_PARTS = { "Nose"0"Neck"1"RShoulder"2"RElbow"3"RWrist"4,
                         "LShoulder"5"LElbow"6"LWrist"7"RHip"8"RKnee"9,
                         "RAnkle"10"LHip"11"LKnee"12"LAnkle"13"REye"14,
                         "LEye"15"REar"16"LEar"17"Background"18 }

          POSE_PAIRS = [ ["Neck""RShoulder"], ["Neck""LShoulder"], ["RShoulder""RElbow"],
                         ["RElbow""RWrist"], ["LShoulder""LElbow"], ["LElbow""LWrist"],
                         ["Neck""RHip"], ["RHip""RKnee"], ["RKnee""RAnkle"], ["Neck""LHip"],
                         ["LHip""LKnee"], ["LKnee""LAnkle"], ["Neck""Nose"], ["Nose""REye"],
                         ["REye""REar"], ["Nose""LEye"], ["LEye""LEar"] ]

          2.定義MPI數(shù)據(jù)集支持的15點人體位置與關系位置

          BODY_PARTS = { "Head": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
                         "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
                         "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "Chest": 14,
                         "Background": 15 }

          POSE_PAIRS = [ ["Head""Neck"], ["Neck""RShoulder"], ["RShoulder""RElbow"],
                         ["RElbow""RWrist"], ["Neck""LShoulder"], ["LShoulder""LElbow"],
                         ["LElbow""LWrist"], ["Neck""Chest"], ["Chest""RHip"], ["RHip""RKnee"],
                         ["RKnee""RAnkle"], ["Chest""LHip"], ["LHip""LKnee"], ["LKnee""LAnkle"] ]

          3.根據(jù)不同數(shù)據(jù)集調(diào)用DNN模塊加載指定的預訓練模型

          inWidth = 368
          inHeight = 368
          thr = 0.1
          protoc = "D:/projects/pose_body/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
          model = "D:/projects/pose_body/mpi/pose_iter_160000.caffemodel"
          net = cv.dnn.readNetFromCaffe(protoc, model)

          4.調(diào)用OpenCV打開攝像頭

          cap = cv.VideoCapture(0)
          height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
          width = cap.get(cv.CAP_PROP_FRAME_WIDTH)

          5.使用前饋網(wǎng)絡模型預測

          frameWidth = frame.shape[1]
          frameHeight = frame.shape[0]
          inp = cv.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
                                    (0, 0, 0), swapRB=False, crop=False)
          net.setInput(inp)
          out = net.forward()

          6.繪制檢測到人體姿態(tài)關鍵點位置

          points = []
          for i in range(len(BODY_PARTS)):
              # Slice heatmap of corresponging body's part.
              heatMap = out[0, i, :, :]

              # Originally, we try to find all the local maximums. To simplify a sample
              # we just find a global one. However only a single pose at the same time
              # could be detected this way.
              _, conf, _, point = cv.minMaxLoc(heatMap)
              x = (frameWidth * point[0]) / out.shape[3]
              y = (frameHeight * point[1]) / out.shape[2]

              # Add a point if it's confidence is higher than threshold.
              points.append((x, y) if conf > thr else None)

          for pair in POSE_PAIRS:
              partFrom = pair[0]
              partTo = pair[1]
              assert(partFrom in BODY_PARTS)
              assert(partTo in BODY_PARTS)

              idFrom = BODY_PARTS[partFrom]
              idTo = BODY_PARTS[partTo]
              if points[idFrom] and points[idTo]:
                  x1, y1 = points[idFrom]
                  x2, y2 = points[idTo]
                  cv.line(frame, (np.int32(x1), np.int32(y1)), (np.int32(x2), np.int32(y2)), (02550), 3)
                  cv.ellipse(frame, (np.int32(x1), np.int32(y1)), (33), 00360, (00255), cv.FILLED)
                  cv.ellipse(frame, (np.int32(x2), np.int32(y2)), (33), 00360, (00255), cv.FILLED)

          完整的代碼如下:

          import cv2 as cv
          import numpy as np


          dataset = 'MPI'
          if dataset == 'COCO':
              BODY_PARTS = { "Nose"0"Neck"1"RShoulder"2"RElbow"3"RWrist"4,
                             "LShoulder"5"LElbow"6"LWrist"7"RHip"8"RKnee"9,
                             "RAnkle"10"LHip"11"LKnee"12"LAnkle"13"REye"14,
                             "LEye"15"REar"16"LEar"17"Background"18 }

              POSE_PAIRS = [ ["Neck""RShoulder"], ["Neck""LShoulder"], ["RShoulder""RElbow"],
                             ["RElbow""RWrist"], ["LShoulder""LElbow"], ["LElbow""LWrist"],
                             ["Neck""RHip"], ["RHip""RKnee"], ["RKnee""RAnkle"], ["Neck""LHip"],
                             ["LHip""LKnee"], ["LKnee""LAnkle"], ["Neck""Nose"], ["Nose""REye"],
                             ["REye""REar"], ["Nose""LEye"], ["LEye""LEar"] ]
          else:
              assert(dataset == 'MPI')
              BODY_PARTS = { "Head"0"Neck"1"RShoulder"2"RElbow"3"RWrist"4,
                             "LShoulder"5"LElbow"6"LWrist"7"RHip"8"RKnee"9,
                             "RAnkle"10"LHip"11"LKnee"12"LAnkle"13"Chest"14,
                             "Background"15 }

              POSE_PAIRS = [ ["Head""Neck"], ["Neck""RShoulder"], ["RShoulder""RElbow"],
                             ["RElbow""RWrist"], ["Neck""LShoulder"], ["LShoulder""LElbow"],
                             ["LElbow""LWrist"], ["Neck""Chest"], ["Chest""RHip"], ["RHip""RKnee"],
                             ["RKnee""RAnkle"], ["Chest""LHip"], ["LHip""LKnee"], ["LKnee""LAnkle"] ]

          inWidth = 368
          inHeight = 368
          thr = 0.1
          protoc = "D:/projects/pose_body/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
          model = "D:/projects/pose_body/mpi/pose_iter_160000.caffemodel"
          net = cv.dnn.readNetFromCaffe(protoc, model)

          cap = cv.VideoCapture(0)
          height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
          width = cap.get(cv.CAP_PROP_FRAME_WIDTH)
          video_writer = cv.VideoWriter("D:/pose_estimation_demo.mp4", cv.VideoWriter_fourcc('D''I''V''X'), 15, (640480), True)
          while cv.waitKey(1) < 0:
              hasFrame, frame = cap.read()
              if not hasFrame:
                  cv.waitKey()
                  break

              frameWidth = frame.shape[1]
              frameHeight = frame.shape[0]
              inp = cv.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
                                        (000), swapRB=False, crop=False)
              net.setInput(inp)
              out = net.forward()

              print(len(BODY_PARTS), out.shape[0])
              # assert(len(BODY_PARTS) == out.shape[1])

              points = []
              for i in range(len(BODY_PARTS)):
                  # Slice heatmap of corresponging body's part.
                  heatMap = out[0, i, :, :]

                  # Originally, we try to find all the local maximums. To simplify a sample
                  # we just find a global one. However only a single pose at the same time
                  # could be detected this way.
                  _, conf, _, point = cv.minMaxLoc(heatMap)
                  x = (frameWidth * point[0]) / out.shape[3]
                  y = (frameHeight * point[1]) / out.shape[2]

                  # Add a point if it's confidence is higher than threshold.
                  points.append((x, y) if conf > thr else None)

              for pair in POSE_PAIRS:
                  partFrom = pair[0]
                  partTo = pair[1]
                  assert(partFrom in BODY_PARTS)
                  assert(partTo in BODY_PARTS)

                  idFrom = BODY_PARTS[partFrom]
                  idTo = BODY_PARTS[partTo]
                  if points[idFrom] and points[idTo]:
                      x1, y1 = points[idFrom]
                      x2, y2 = points[idTo]
                      cv.line(frame, (np.int32(x1), np.int32(y1)), (np.int32(x2), np.int32(y2)), (02550), 3)
                      cv.ellipse(frame, (np.int32(x1), np.int32(y1)), (33), 00360, (00255), cv.FILLED)
                      cv.ellipse(frame, (np.int32(x2), np.int32(y2)), (33), 00360, (00255), cv.FILLED)

              t, _ = net.getPerfProfile()
              freq = cv.getTickFrequency() / 1000
              cv.putText(frame, '%.2fms' % (t / freq), (1020), cv.FONT_HERSHEY_SIMPLEX, 0.5, (000))
              # video_writer.write(frame);
              # cv.imwrite("D:/pose.png", frame)
              cv.imshow('OpenPose using OpenCV', frame)

          運行結(jié)果如下:


          好消息,小白學視覺團隊的知識星球開通啦,為了感謝大家的支持與厚愛,團隊決定將價值149元的知識星球現(xiàn)時免費加入。各位小伙伴們要抓住機會哦!


          下載1:OpenCV-Contrib擴展模塊中文版教程
          在「小白學視覺」公眾號后臺回復:擴展模塊中文教程即可下載全網(wǎng)第一份OpenCV擴展模塊教程中文版,涵蓋擴展模塊安裝、SFM算法、立體視覺、目標跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實戰(zhàn)項目52講
          小白學視覺公眾號后臺回復:Python視覺實戰(zhàn)項目即可下載包括圖像分割、口罩檢測、車道線檢測、車輛計數(shù)、添加眼線、車牌識別、字符識別、情緒檢測、文本內(nèi)容提取、面部識別等31個視覺實戰(zhàn)項目,助力快速學校計算機視覺。

          下載3:OpenCV實戰(zhàn)項目20講
          小白學視覺公眾號后臺回復:OpenCV實戰(zhàn)項目20講即可下載含有20個基于OpenCV實現(xiàn)20個實戰(zhàn)項目,實現(xiàn)OpenCV學習進階。

          交流群


          歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動駕駛、計算攝影、檢測、分割、識別、醫(yī)學影像、GAN算法競賽等微信群(以后會逐漸細分),請掃描下面微信號加群,備注:”昵稱+學校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進入相關微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~


          瀏覽 15
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆av影院 | 高清无码视频在线免费观看 | 激情A学生妹 | 性做久久久久久久 | 万影网五月天成人网 |