如何使用手势添加右键单击虚拟鼠标?

时间:2018-01-24 01:52:11

标签: python

你能帮我解决一下我的代码吗?我应该把我的左键代码放在脚本里面?

场景:当看到2绿色通过线连接时,线的中心是鼠标指针。哪个是Open Gesture。它移动鼠标。 场景2(左键单击):当它是关闭手势时,它执行左键单击。当2绿色成为一个时。

我的问题是我不知道在哪里为我的右键单击添加代码。请帮忙。

这是完整的代码。

import cv2
import numpy as np
#FOR MOUSE CLICK AND MOVEMENT:
from pynput.mouse import Button, Controller 
#TO GET SCREEN SIZE IMPORT THIS:    
#import wx                                      
from PySide import QtGui

#mouse object
mouse = Controller()

#app = QtGui.QApplication([])
#screen_resolution = app.desktop().screenGeometry()
#sx, sy = screen_resolution.width(), screen_resolution.height()

sx, sy = (2800, 768)


#camera will capture an image in this resolution                    
(camx,camy)=(640,480)                          

#BOUNDARY VALUES FOR GREEN: Hue, Saturation, Value
lowerBound=np.array([33,80,40])                 
upperBound=np.array([102,255,255])

#DEFINE THE CAMERA:
cam = cv2.VideoCapture(0)
#TO SET THE RESOLUTION                      
cam.set(9,camx)                                 
cam.set(16,camy)

#USE FOR REMOVING EXCESS SMALL PIXELS (noise):
kernelOpen=np.ones((5,5))                       
kernelClose=np.ones((20,20))

mLocOld=np.array([0,0])
mouseLoc=np.array([0,0])
#should be between > 1-0
DampingFactor=2                                 

pinchFlag=0
openx,openy,openw,openh=(0,0,0,0)

#mouseLoc=mLocOld+(targetLoc-mLocOld)/DampingFactor

while True:
#CAPTURE FRAME-BY-FRAME:
ret, img=cam.read()     

frame=cv2.flip(img ,1)                 
#img=cv2.resize(img,(340,220))
#CONVERT BGR to HSV:
imgHSV= cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)    
#CREATE THE FILTER/MASK: will only filter the green portion of the image
mask=cv2.inRange(imgHSV,lowerBound,upperBound)  

#MORPHOLOGY: Pixels removal
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)   
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)

maskFinal=maskClose

图像,conts,H = cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

#Detect if Open gesture or close. If 2 objects, means 2 contours and its open position.
if(len(conts)==2):                                      
    if(pinchFlag==1):
        pinchFlag=0
        mouse.release(Button.left)
    mouse.release(Button.left)
    x1,y1,w1,h1=cv2.boundingRect(conts[0])
    x2,y2,w2,h2=cv2.boundingRect(conts[1])
    x
    #Draw bounding box
    cv2.rectangle(frame,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)    
    cv2.rectangle(frame,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
    #connect to Squarea:
    cx1=x1+w1/2
    cy1=y1+h1/2
    cx2=x2+w2/2
    cy2=y2+h2/2
    #find the center of the line
    cx=(cx1+cx2)/2
    cy=(cy1+cy2)/2
    cv2.line(frame, (cx1,cy1),(cx2,cy2),(255,0,0),2)
    cv2.circle(frame,(cx,cy),2,(0,0,255),3)

    mouseLoc=mLocOld+((cx,cy)-mLocOld)/DampingFactor

    #mouse Part
    mouse.position=(mouseLoc[0]*sx/camx,mouseLoc[1]*sy/camy)
    while mouse.position!=(mouseLoc[0]*sx/camx,mouseLoc[1]*sy/camy):
        pass

    mLocOld=mouseLoc
    openx,openy,openw,openh=cv2.boundingRect(np.array([[[x1,y1],[x1+w1,y1+h1],[x2,y2],[x2+w2,y2+h2]]]))
    #cv2.rectangle(img,(openx,openy),(openx+openw,openy+openh),(255,0,0),2)

elif(len(conts)==1):
    x,y,w,h=cv2.boundingRect(conts[0])
    if(pinchFlag==0):
        if(abs((w*h-openw*openh)*100/(w*h))<20):

            pinchFlag=1
            mouse.press(Button.left)
            openx,openy,openw,openh=(0,0,0,0)

    #if one green is view the mouse wont move
    else:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        cx=x+w/2
        cy=y+h/2
        cv2.circle(frame,(cx,cy),(w+h)/4,(0,0,255),2)
        mouseLoc=mLocOld+((cx,cy)-mLocOld)/DampingFactor

    #mouse Part
        mouse.position=(mouseLoc[0]*sx/camx,mouseLoc[1]*sy/camy)
        while mouse.position!=(mouseLoc[0]*sx/camx,mouseLoc[1]*sy/camy):
            pass

        mLocOld=mouseLoc
    #Dragging Mouse
    #mouse.press(Button.right)

#cv2.imshow("maskClose",maskClose)
#cv2.imshow("maskOpen",maskOpen)
#cv2.imshow("mask",mask)

#DISPLAY THE RESULTING FRAME:
cv2.imshow("cam",frame)
cv2.waitKey(5)

0 个答案:

没有答案