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!
=== Assistant: OK,我直接在你一開始貼的那個「石頭剪刀布範例」上改, === OK,我直接在你一開始貼的那個「石頭剪刀布範例」上改, 讓 只有拇指+小指伸直([1,0,0,0,1])時顯示數字 6。 關鍵概念: * fingers = detector.fingersUp(hand) 會回傳 [thumb, index, middle, ring, pinky] * 台灣的 6 手勢 = fingers == [1, 0, 0, 0, 1] * 因為這種手勢伸出 2 根手指,所以 要在判斷「2 根手指=剪刀」之前就先特判 6,不然會被當成總數 2 的情況吃掉。 ==== <syntaxhighlight lang="python">from cvzone.HandTrackingModule import HandDetector ==== import cv2 cap = cv2.VideoCapture(0) detector = HandDetector(detectionCon=0.5, maxHands=1) while cap.isOpened(): success, img = cap.read() if not success: break hands, img = detector.findHands(img) msg = "None" if hands: hand = hands[0] bbox = hand["bbox"] fingers = detector.fingersUp(hand) # [thumb, index, middle, ring, pinky] totalFingers = fingers.count(1) print(fingers, " total:", totalFingers) # ★★★ 先判斷台灣 6:只有拇指和小指伸直 ★★★ if fingers == [1, 0, 0, 0, 1]: msg = "6" # 下面是原本的石頭剪刀布邏輯(可保留或改掉) elif totalFingers == 5: msg = "Paper" elif totalFingers == 0: msg = "Rock" elif totalFingers == 2: # 兩根手指,且是食指+中指 才當「剪刀」 if fingers[1] == 1 and fingers[2] == 1: msg = "Scissors" cv2.putText(img, msg, (bbox[0] + 200, bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2) cv2.imshow("Image", img) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() </syntaxhighlight> ===== - if fingers == [1, 0, 0, 0, 1]: msg = "6" 這一段一定要放在「剪刀(totalFingers == 2)」之前。 ===== * 這樣一來: - 只伸拇指+小指 → 顯示 6 - 伸食指+中指 → 顯示 Scissors - 其他部分維持原本的 Rock / Paper 邏輯。 如果你接下來想: * 把 1~5 的手勢也改成「台灣版數字手勢判斷」 * 或乾脆全部改成「1~10 台灣數字手勢」的程式(不玩剪刀石頭布) 跟我說一聲,我可以幫你把完整 1~10 判斷表寫成一個乾淨的 if fingers == ... 模組給你用。
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)