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!
=== 直接複製即可執行: === <syntaxhighlight lang="python"># hand_gesture_1_10_no_sound.py === Python 3.7 無聲版:手勢 1~10 辨識+教學面板(無語音、無 pygame) === from cvzone.HandTrackingModule import HandDetector import cv2 import os import numpy as np === ========= 小工具:2D 距離 ========= === def euclidean(a, b): return ((a[0] - b[0])'''2 + (a[1] - b[1])'''2)**0.5 === ========= 初始設定 ========= === cap = cv2.VideoCapture(0) detector = HandDetector(detectionCon=0.7, maxHands=2) zh_map = { 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六", 7: "七", 8: "八", 9: "九", 10: "十", } === ========= 讀取手勢示意圖 ========= === 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: print(f"[提示] 找不到手勢圖片 gestures/{n}.png") === ========= 單手手勢判斷(1~9) ========= === def classify_single_hand_number(hand, fingers): pattern = tuple(fingers) mapping = { (0, 1, 1, 0, 0): (2, "2"), (1, 1, 1, 0, 0): (3, "3"), (0, 1, 1, 1, 1): (4, "4"), (1, 1, 1, 1, 1): (5, "5"), (1, 0, 0, 0, 1): (6, "6"), (1, 1, 0, 0, 1): (7, "7"), (1, 1, 0, 0, 0): (8, "8"), } if pattern in mapping: return mapping[pattern] # ---- 特判 1 / 9:只伸食指 ---- if pattern == (0, 1, 0, 0, 0): lm = hand["lmList"] mcp = lm[5] pip = lm[6] dip = lm[7] tip = lm[8] dist_mcp_tip = euclidean(mcp, tip) poly = ( euclidean(mcp, pip) + euclidean(pip, dip) + euclidean(dip, tip) ) if dist_mcp_tip > 0 and poly / dist_mcp_tip > 1.25: return (9, "9") else: return (1, "1") return (None, "None") === ========= 整體判斷(含 10) ========= === def classify_number(hands): if not hands: return None, "No Hand", None # 判斷 10(兩手都是 1) if len(hands) >= 2: nums = [] for h in hands[:2]: f = detector.fingersUp(h) n, _ = classify_single_hand_number(h, f) nums.append(n) if nums[0] == 1 and nums[1] == 1: return 10, "10", hands[0]["bbox"] # 判斷 1~9 main = hands[0] bbox = main["bbox"] fingers = detector.fingersUp(main) number, label = classify_single_hand_number(main, fingers) 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 = 320 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.75) cv2.putText(panel, str(number), (30, ty), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 0), 2) cv2.putText(panel, f"中文:{zh_map[number]}", (30, ty + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 0), 2) else: cv2.putText(panel, "請比出 1~10 的手勢", (20, h // 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (80, 80, 80), 2) # 偵測框 if bbox and number: x, y, bw, bh = bbox cv2.rectangle(img, (x, y), (x + bw, y + bh), (0, 255, 0), 2) cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # 合併畫面 panel = cv2.resize(panel, (panel_width, h)) combined = cv2.hconcat([img, panel]) cv2.imshow("Hand Number Teaching (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)