基于OpenCV+MediaPipe的手勢識別

【精選】【優秀課設】基于OpenCV+MediaPipe的手勢識別(數字、石頭剪刀布等手勢識別)_石頭剪刀布opencv識別代碼_網易獨家音樂人Mike Zhou的博客-CSDN博客

import cv2
import mediapipe as mp
import mathdef vector_2d_angle(v1, v2):'''求解二維向量的角度'''v1_x = v1[0]v1_y = v1[1]v2_x = v2[0]v2_y = v2[1]try:angle_ = math.degrees(math.acos((v1_x * v2_x + v1_y * v2_y) / (((v1_x ** 2 + v1_y ** 2) ** 0.5) * ((v2_x ** 2 + v2_y ** 2) ** 0.5))))except:angle_ = 65535.if angle_ > 180.:angle_ = 65535.return angle_def hand_angle(hand_):'''獲取對應手相關向量的二維角度,根據角度確定手勢'''angle_list = []# ---------------------------- thumb 大拇指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[2][0])), (int(hand_[0][1]) - int(hand_[2][1]))),((int(hand_[3][0]) - int(hand_[4][0])), (int(hand_[3][1]) - int(hand_[4][1]))))angle_list.append(angle_)# ---------------------------- index 食指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[6][0])), (int(hand_[0][1]) - int(hand_[6][1]))),((int(hand_[7][0]) - int(hand_[8][0])), (int(hand_[7][1]) - int(hand_[8][1]))))angle_list.append(angle_)# ---------------------------- middle 中指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[10][0])), (int(hand_[0][1]) - int(hand_[10][1]))),((int(hand_[11][0]) - int(hand_[12][0])), (int(hand_[11][1]) - int(hand_[12][1]))))angle_list.append(angle_)# ---------------------------- ring 無名指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[14][0])), (int(hand_[0][1]) - int(hand_[14][1]))),((int(hand_[15][0]) - int(hand_[16][0])), (int(hand_[15][1]) - int(hand_[16][1]))))angle_list.append(angle_)# ---------------------------- pink 小拇指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[18][0])), (int(hand_[0][1]) - int(hand_[18][1]))),((int(hand_[19][0]) - int(hand_[20][0])), (int(hand_[19][1]) - int(hand_[20][1]))))angle_list.append(angle_)return angle_listdef h_gesture(angle_list):'''# 二維約束的方法定義手勢# fist five gun love one six three thumbup yeah'''thr_angle = 65.  # 手指閉合則大于這個值(大拇指除外)thr_angle_thumb = 53.  # 大拇指閉合則大于這個值thr_angle_s = 49.  # 手指張開則小于這個值gesture_str = "Unknown"if 65535. not in angle_list:if (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "0"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "1"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "2"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] > thr_angle):gesture_str = "3"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "4"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "5"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "6"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "8"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Pink Up"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Thumb Up"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Fuck"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "Princess"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Bye"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Spider-Man"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Rock'n'Roll"return gesture_strdef detect():mp_drawing = mp.solutions.drawing_utilsmp_hands = mp.solutions.handshands = mp_hands.Hands(static_image_mode=False,max_num_hands=1,min_detection_confidence=0.75,min_tracking_confidence=0.75)cap = cv2.VideoCapture(0)while True:ret, frame = cap.read()frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)frame = cv2.flip(frame, 1)results = hands.process(frame)frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)if results.multi_handedness:for hand_label in results.multi_handedness:hand_jugg = str(hand_label).split('"')[1]print(hand_jugg)cv2.putText(frame, hand_jugg, (50, 200), 0, 1.3, (0, 0, 255), 2)if results.multi_hand_landmarks:for hand_landmarks in results.multi_hand_landmarks:mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)hand_local = []for i in range(21):x = hand_landmarks.landmark[i].x * frame.shape[1]y = hand_landmarks.landmark[i].y * frame.shape[0]hand_local.append((x, y))if hand_local:angle_list = hand_angle(hand_local)gesture_str = h_gesture(angle_list)print(gesture_str)cv2.putText(frame, gesture_str, (50, 100), 0, 1.3, (0, 0, 255), 2)cv2.imshow('MediaPipe Hands', frame)if cv2.waitKey(1) & 0xFF == 27:breakcap.release()cv2.destroyAllWindows()if __name__ == '__main__':detect()

