demo--基于手势实现视频交互

在这里插入图片描述

通过某个手势动作控制窗口中方块位置移动。
电脑上安装一个摄像头就可以实现。
话不多说,直接上代码。

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只显示第一帧
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值