Mouse.position из pynput не работает [python2, opencv, mac, jupyter] - PullRequest
0 голосов
/ 20 января 2019

Я новичок в этом, так что это может быть просто проблема синтаксиса, но кто-нибудь может выяснить, почему строка 77 mouse.position = (x,y) не двигает мою мышь?Он должен быть сопоставлен с точкой, нарисованной зеленым объектом на моей веб-камере.

Кроме того, когда я представляю while mouse.position!=(x,y): pass, камера зависает, когда появляется зеленый объект.

Код здесь (ошибки не отображаются при зависании):

import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx

mouse=Controller()

#get monitor size
app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()

print sx, sy

#output window size
(camx,camy)=(320,240)

#set filter limits
lowerBound=np.array([33,80,40])
upperBound=np.array([102,255,255])

#initialise cam
cam= cv2.VideoCapture(0)
cam.set(3,camx)
cam.set(4,camy)

#mask parameters
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))

while True:
    ret, img=cam.read()
    img=cv2.resize(img,(340,220))

    #convert BGR to HSV
    imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    #create the Mask
    mask=cv2.inRange(imgHSV,lowerBound,upperBound)

    #morphology
    maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
    maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)

    maskFinal=maskClose

    #Find contours
    conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    #For two contours (open):
    if(len(conts)==2):
        x1,y1,w1,h1=cv2.boundingRect(conts[0])
        x2,y2,w2,h2=cv2.boundingRect(conts[1])
        cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
        cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)

        #finding points in middle of rectangles
        cx1=x1+w1/2
        cy1=y1+h1/2
        cx2=x2+w2/2
        cy2=y2+h2/2

        #find centre of line
        cx=(cx1+cx2)/2
        cy=(cy1+cy2)/2

        #create line between centres of contours
        cv2.line(img,(cx1,cy1),(cx2,cy2),(255,0,0),2)

        #create dot in middle of line
        cv2.circle(img, (cx,cy),2,(0,0,255),2)

        #to check values
        print (cx*sx/camx)
        print (cy*sy/camy)

        #move mouse to dot cx,cy (scaled for monitor)
        mouse.position = (cx*sx/camx,cy*sy/camy)
        while mouse.position != (cx*sx/camx,cy*sy/camy):
            pass

    #For one contour (closed):
    elif (len(conts)==1):
        x,y,w,h= cv2.boundingRect(conts[0])

        #draw single rectangle around 'clicked' object
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        #show when clicked with large circle
        cx=x+w/2
        cy=y+h/2
        cv2.circle(img,(cx,cy),(w+h)/4,(0,0,255),2)

        #set mouse to dot cx,cy       
        mouse.position = (cx*sx/camx,cy*sy/camy)
        while mouse.position != (cx*sx/camx,cy*sy/camy):
            pass

    #cv2.imshow("maskClose",maskClose)
    #cv2.imshow("mask",mask)
    cv2.imshow("cam",img)
    cv2.waitKey(5)

Я ценю любую помощь!

1 Ответ

0 голосов
/ 22 января 2019

Моя проблема заключалась в том, что мне нужно было разрешить терминалу и jupyter разрешать доступ к управлению мышью (Mac Mojave 10.14.2).

Причина зависания заключается в том, что он застрял в цикле while, нос помощью mouse.position теперь все хорошо!

...