# Mediapipe # 手部辨識 # prg1 # Open webcam import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if ret: cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg2 # Open Webcam # 手部關鍵點辨識 # import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg3 # import cv2 import mediapipe as mp # open webcam # 手部關鍵點辨識 # 繪製手部關鍵點 cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: mpDraw.draw_landmarks(img, handLms) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg4 # # open webcam # 手部關鍵點辨識 # 繪製手部關鍵點 # 繪製手部關鍵點連線 import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg5 # # open webcam # 手部關鍵點辨識 # 繪製手部關鍵點 # 繪製手部關鍵點連線 # 設定手部關鍵點顏色&線條顏色屬性 import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils # 設定點的顏色屬性 handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=5) # 設定線的顏色屬性 handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=10) while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg6 # 列印所有手部關鍵點座標 import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils # 設定點的顏色屬性 handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=5) # 設定線的顏色屬性 handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=10) while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) # 列印出所有手部關鍵點座標 (x, y) for i, lm in enumerate(handLms.landmark): print(i, lm.x, lm.y) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg7 # import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils # 設定點的顏色屬性 handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=5) # 設定線的顏色屬性 handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=10) while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 取得影像的高度與寬度 # 影像高度 imgHeight = img.shape[0] # 影像寬度 imgWidth = img.shape[1] # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) # 列印出所有手部關鍵點座標 (x, y) for i, lm in enumerate(handLms.landmark): # 重新計算座標值 xPos = int(lm.x * imgWidth) yPos = int(lm.y * imgHeight) # 列印在畫面上 cv2.putText(img, str(i), (xPos-25, yPos+5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,0,255, 2)) # 列印座標 print(i, xPos, yPos) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg8 # 指定大拇指畫圓 import cv2 import mediapipe as mp # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils # 設定點的顏色屬性 handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=5) # 設定線的顏色屬性 handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=10) while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 取得影像的高度與寬度 # 影像高度 imgHeight = img.shape[0] # 影像寬度 imgWidth = img.shape[1] # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) # 列印出所有手部關鍵點座標 (x, y) for i, lm in enumerate(handLms.landmark): # 重新計算座標值 xPos = int(lm.x * imgWidth) yPos = int(lm.y * imgHeight) # 列印在畫面上 cv2.putText(img, str(i), (xPos-25, yPos+5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,0,255, 2)) # 列印大拇指 if i == 4: cv2.circle(img, (xPos, yPos), 10, (0, 0, 255)) # 列印座標 print(i, xPos, yPos) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break # ------------------------------------------------------------ # prg9 # 新增fps數值&顯示 # import cv2 import mediapipe as mp import time # open webcam cap = cv2.VideoCapture(0) # mpHands = mp.solutions.hands hands = mpHands.Hands() # 透過drawing_utils屬性來繪圖 mpDraw = mp.solutions.drawing_utils # 設定點的顏色屬性 handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=5) # 設定線的顏色屬性 handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=10) # 新增時間變數 currentTime = 0 endTime = 0 while True: ret, img = cap.read() if ret: # 把影像轉換成RGB格式,並存入imgRGB變數 imgRGB= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 取得影像的高度與寬度 # 影像高度 imgHeight = img.shape[0] # 影像寬度 imgWidth = img.shape[1] # 透過Mediapipe中的hands.process分析手部資訊,存入result變數 result = hands.process(imgRGB) # 透過multi_hand_landmarks屬性,印出手部的關鍵座標 print(result.multi_hand_landmarks) if result.multi_hand_landmarks: for handLms in result.multi_hand_landmarks: # 僅繪製手部關鍵點 # mpDraw.draw_landmarks(img, handLms) # 繪製手部關鍵點與連線 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) # 列印出所有手部關鍵點座標 (x, y) for i, lm in enumerate(handLms.landmark): # 重新計算座標值 xPos = int(lm.x * imgWidth) yPos = int(lm.y * imgHeight) # 列印在畫面上 cv2.putText(img, str(i), (xPos-25, yPos+5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,0,255, 2)) # 列印大拇指 if i == 4: cv2.circle(img, (xPos, yPos), 20, (255, 0, 0), cv2.FILLED) # 列印座標 print(i, xPos, yPos) currentTime = time.time() fps = 1/(currentTime-endTime) endTime = currentTime cv2.putText(img, f"FPS : {int(fps)}", (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3) cv2.imshow('img', img) if cv2.waitKey(1) == ord('q'): cap.release() cv2.destroyAllWindows() break