-1

I'm using opencv on windows. This error occurs only after detecting green colour (which obviously is the task of the code). CODE:

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

app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()
(camx,camy)=(640,480)

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

cam= cv2.VideoCapture(0)

kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
pinchFlag=0

while True:
    ret, img=cam.read()
    img=cv2.resize(img,(640,480))

    #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



if(len(conts)==2):
    if(pinchFlag==1):
        pinchFlag=0
        mouse.release(Button.left)
    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)
    cx1=int(x1+w1/2)
    cy1=int(y1+h1/2)
    cx2=int(x2+w2/2)
    cy2=int(y2+h2/2)
    cx=int((cx1+cx2)/2)
    cy=int((cy1+cy2)/2)
    cv2.line(img, (cx1,cy1),(cx2,cy2),(255,0,0),2)
    cv2.circle(img, (cx,cy),2,(0,0,255),2)
    mouseLoc=(sx-(cx*sx/camx), cy*sy/camy)
    mouse.position=mouseLoc 
    while mouse.position!=mouseLoc:
        pass
elif(len(conts)==1):
    x,y,w,h=cv2.boundingRect(conts[0])
    if(pinchFlag==0):
        pinchFlag=1
        mouse.press(Button.left)
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cx=int(x+w/2)
    cy=int(y+h/2)
    cv2.circle(img,(cx,cy),int((w+h)/4),(0,0,255),2)
    mouseLoc=(sx-(cx*sx/camx), cy*sy/camy)
    mouse.position = mouseLoc 
    while mouse.position != mouseLoc:
        pass
cv2.imshow("cam",img)
cv2.waitKey(5)

Error:

Traceback (most recent call last):

  File "virtual_mouse.py", line 65, in <module>
    mouse.position = mouseLoc

File "C:\Users\dell\Anaconda3\envs\kj\lib\site-packages\pynput\mouse\_base.py", line 65, in position
    self._position_set(pos)

File "C:\Users\dell\Anaconda3\envs\kj\lib\site-packages\pynput\mouse\_win32.py", line 66, in _position_set
    self.__SetCursorPos(*pos)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
DavidG
  • 24,279
  • 14
  • 89
  • 82
  • Have you looked into the requirements of `Controller().position`, vs what `mouseLoc` is? – tehhowch Mar 09 '18 at 15:42
  • @tehhowch controller().position takes tuple (x,y) of the pointer of the mouse. And mouseLoc is also giving x,y values. Don't know where I am going wrong? – jkashish1818 Mar 09 '18 at 16:22
  • Have you varied the inputs to `controller.position`? I.e. explicitly pass in various tuples? Verified your `mouseLoc` unpacks as expected by `__SetCursorPos`? – tehhowch Mar 09 '18 at 16:39

1 Answers1

0

If you're using openCV3, then the formula mouseLoc = (sx-(cx*sx/camx), cy*sy/camy) returns float values which are not shown in the iPython console. So you have to convert those into integer values and this will surely work out. Hence do the following changes:

mouseLoc = ( int(sx-(cx*sx/camx)) , int(cy*sy/camy) )
Omar Einea
  • 2,478
  • 7
  • 23
  • 35