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!
==== 下面這支程式會: ==== * 顯示 21 個 landmark + 編號 * 在畫面左上角顯示: - 食指角度(度數)+「伸直/彎曲/介於中間」 - 拇指尖~食指尖距離+是否「Pinch」 <syntaxhighlight lang="python">from cvzone.HandTrackingModule import HandDetector import cv2 import math === ===== 小工具:距離與角度 ===== === def dist2d(a, b): """計算 2D 距離(不使用 math.dist,支援 Python 3.7)""" return ((a[0] - b[0]) ''' 2 + (a[1] - b[1]) ''' 2) ** 0.5 def angle_between(p1, p2, p3): """ 計算夾角 ∠p1-p2-p3(以 p2 為頂點) 回傳角度(degree) """ # 向量 v1 = p1 - p2, v2 = p3 - p2 v1 = (p1[0] - p2[0], p1[1] - p2[1]) v2 = (p3[0] - p2[0], p3[1] - p2[1]) # 內積 v1·v2 dot = v1[0] '' v2[0] + v1[1] '' v2[1] # 長度 |v1|, |v2| len1 = math.sqrt(v1[0] ''' 2 + v1[1] ''' 2) len2 = math.sqrt(v2[0] ''' 2 + v2[1] ''' 2) if len1 == 0 or len2 == 0: return 0.0 cosang = dot / (len1 * len2) # 避免浮點數誤差造成超出 [-1, 1] cosang = max(-1.0, min(1.0, cosang)) ang = math.degrees(math.acos(cosang)) return ang === ===== CVZone 初始化 ===== === cap = cv2.VideoCapture(0) # 如有需要可改成 cv2.VideoCapture(0, cv2.CAP_DSHOW) detector = HandDetector(detectionCon=0.7, maxHands=1) while True: success, img = cap.read() if not success: break hands, img = detector.findHands(img) # img 會自動畫骨架 info_lines = [] # 要顯示在畫面左上的文字行 if hands: hand = hands[0] lmList = hand["lmList"] # 21 個 [x,y,z] handType = hand["type"] # "Left" or "Right" # --- 把 21 點畫出來 + 編號(可當教學用)--- for i, lm in enumerate(lmList): x, y, z = lm cv2.circle(img, (x, y), 5, (0, 0, 255), cv2.FILLED) cv2.putText(img, str(i), (x + 5, y - 5), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 0), 1) # ===== 任務 A:判斷食指彎曲程度 ===== # 食指關節點:MCP=5, PIP=6, DIP=7 p_mcp = lmList[5] p_pip = lmList[6] p_dip = lmList[7] ang_index = angle_between(p_mcp, p_pip, p_dip) # 簡單規則: # 角度 > 150 度 → 幾乎筆直 # 角度 < 120 度 → 明顯彎曲 # 介於中間 → 半彎 if ang_index > 150: index_state = "伸直" elif ang_index < 120: index_state = "彎曲" else: index_state = "介於直/彎之間" info_lines.append("食指角度: {:.1f} 度 ({})".format(ang_index, index_state)) # ===== 任務 B:判斷拇指尖 + 食指尖距離(Pinch or not) ===== thumb_tip = lmList[4] # 拇指尖 index_tip = lmList[8] # 食指尖 d_thumb_index = dist2d(thumb_tip, index_tip) # 簡單門檻:距離 < 40 pixel 視為 Pinch # (實際可視鏡頭解析度調整) if d_thumb_index < 40: pinch_state = "Pinch (捏住)" else: pinch_state = "分開" info_lines.append("拇指尖-食指尖距離: {:.1f} ({})".format(d_thumb_index, pinch_state)) # 也可印在 console 給老師 debug # print(info_lines[0]) # print(info_lines[1]) # ===== 把資訊畫到畫面左上角 ===== y0 = 30 for line in info_lines: cv2.putText(img, line, (10, y0), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) y0 += 25 cv2.imshow("Hand 21 Landmarks - 運算思維示範版", img) 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)