Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/692fcfd8-4b44-800f-a184-fa098175a6d5
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== 請將整段貼到一個檔案,例如 taiwan_hand_number_1_10.py: ==== <syntaxhighlight lang="python">from cvzone.HandTrackingModule import HandDetector import cv2 import os import numpy as np === ========= 小工具:2D 距離(取代 math.dist,支援 Python 3.7) ========= === def euclidean(a, b): """ 計算 2D 歐幾里得距離 a, b: [x, y, (z)],這裡只用前兩個值 """ return ((a[0] - b[0])'''2 + (a[1] - b[1])'''2)**0.5 === ========= 初始設定 ========= === cap = cv2.VideoCapture(0) # 如有需要可改成 cv2.VideoCapture(0, cv2.CAP_DSHOW) detector = HandDetector(detectionCon=0.7, maxHands=2) === 中文數字對照(只是讓畫面顯示好看一點) === zh_map = { 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六", 7: "七", 8: "八", 9: "九", 10: "十", } === =========(可選)讀取手勢示意圖:gestures/1.png ~ 10.png ========= === gesture_images = {} for n in range(1, 11): path = os.path.join("gestures", f"{n}.png") if os.path.exists(path): img = cv2.imread(path) if img is not None: gesture_images[n] = img else: # 沒有圖也沒關係,只是少了右側示意圖 pass === ========= 單手台灣數字手勢 1~9 判斷 ========= === def classify_single_hand_number(hand): """ 根據單手手勢判斷 1~9(台灣數字手勢) 使用 fingersUp + 一點點關節角度判斷(區分 1 / 9) 回傳 (number, label, fingersPattern) """ fingers = detector.fingersUp(hand) # [thumb, index, middle, ring, pinky] pattern = tuple(fingers) # 固定 pattern 的 2~8(不含 1,因為 1 和 9 pattern 一樣,要特判) mapping = { (0, 1, 1, 0, 0): 2, # 食 + 中 (1, 1, 1, 0, 0): 3, # 拇 + 食 + 中 (0, 1, 1, 1, 1): 4, # 四指伸出,拇指收 (1, 1, 1, 1, 1): 5, # 五指全伸 (1, 0, 0, 0, 1): 6, # 拇 + 小(台灣 6) (1, 1, 0, 0, 1): 7, # 拇 + 食 + 小(台灣 7 的一種) (1, 1, 0, 0, 0): 8, # 拇 + 食(手槍,台灣 8) } # 先吃 mapping 裡的(2~8) if pattern in mapping: n = mapping[pattern] return n, str(n), fingers # ---- 只伸食指 → 可能是 1 或 9 ---- if pattern == (0, 1, 0, 0, 0): lm = hand["lmList"] # 食指:MCP = 5, PIP = 6, DIP = 7, TIP = 8 mcp = lm[5] pip = lm[6] dip = lm[7] tip = lm[8] # 直線距離 MCP→TIP dist_mcp_tip = euclidean(mcp, tip) # 折線距離 MCP→PIP→DIP→TIP dist_mcp_pip = euclidean(mcp, pip) dist_pip_dip = euclidean(pip, dip) dist_dip_tip = euclidean(dip, tip) poly_len = dist_mcp_pip + dist_pip_dip + dist_dip_tip # 比例 > 1.25 → 手指彎得比較明顯 → 當作 9 if dist_mcp_tip > 0 and poly_len / dist_mcp_tip > 1.25: return 9, "9", fingers else: return 1, "1", fingers # 其他 pattern 不判斷 return None, "Unknown", fingers === ========= 判斷整體數字(考慮 10) ========= === def classify_number(hands): """ hands: HandDetector 回傳的 hands 陣列 回傳 (number, label, bbox_for_draw) """ if not hands: return None, "No Hand", None # 若有兩手 → 有機會判斷「10」 if len(hands) >= 2: results = [] for h in hands[:2]: n, _, _ = classify_single_hand_number(h) results.append(n) # 這裡定義:兩隻手都比「5」 → 視為 10 if results[0] == 5 and results[1] == 5: bbox = hands[0]["bbox"] return 10, "10", bbox # 只有一手,或兩手但不符合 10 → 以第一隻手為主 main_hand = hands[0] bbox = main_hand["bbox"] number, label, _ = classify_single_hand_number(main_hand) return number, label, bbox === ========= 主迴圈 ========= === while True: success, img = cap.read() if not success: break hands, img = detector.findHands(img) number, label, bbox = classify_number(hands) h, w, _ = img.shape # 右側示意面板(可省略,不想要的話可以整塊刪掉) panel_width = 300 panel = 255 * np.ones((h, panel_width, 3), dtype="uint8") if number in zh_map: # 若有圖就貼圖 if number in gesture_images: g = gesture_images[number] gh, gw, _ = g.shape scale = min((h '' 0.6) / gh, (panel_width '' 0.9) / gw) nw = int(gw * scale) nh = int(gh * scale) g2 = cv2.resize(g, (nw, nh)) xo = (panel_width - nw) // 2 yo = 20 panel[yo:yo + nh, xo:xo + nw] = g2 # 底下寫上「阿拉伯 + 中文」 ty = int(h * 0.8) cv2.putText(panel, f"{number}", (20, ty), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 0), 2) cv2.putText(panel, f"中文:{zh_map[number]}", (20, ty + 35), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2) else: cv2.putText(panel, "請比出 1~10 的手勢", (20, h // 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (100, 100, 100), 2) # 在原影像上畫框&數字 if bbox and number: x, y, bw, bh = bbox cv2.rectangle(img, (x, y), (x + bw, y + bh), (0, 255, 0), 2) if number in zh_map: text = f"{number} ({zh_map[number]})" else: text = label cv2.putText(img, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # 合併畫面:左邊攝影機,右邊示意 panel = cv2.resize(panel, (panel_width, h)) combined = cv2.hconcat([img, panel]) cv2.imshow("Taiwan Hand Number (1-10)", combined) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)