herrDeng網內搜尋

自訂搜尋

Ads

2023年11月15日 星期三

mediaPipe描繪手掌hands landmarks在本機端python實作練習


python code請進


  1. import cv2
  2. import mediapipe as mp
  3. print(cv2.__version__)
  4. print(mp.__version__)
  1. mp_drawing = mp.solutions.drawing_utils # mediapipe 繪圖方法
  2. mp_drawing_styles = mp.solutions.drawing_styles
  3. mp_hands = mp.solutions.hands # mediapipe 偵測手
  1. cap = cv2.VideoCapture(0)
  2. cap.set(3, 1280)
  3. cap.set(4, 720)
  4.  
  5. if not cap.isOpened():
  6. print("Cannot open camera")
  7. exit()
  8. hands=mp_hands.Hands(
  9.     model_complexity=0,
  10.     max_num_hands=2,
  11.     min_detection_confidence=0.5,
  12.     min_tracking_confidence=0.5)
  13. while True:
  14.     ret, im = cap.read()
  15.     if not ret:
  16.         print("Fail to receive frame")
  17.         break
  18.     im2 = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)   # 將 BGR 轉換成 RGB
  19.     results = hands.process(im2)                 # 偵測手掌
  20.     if results.multi_hand_landmarks:
  21.         for hand_landmarks in results.multi_hand_landmarks:
  22.             # 繪製到影像中
  23.             mp_drawing.draw_landmarks(
  24.                 im, 
  25.                 hand_landmarks,
  26.                 mp_hands.HAND_CONNECTIONS,
  27.                 mp_drawing_styles.get_default_hand_landmarks_style(),
  28.                 mp_drawing_styles.get_default_hand_connections_style())   
  29.     cv2.imshow('hands', im)
  30.     if cv2.waitKey(1)>0: break    # 按任意鍵停止
  31. cap.release()
  32. cv2.destroyAllWindows()
  33. hands.close()

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章