這篇文章主要介紹OpenCV如何利用手勢識別實現虛擬拖放效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
通過opencv設置顯示框和調用攝像頭顯示當前畫面
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
while True:
succes, img = cap.read()
cv2.imshow("Image", img)
cv2.waitKey(1)在當前畫面中找到手,本文將使用cv zone中的手跟蹤模塊
from cvzone.HandTrackingModule import HandDetector detector = HandDetector(detectionCon=0.8)#更改了默認的置信度,讓其檢測更加準確
找到手的完整代碼
import cv2
from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
detector = HandDetector(detectionCon=0.8)
while True:
succes, img = cap.read()
detector.findHands(img)
lmList, _ = detector.findPosition(img)
cv2.imshow("Image", img)
cv2.waitKey(1)
第三步首先創建一個方塊
cv2.rectangle(img, (100,100), (300,300), (0, 0 , 255),cv2.FILLED)

然后檢測我們的食指有沒有進入到這個方框中,如果進入的話,這個方框就改變顏色
if lmList: cursor = lmList[8] if 100<cursor[0]<300 and 100<cursor[1]<300: colorR =0, 255, 0 else: colorR = 0,0,255 cv2.rectangle(img, (100,100), (300,300), colorR,cv2.FILLED)

然后檢測我們是否點擊這個方框
當我們食指的之間在這個方框的中心,就會跟隨為我們的指尖運動。

但是這樣的話,我們不想這個方塊跟隨我,我就得很快的將手移開,不是很方便。
所以我們要模擬鼠標點擊確定是否選中它,所以我們就在加入了一根中指來作為判斷,那判斷的依據就是中指和食指指尖的距離。
l,_,_ = detector.findDistance(8,12,img)

假設倆指尖的距離小于30就選中,大于30就取消
if l<30: cursor = lmList[8] if cx-w//2<cursor[0]<cx+w//2 and cy-h//2<cursor[1]<cy+h//2: colorR =0, 255, 0 cx, cy = cursor else: colorR = 0,0,255
import cv2
from cvzone.HandTrackingModule import HandDetector
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
colorR =(0, 0, 255)
detector = HandDetector(detectionCon=0.8)
cx, cy, w, h= 100, 100, 200, 200
while True:
succes, img = cap.read()
img = cv2.flip(img, 1)
detector.findHands(img)
lmList, _ = detector.findPosition(img)
if lmList:
l,_,_ = detector.findDistance(8,12,img)
print(l)
if l<30:
cursor = lmList[8]
if cx-w//2<cursor[0]<cx+w//2 and cy-h//2<cursor[1]<cy+h//2:
colorR =0, 255, 0
cx, cy = cursor
else:
colorR = 0,0,255
cv2.rectangle(img, (cx-w//2,cy-h//2), (cx+w//2,cy+h//2), colorR,cv2.FILLED)
cv2.imshow("Image", img)
cv2.waitKey(1)以上是“OpenCV如何利用手勢識別實現虛擬拖放效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。