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">from cvzone.HandTrackingModule import HandDetector import cv2 import math === ===== 小工具:距離與角度 ===== === def dist2d(a, b): """計算 2D 距離(支援 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[0] - p2[0], p1[1] - p2[1]) v2 = (p3[0] - p2[0], p3[1] - p2[1]) dot = v1[0] '' v2[0] + v1[1] '' v2[1] 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) cosang = max(-1.0, min(1.0, cosang)) ang = math.degrees(math.acos(cosang)) return ang === ===== CVZone 初始化 ===== === cap = cv2.VideoCapture(0) detector = HandDetector(detectionCon=0.7, maxHands=1) while True: success, img = cap.read() if not success: break hands, img = detector.findHands(img) info_lines = [] if hands: hand = hands[0] lmList = hand["lmList"] # 每個 landmark 是 [x,y] 或 [x,y,z] handType = hand["type"] # --- 畫 21 個點(自動支援 2D/3D)--- for i, lm in enumerate(lmList): x = lm[0] y = lm[1] # 若有 z 就取,不然給 0 z = lm[2] if len(lm) > 2 else 0 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:食指彎曲偵測 ===== # 食指節點:5,6,7 p_mcp = lmList[5] p_pip = lmList[6] p_dip = lmList[7] # 取 x,y(無論 2D 或 3D) p_mcp2 = (p_mcp[0], p_mcp[1]) p_pip2 = (p_pip[0], p_pip[1]) p_dip2 = (p_dip[0], p_dip[1]) ang_index = angle_between(p_mcp2, p_pip2, p_dip2) 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:拇指尖 - 食指尖距離 ===== thumb_tip = lmList[4] index_tip = lmList[8] d_thumb_index = dist2d( (thumb_tip[0], thumb_tip[1]), (index_tip[0], index_tip[1]) ) if d_thumb_index < 40: pinch = "Pinch (捏住)" else: pinch = "分開" info_lines.append("拇指尖-食指尖: {:.1f} ({})".format(d_thumb_index, pinch)) # ===== 在畫面左上角顯示結果 ===== 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("21 Landmarks - 運算思維示範版(2D/3D 自動支援)", 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)