国产无码免费,人妻口爆,国产V在线,99中文精品7,国产成人无码AA精品一,制度丝袜诱惑av,久久99免费麻辣视频,蜜臀久久99精品久久久久久酒店
        訂閱
        糾錯
        加入自媒體

        一文了解如何使用Python+OpenCV構建手部跟蹤系統

        2021-07-21 10:24
        磐創AI
        關注

        OpenCV 是一個用于計算機視覺應用程序的庫。在 OpenCV 的幫助下,我們可以構建大量實時運行更好的應用程序。主要用于圖像和視頻處理?梢栽诖颂帿@取有關 OpenCV 的更多信息

        除了 OpenCV,我們將使用 MediaPipe 庫。MediaPipeMediaPipe是一個主要用于構建音頻、視頻或任何時間序列數據的框架。在 MediaPipe 框架的幫助下,我們可以為不同的媒體處理功能構建管道。MediaPipe 的一些主要應用。多手追蹤人臉檢測對象檢測和跟蹤Objection:3D 對象檢測和跟蹤AutoFlip:自動視頻裁剪管道等。

        手地標模型

        MediaPipe 使用單次手掌檢測模型,一旦完成,它會對檢測到的手部區域中的 21 個 3D 手掌坐標執行精確的關鍵點定位。MediaPipe 管道使用多個模型,例如,從完整圖像返回定向手邊界框的手掌檢測模型。裁剪后的圖像區域被饋送到由手掌檢測器定義的手部標志模型,并返回高保真 3D 手部關鍵點,F在讓我們實現手部跟蹤模型。安裝所需的模塊–> pip install opencv-python–> pip install mediapipe首先,讓我們檢查網絡攝像頭的工作情況。import cv2
        import time
        cap = cv2.VideoCapture(0)
        pTime = 0
        while True:
           success, img = cap.read()
           imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
           cTime = time.time()
           fps = 1 / (cTime - pTime)
           pTime = cTime
           cv2.putText(img, f'FPS:{int(fps)}', (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
           cv2.imshow("Test", img)
        cv2.waitKey(1)

        如果任何網絡攝像頭連接到你的 PC,上面的代碼將彈出一個窗口,并在輸出窗口的左上角顯示每秒幀數 (fps),F在讓我們開始實施。導入所需的模塊并初始化所需的變量。import cv2
        import mediapipe as mp
        import time
        cap = cv2.VideoCapture(0)
        mpHands = mp.solutions.hands
        hands = mpHands.Hands(static_image_mode=False,
                             max_num_hands=2,
                             min_detection_confidence=0.5,
                             min_tracking_confidence=0.5)
        mpDraw = mp.solutions.drawing_utils
        pTime = 0
        cTime = 0
        在上面這段代碼中,我們在mp.solutions.hand 中聲明了一個名為“hands”的對象來檢測手部,默認情況下,查看類“ Hands() ”內部,要檢測的手部數量設置為2、最小檢測置信度設置為0.5,最小跟蹤置信度設置為0.5。我們將使用mpDraw繪制關鍵點,F在讓我們編寫一個 while 循環來執行我們的代碼。while True:
           success, img = cap.read()
           imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
           results = hands.process(imgRGB)
           #print(results.multi_hand_landmarks)
           if results.multi_hand_landmarks:
               for handLms in results.multi_hand_landmarks:
                   for id, lm in enumerate(handLms.landmark):
                       #print(id,lm)
                       h, w, c = img.shape
                       cx, cy = int(lm.x *w), int(lm.y*h)
                       #if id ==0:
                       cv2.circle(img, (cx,cy), 3, (255,0,255), cv2.FILLED)
                   mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
           cTime = time.time()
           fps = 1/(cTime-pTime)
           pTime = cTime
           cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)
           cv2.imshow("Image", img)
           cv2.waitKey(1)
        在上面的代碼中,我們從網絡攝像頭讀取幀并將圖像轉換為 RGB。然后我們在“ hands.process()” 函數的幫助下檢測幀中的手。一旦檢測到手,我們將找到關鍵點,然后使用cv2.circle突出顯示關鍵點中的點,并使用mpDraw.draw_landmarks連接關鍵點。整個代碼如下import cv2
        import mediapipe as mp
        import time
        cap = cv2.VideoCapture(0)
        mpHands = mp.solutions.hands
        hands = mpHands.Hands(static_image_mode=False,
                             max_num_hands=2,
                             min_detection_confidence=0.5,
                             min_tracking_confidence=0.5)
        mpDraw = mp.solutions.drawing_utils
        pTime = 0
        cTime = 0
        while True:
           success, img = cap.read()
           imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
           results = hands.process(imgRGB)
           #print(results.multi_hand_landmarks)
           if results.multi_hand_landmarks:
               for handLms in results.multi_hand_landmarks:
                   for id, lm in enumerate(handLms.landmark):
                       #print(id,lm)
                       h, w, c = img.shape
                       cx, cy = int(lm.x *w), int(lm.y*h)
                       #if id ==0:
                       cv2.circle(img, (cx,cy), 3, (255,0,255), cv2.FILLED)
                   mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
           cTime = time.time()
           fps = 1/(cTime-pTime)
           pTime = cTime
           cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)
           cv2.imshow("Image", img)
           cv2.waitKey(1)
        輸出是:

        手部追蹤模型輸出現在讓我們創建一個手部跟蹤模塊,以便我們可以在其他項目中使用它。創建一個新的 python 文件,首先讓我們創建一個名為handDetector的類,其中有兩個成員函數,名為findHands 和findPosition。函數findHands將接受一個 RGB 圖像并檢測幀中的手并定位關鍵點,繪制地標,函數findPosition 將給出手的位置和 id。然后是我們初始化模塊的 main 函數,我們還編寫了一個 while 循環來運行模型。你可以在此處將此設置或模塊導入到任何其他相關項目作品中。整個代碼如下import cv2
        import mediapipe as mp
        import time
        class handDetector():
           def __init__(self, mode = False, maxHands = 2, detectionCon = 0.5, trackCon = 0.5):
               self.mode = mode
               self.maxHands = maxHands
               self.detectionCon = detectionCon
               self.trackCon = trackCon
               self.mpHands = mp.solutions.hands
               self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.detectionCon, self.trackCon)
               self.mpDraw = mp.solutions.drawing_utils
               
           def findHands(self,img, draw = True):
               imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
               self.results = self.hands.process(imgRGB)
               # print(results.multi_hand_landmarks)
               if self.results.multi_hand_landmarks:
                   for handLms in self.results.multi_hand_landmarks:
                       if draw:
                           self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
               return img
           def findPosition(self, img, handNo = 0, draw = True):
               lmlist = []
               if self.results.multi_hand_landmarks:
                   myHand = self.results.multi_hand_landmarks[handNo]
                   for id, lm in enumerate(myHand.landmark):
                       h, w, c = img.shape
                       cx, cy = int(lm.x * w), int(lm.y * h)
                       lmlist.append([id, cx, cy])
                       if draw:
                           cv2.circle(img, (cx, cy), 3, (255, 0, 255), cv2.FILLED)
               return lmlist
        def main():
           pTime = 0
           cTime = 0
           cap = cv2.VideoCapture(0)
           detector = handDetector()
           while True:
               success, img = cap.read()
               img = detector.findHands(img)
               lmlist = detector.findPosition(img)
               if len(lmlist) != 0:
                   print(lmlist[4])
               cTime = time.time()
               fps = 1 / (cTime - pTime)
               pTime = cTime
               cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
               cv2.imshow("Image", img)
               cv2.waitKey(1)
        if __name__ == "__main__":
           main()
        輸出將與上面顯示的相同,以及被跟蹤的手的位置。


        聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權或其他問題,請聯系舉報。

        發表評論

        0條評論,0人參與

        請輸入評論內容...

        請輸入評論/評論長度6~500個字

        您提交的評論過于頻繁,請輸入驗證碼繼續

        暫無評論

        暫無評論

          掃碼關注公眾號
          OFweek人工智能網
          獲取更多精彩內容
          文章糾錯
          x
          *文字標題:
          *糾錯內容:
          聯系郵箱:
          *驗 證 碼:

          粵公網安備 44030502002758號

          主站蜘蛛池模板: 隆回县| 欧美人妖性爱| 97精品视频| 亚洲精品97久久一| 伊人免费| 91在线无码精品秘?国产千人斩| 亚洲AV日韩AV永久无码网站| 乌审旗| 成人三级精品| 影音先锋91| 国产玖玖| AV天堂免费观看| 海宁市| 欧美阿v视频| 91成人无码| 欧美丝袜另类| 国内视频自拍| 一本大道东京热无码va在线播放| 日本在线观看| 天天干天天色综合网| 中文日韩欧美| 革吉县| va精品在线| 国产传媒AV| 日韩大香蕉| 亚洲成人国产| 于都县| 狠狠干狠狠爱| 久草在线资源| 国内老熟妇对白XXXXHD| 怡春院综合| 熟妇人妻系列| 铁岭县| 玖玖精品视频| 欧美另类3| 中文字幕日韩有码| 成人无码免费毛片A片| 青青草99| 巴楚县| 日韩精品人妻| 无码h片|