Python如何使用opencv進行手勢識別詳解
前言
本項目是使用了谷歌開源的框架mediapipe,里面有非常多的模型提供給我們使用,例如面部檢測,身體檢測,手部檢測等。
原理

首先先進行手部的檢測,找到之后會做Hand Landmarks。

將手掌的21個點找到,然后我們就可以通過手掌的21個點的坐標推測出來手勢,或者在干什么。
程序部分
第一安裝Opencv
pip install opencv-python
第二安裝mediapipe
pip install mediapipe
程序
先調(diào)用這倆個函數(shù)庫
import cv2 import mediapipe as mp
然后再調(diào)用攝像頭
cap = cv2.VideoCapture(0)
函數(shù)主體部分
while True:
ret, img = cap.read()#讀取當前數(shù)據(jù)
if ret:
cv2.imshow('img',img)#顯示當前讀取到的畫面
if cv2.waitKey(1) == ord('q'):#按q鍵退出程序
break
全部函數(shù)
import cv2
import mediapipe as mp
import time
cap = cv2.VideoCapture(1)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=3)
handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=5)
pTime = 0
cTime = 0
while True:
ret, img = cap.read()
if ret:
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(imgRGB)
# print(result.multi_hand_landmarks)
imgHeight = img.shape[0]
imgWidth = img.shape[1]
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle)
for i, lm in enumerate(handLms.landmark):
xPos = int(lm.x * imgWidth)
yPos = int(lm.y * imgHeight)
# cv2.putText(img, str(i), (xPos-25, yPos+5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
# if i == 4:
# cv2.circle(img, (xPos, yPos), 20, (166, 56, 56), cv2.FILLED)
# print(i, xPos, yPos)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, f"FPS : {int(fps)}", (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
cv2.imshow('img', img)
if cv2.waitKey(1) == ord('q'):
break
這樣我們就能再電腦上顯示我們的手部關鍵點和坐標了,對于手勢識別或者別的操作就可以通過獲取到的關鍵點的坐標進行判斷了。
附另一個手勢識別實例

'''
@Time : 2021/2/6 15:41
@Author : WGS
@remarks :
'''
""" 從視頻讀取幀保存為圖片"""
import cv2
import numpy as np
# cap = cv2.VideoCapture("C:/Users/lenovo/Videos/wgs.mp4") #讀取文件
cap = cv2.VideoCapture(0) # 讀取攝像頭
# 皮膚檢測
def A(img):
YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) # 轉(zhuǎn)換至YCrCb空間
(y, cr, cb) = cv2.split(YCrCb) # 拆分出Y,Cr,Cb值
cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
_, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Ostu處理
res = cv2.bitwise_and(img, img, mask=skin)
return res
def B(img):
# binaryimg = cv2.Canny(Laplacian, 50, 200) #二值化,canny檢測
h = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 尋找輪廓
contour = h[0]
contour = sorted(contour, key=cv2.contourArea, reverse=True) # 已輪廓區(qū)域面積進行排序
# contourmax = contour[0][:, 0, :]#保留區(qū)域面積最大的輪廓點坐標
bg = np.ones(dst.shape, np.uint8) * 255 # 創(chuàng)建白色幕布
ret = cv2.drawContours(bg, contour[0], -1, (0, 0, 0), 3) # 繪制黑色輪廓
return ret
while (True):
ret, frame = cap.read()
# 下面三行可以根據(jù)自己的電腦進行調(diào)節(jié)
src = cv2.resize(frame, (400, 350), interpolation=cv2.INTER_CUBIC) # 窗口大小
cv2.rectangle(src, (90, 60), (300, 300), (0, 255, 0)) # 框出截取位置
roi = src[60:300, 90:300] # 獲取手勢框圖
res = A(roi) # 進行膚色檢測
cv2.imshow("0", roi)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
dst = cv2.Laplacian(gray, cv2.CV_16S, ksize=3)
Laplacian = cv2.convertScaleAbs(dst)
contour = B(Laplacian) # 輪廓處理
cv2.imshow("2", contour)
key = cv2.waitKey(50) & 0xFF
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
總結(jié)
到此這篇關于Python如何使用opencv進行手勢識別的文章就介紹到這了,更多相關Python用opencv手勢識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python 動態(tài)導入對象,importlib.import_module()的使用方法
今天小編就為大家分享一篇Python 動態(tài)導入對象,importlib.import_module()的使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python實現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換
這篇文章主要介紹了python實現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換,RGB與YCbCr顏色空間概念的與變換關系,包括內(nèi)容灰度值和亮度的關系、RGB顏色空間與顏色控制、YCbCr顏色空間及與RGB的變換關系,需要的小伙伴可以參考一下2022-03-03
Python使用Flask Migrate模塊遷移數(shù)據(jù)庫
Flask-Migrate是一個為Flask應用處理SQLAlchemy數(shù)據(jù)庫遷移的擴展,使得可以通過Flask的命令行接口或者Flask-Scripts對數(shù)據(jù)庫進行操作2022-07-07
python 函數(shù)嵌套及多函數(shù)共同運行知識點講解
在本篇文章里小編給各位整理的是一篇關于python 函數(shù)嵌套及多函數(shù)共同運行知識點講解,需要的朋友們可以學習下。2020-03-03

