您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Python怎么使用opencv進(jìn)行手勢識別,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
首先先進(jìn)行手部的檢測,找到之后會做Hand Landmarks。
將手掌的21個點(diǎn)找到,然后我們就可以通過手掌的21個點(diǎn)的坐標(biāo)推測出來手勢,或者在干什么。
第一安裝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()#讀取當(dāng)前數(shù)據(jù) if ret: cv2.imshow('img',img)#顯示當(dāng)前讀取到的畫面 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
這樣我們就能再電腦上顯示我們的手部關(guān)鍵點(diǎn)和坐標(biāo)了,對于手勢識別或者別的操作就可以通過獲取到的關(guān)鍵點(diǎn)的坐標(biāo)進(jìn)行判斷了。
''' @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ū)域面積進(jìn)行排序 # contourmax = contour[0][:, 0, :]#保留區(qū)域面積最大的輪廓點(diǎn)坐標(biāo) 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ù)自己的電腦進(jìn)行調(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) # 進(jìn)行膚色檢測 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()
關(guān)于“Python怎么使用opencv進(jìn)行手勢識別”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。