欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV利用手勢識(shí)別實(shí)現(xiàn)虛擬拖放效果

 更新時(shí)間:2022年01月14日 10:08:43   作者:hax8124  
這篇文章主要介紹了利用OpenCV實(shí)現(xiàn)手勢識(shí)別,從而進(jìn)行虛擬拖放效果,我們可以使用這個(gè)技術(shù)實(shí)現(xiàn)一些游戲,控制機(jī)械臂等很多有趣的事情。感興趣的可以學(xué)習(xí)一下

本文將實(shí)現(xiàn)一些通過手拖放一些框,我們可以使用這個(gè)技術(shù)實(shí)現(xiàn)一些游戲,控制機(jī)械臂等很多有趣的事情。

第一步

通過opencv設(shè)置顯示框和調(diào)用攝像頭顯示當(dāng)前畫面

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)

第二步

在當(dāng)前畫面中找到手,本文將使用cv zone中的手跟蹤模塊

from cvzone.HandTrackingModule import HandDetector

detector = HandDetector(detectionCon=0.8)#更改了默認(rèn)的置信度,讓其檢測更加準(zhǔn)確

找到手的完整代碼

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)

第三步

第三步首先創(chuàng)建一個(gè)方塊

cv2.rectangle(img, (100,100), (300,300), (0, 0 , 255),cv2.FILLED)

然后檢測我們的食指有沒有進(jìn)入到這個(gè)方框中,如果進(jìn)入的話,這個(gè)方框就改變顏色 

? 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)

然后檢測我們是否點(diǎn)擊這個(gè)方框

當(dāng)我們食指的之間在這個(gè)方框的中心,就會(huì)跟隨為我們的指尖運(yùn)動(dòng)。

但是這樣的話,我們不想這個(gè)方塊跟隨我,我就得很快的將手移開,不是很方便。

所以我們要模擬鼠標(biāo)點(diǎn)擊確定是否選中它,所以我們就在加入了一根中指來作為判斷,那判斷的依據(jù)就是中指和食指指尖的距離。

l,_,_ = detector.findDistance(8,12,img)

假設(shè)倆指尖的距離小于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)

到此這篇關(guān)于OpenCV利用手勢識(shí)別實(shí)現(xiàn)虛擬拖放效果的文章就介紹到這了,更多相關(guān)OpenCV手勢識(shí)別 虛擬拖放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論