python code請進
import cv2 import mediapipe as mp print(cv2.__version__) print(mp.__version__)
mp_drawing = mp.solutions.drawing_utils # mediapipe 繪圖方法 mp_drawing_styles = mp.solutions.drawing_styles mp_hands = mp.solutions.hands # mediapipe 偵測手
cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) if not cap.isOpened(): print("Cannot open camera") exit()hands=mp_hands.Hands( model_complexity=0, max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5)while True: ret, im = cap.read() if not ret: print("Fail to receive frame") break im2 = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) # 將 BGR 轉換成 RGB results = hands.process(im2) # 偵測手掌 if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: # 繪製到影像中 mp_drawing.draw_landmarks( im, hand_landmarks, mp_hands.HAND_CONNECTIONS, mp_drawing_styles.get_default_hand_landmarks_style(), mp_drawing_styles.get_default_hand_connections_style()) cv2.imshow('hands', im) if cv2.waitKey(1)>0: break # 按任意鍵停止 cap.release() cv2.destroyAllWindows() hands.close()
沒有留言:
張貼留言