通过某个手势动作控制窗口中方块位置移动。
电脑上安装一个摄像头就可以实现。
话不多说,直接上代码。
import cv2
from cvzone.HandTrackingModule import HandDetector
import cvzone
import numpy as np
cap =cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
detector=HandDetector(detectionCon=0.8)
colorR=(255,0,255)
cx,cy,w ,h =100,100,200,200
class DragRect():
def __init__(self,posCenter,size=[200,200]):
self.posCenter = posCenter
self.size= size
# cv2.rectangle(img, (cx - w // 2, cy - h // 2), (cx + w // 2, cy + h // 2), colorR, cv2.FILLED)
def update(self,cursor):
cx,cy=self.posCenter
w,h=self.size
#if the index finger tip is in the rectangle region
if cx - w // 2 < cursor[0] < cx + w // 2 and cy - h // 2 < cursor[1] < cy + h // 2:
self.posCenter=cursor
rectLsit=[]
for x in range(5):
rectLsit.append(DragRect([x*250+150,150]))
print( rectLsit)
while True:
success, img =cap.read()
img=cv2.flip(img,1)
_,img=detector.findHands(img)
lmList=detector.findPosition(img)
if lmList:
l,_,_=detector.findDistance(lmList[8],lmList[12],img)
if l<30:
cursor = lmList[8]
for rect in rectLsit:
rect.update(cursor)
#Draw Transperency
imgNew = np.zeros_like(img,np.uint8)
for rect in rectLsit:
cx, cy = rect.posCenter
w, h = rect.size
cv2.rectangle(imgNew,(cx-w//2,cy-h//2),(cx+w//2,cy+h//2),colorR,cv2.FILLED)
cvzone.cornerRect(imgNew,(cx-w//2, cy-h//2,w,h),20,rt=0)
out = img.copy()
alpha = 0.5
mask=imgNew.astype(bool)
out[mask] = cv2.addWeighted(img,alpha,imgNew,1-alpha,0)[mask]
# cv2.rectangle(img, (350, 100), (450, 450), (255, 0, 255), cv2.FILLED)
cv2.imshow('image',out)
cv2.waitKey(1)#1代表更新,0只显示第一帧