代碼的解釋請參考

【精選】【優秀課設】基于OpenCV+MediaPipe的手勢識別(數字、石頭剪刀布等手勢識別)_石頭剪刀布opencv識別代碼_網易獨家音樂人Mike Zhou的博客-CSDN博客

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/167562.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/167562.shtml
英文地址,請注明出處:http://en.pswp.cn/news/167562.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HIVE SQL 判斷空值函數

目錄 nvl()coalesce() nvl() select nvl(null,2);輸出&#xff1a;2 select nvl(,2);輸出&#xff1a;‘’ coalesce() select coalesce(null,2);輸出&#xff1a;2 select coalesce(,2);輸出&#xff1a;‘’ select coalesce(null,null,2);輸出&#xff1a;2 *coalesc…

Maxwell安裝部署消費到kafka集群

1.上傳安裝包到linux系統上面 2.解壓安裝包到安裝目錄下&#xff0c;并且重命名 [rootVM-4-10-centos package]# tar -zxvf maxwell-1.29.2.tar.gz -C /opt/software/3.配置mysql 增加以下配置 #數據庫id server-id 1 #啟動binlog&#xff0c;該參數的值會作為binlog的文件…

分布式鎖詳解

文章目錄 分布式鎖1. [傳統鎖回顧](https://blog.csdn.net/qq_45525848/article/details/134608044?csdn_share_tail%7B%22type%22:%22blog%22,%22rType%22:%22article%22,%22rId%22:%22134608044%22,%22source%22:%22qq_45525848%22%7D)1.1. 從減庫存聊起1.2. 環境準備1.3. 簡…

leetcode每日一題32

82.刪除排序鏈表中的重復元素 主要問題是沒有頭節點&#xff0c;以及要刪除所有的相等元素&#xff0c;不是留下一個 那么首先要建立一個頭節點&#xff0c;指向head 而且指針要始終指向要刪除的節點的前一個節點 ListNode* pre new ListNode(0,head);在搜索的過程中&#x…

Handler系列-Message是怎么重復利用的

1.Message類的支持 使用鏈表來緩存Message&#xff0c;sPool為表頭&#xff1b;最多能緩存50個Message&#xff1b;sPoolSync用來保證讀寫鏈表的安全&#xff1b; public final class Message implements Parcelable {private static Message sPool; //緩存的列表表頭/*packa…

98、Text2Room: Extracting Textured 3D Meshes from 2D Text-to-Image Models

簡介 github 利用預訓練的2D文本到圖像模型來合成來自不同姿勢的一系列圖像。為了將這些輸出提升為一致的3D場景表示&#xff0c;將單目深度估計與文本條件下的繪畫模型結合起來&#xff0c;提出了一個連續的對齊策略&#xff0c;迭代地融合場景幀與現有的幾何形狀&#xff0…

#Js篇:單線程模式同步任務異步任務任務隊列事件循環setTimeout() setInterval()

單線程模式 之所以采用單線程&#xff0c;而不是多線程&#xff0c;跟歷史有關系。原因是不想讓瀏覽器變得太復雜&#xff0c;因為多線程需要共享資源、且有可能修改彼此的運行結果&#xff0c;對于一種網頁腳本語言來說&#xff0c;太復雜了。 好處 實現起來比較簡單&#…

nginx國密ssl測試

文章目錄 文件準備編譯部署nginx申請國密數字證書配置證書并測試 文件準備 下載文件并上傳到服務器&#xff0c;這里使用centos 7.8 本文涉及的程序文件已打包可以直接下載。 點擊下載 下載國密版openssl https://www.gmssl.cn/gmssl/index.jsp 下載穩定版nginx http://n…

訪問者模式 (Visitor Pattern)

定義 訪問者模式&#xff08;Visitor Pattern&#xff09;是一種行為型設計模式&#xff0c;用于將算法與其作用于的對象結構分離。這種模式主要用于執行操作或應用過程&#xff0c;這些操作需要在不同類型的對象上執行&#xff0c;同時避免讓這些對象的類變得過于復雜。 關鍵…

【Python 訓練營】N_5 斐波那契數列

題目 輸出斐波那契數列 分析 斐波那契數列&#xff08;Fibonacci sequence&#xff09;&#xff0c;又稱黃金分割數列&#xff0c;指的是這樣一個數列&#xff1a;0、1、1、2、3、5、8、13、21、34、……。 在數學上&#xff0c;費波那契數列是以遞歸的方法來定義&#xff…

9.9 Windows驅動開發:內核遠程線程實現DLL注入

在筆者上一篇文章《內核RIP劫持實現DLL注入》介紹了通過劫持RIP指針控制程序執行流實現插入DLL的目的&#xff0c;本章將繼續探索全新的注入方式&#xff0c;通過NtCreateThreadEx這個內核函數實現注入DLL的目的&#xff0c;需要注意的是該函數在微軟系統中未被導出使用時需要首…

用XMind2TestCase,測試更輕松

&#x1f4e2;專注于分享軟件測試干貨內容&#xff0c;歡迎點贊 &#x1f44d; 收藏 ?留言 &#x1f4dd; 如有錯誤敬請指正&#xff01;&#x1f4e2;交流討論&#xff1a;歡迎加入我們一起學習&#xff01;&#x1f4e2;資源分享&#xff1a;耗時200小時精選的「軟件測試」資…

C++ Qt QByteArray用法介紹

作者:令狐掌門 技術交流QQ群:675120140 csdn博客:https://mingshiqiang.blog.csdn.net/ 文章目錄 一、QByteArray的基本用法1、初始化和賦值2、訪問和修改元素3、 常用方法4、數據轉換二、QByteArray與文件操作三、QByteArray與網絡編程四、QByteArray數據編碼1、Base64 編解…

數據庫-MySQL之數據庫必知必會10-13章

第10章 創建計算字段 拼接字段 使用Concat()函數 執行算術計算 示例&#xff1a;從 Products 表中返回 prod_id、prod_price 和 sale_price。sale_price 是一個包含促銷價格的計算字段。提示&#xff1a;可以乘以 0.9&#xff0c;得到原價的 90%&#xff08;即 10%的折扣&…

2023.11.24 海豚調度,postgres庫使用

目錄 海豚調度架構dolphinscheduler DAG(Directed Acyclic Graph)&#xff0c; 個人自用啟動服務 DS的架構(海豚調度) 海豚調度架構dolphinscheduler 注:需要先開啟zookeeper服務,才能進行以下操作 通過UI進行工作流的配置操作, 配置完成后, 將其提交執行, 此時執行請求會被…

數組基礎知識

數組基礎&#xff08;不定時更新&#xff09; 數組基礎 數組基礎 &#xff08;1&#xff09;數組是存放在連續內存空間上的相同類型數據的集合。數組可以方便的通過下標索引的方式獲取到下標下對應的數據。數組下標都是從0開始的。數組內存空間的地址是連續的。 &#xff08;…

【科普知識】什么是步進電機?

德國百格拉公司于1973年發明了五相混合式步進電機及其驅動器&#xff0c;1993年又推出了性能更加優越的三相混合式步進電機。我國在80年代以前&#xff0c;一直是反應式步進電機占統治地位&#xff0c;混合式步進電機是80年代后期才開始發展。 步進電機是一種用電脈沖信號進行…

Verilog基礎:時序調度中的競爭(一)

相關閱讀 Verilog基礎https://blog.csdn.net/weixin_45791458/category_12263729.html?spm1001.2014.3001.5482 作為一個硬件描述語言&#xff0c;Verilog HDL常常需要使用語句描述并行執行的電路&#xff0c;但其實在仿真器的底層&#xff0c;這些并行執行的語句是有先后順序…

機器學習數據集整理:圖像、表格

前言 如果你對這篇文章感興趣&#xff0c;可以點擊「【訪客必讀 - 指引頁】一文囊括主頁內所有高質量博客」&#xff0c;查看完整博客分類與對應鏈接。 表格數據 Sklearn 提供了 13 個表格型數據&#xff0c;且數據處理接口統一&#xff1b;LIBSVM 提供了 131 個表格型數據&a…

【TypeScript】常見數據結構與算法(二):鏈表

文章目錄 鏈表結構&#xff08;LinkedList&#xff09;鏈表以及數組的缺點數組鏈表的優勢 什么是鏈表?封裝鏈表相關方法源碼鏈表常見面試題237-刪除鏈表中的節點206 - 反轉鏈表 數組和鏈表的復雜度對比 鏈表結構&#xff08;LinkedList&#xff09; 鏈表以及數組的缺點 鏈表…