Spaces:
Running
Running
Commit ·
7b60f61
1
Parent(s): e87a61a
Add labels.txt to gesture classification and a new hand recognition project for 張君雅
Browse files
projects/得能2026專題班/張君雅/手勢特徵分類/labels.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 背景
|
| 2 |
+
1 喝水中
|
| 3 |
+
2 其他動作
|
projects/得能2026專題班/張君雅/手部辨識/hand_landmarker.task
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fbc2a30080c3c557093b5ddfc334698132eb341044ccee322ccf8bcf3607cde1
|
| 3 |
+
size 7819105
|
projects/得能2026專題班/張君雅/手部辨識/main.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
from mediapipe.tasks import python
|
| 4 |
+
from mediapipe.tasks.python import vision
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# ============================================
|
| 8 |
+
# Week 9 - Step 2: MediaPipe 手部追蹤 (Tasks API)
|
| 9 |
+
# ============================================
|
| 10 |
+
|
| 11 |
+
# 初始化 MediaPipe Hand Landmarker
|
| 12 |
+
model_path = os.path.join(os.path.dirname(__file__), 'hand_landmarker.task')
|
| 13 |
+
base_options = python.BaseOptions(model_asset_path=model_path)
|
| 14 |
+
options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
|
| 15 |
+
detector = vision.HandLandmarker.create_from_options(options)
|
| 16 |
+
|
| 17 |
+
cap = cv2.VideoCapture(0)
|
| 18 |
+
|
| 19 |
+
while True:
|
| 20 |
+
success, img = cap.read()
|
| 21 |
+
if not success:
|
| 22 |
+
break
|
| 23 |
+
|
| 24 |
+
img = cv2.flip(img, 1)
|
| 25 |
+
|
| 26 |
+
# 1. OpenCV 的 BGR 轉為 RGB
|
| 27 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 28 |
+
|
| 29 |
+
# 2. 將 Numpy 陣列轉為 MediaPipe Image 物件
|
| 30 |
+
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=img_rgb)
|
| 31 |
+
|
| 32 |
+
# 3. 進行偵測
|
| 33 |
+
results = detector.detect(mp_image)
|
| 34 |
+
|
| 35 |
+
# 4. 如果有偵測到手,把 21 個關鍵點都畫出來
|
| 36 |
+
if results.hand_landmarks:
|
| 37 |
+
for hand_lms in results.hand_landmarks:
|
| 38 |
+
h, w, c = img.shape
|
| 39 |
+
|
| 40 |
+
# 迴圈畫出 21 個節點
|
| 41 |
+
for i, lm in enumerate(hand_lms):
|
| 42 |
+
cx, cy = int(lm.x * w), int(lm.y * h)
|
| 43 |
+
|
| 44 |
+
# 畫出小綠點標示每個關節
|
| 45 |
+
cv2.circle(img, (cx, cy), 5, (0, 255, 0), -1)
|
| 46 |
+
|
| 47 |
+
# 特別把食指尖 (Landmark 8) 標示為大紅點,並印出座標
|
| 48 |
+
if i == 8:
|
| 49 |
+
cv2.circle(img, (cx, cy), 12, (0, 0, 255), -1)
|
| 50 |
+
print(f"食指(8) 座標:({cx}, {cy})")
|
| 51 |
+
|
| 52 |
+
cv2.imshow("Hand Tracking", img)
|
| 53 |
+
|
| 54 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
cap.release()
|
| 58 |
+
cv2.destroyAllWindows()
|