Search code examples
pythonpyautoguiopenai-gym

Receiving a TypeError: Required argument 'object' (pos 1) not found


I'm trying to create an gym environment that moves the mouse (in a VM, obviously)... I don't know much about classes, but is there supposed to be an argument for self or something...? Also, any improvements would be greatly appreciated...

This code is basically going to run on a VM, soo...I've tried to remove the line of code, but there's several lines that don't run... (I'm terrible at explaining things)

Here's the code:

class MouseEnv(Env):
    def __init__(self):
        self.ACC = 0
        self.reward = 0
        self.done = False
        self.reset()

    def step(self, action):
        try:
            self.action = action
            done = False

            if self.action == 1:
                pyautogui.click()
                self.reward += 0.2
            else:
                if self.ACC == 1:
                    self.action = min((self.action/100), 1) * 1920
                    self.prev_action = min((self.prev_action/100), 1) * 1080
                    self.reward += 0.4

                else:
                    self.ACC = 1
                    self.prev_action = self.action()
                    self.reset()
            screen = ImageGrab.grab()
            self.observation = np.array(screen)
        except:
            done = True
        return self.observation, self.reward, done, {}           
    def reset(self):
        self.observation = np.array()
        self.reward = 0
        self.done = 0
        return self.observation

And the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rees/.local/lib/python3.6/site-packages/gym/envs/registration.py", line 171, in make
    return registry.make(id, **kwargs)
  File "/home/rees/.local/lib/python3.6/site-packages/gym/envs/registration.py", line 123, in make
    env = spec.make(**kwargs)
  File "/home/rees/.local/lib/python3.6/site-packages/gym/envs/registration.py", line 87, in make
    env = cls(**_kwargs)
  File "/home/rees/Desktop/gym-mouse/MouseGym/envs/mouse_env.py", line 12, in __init__
    self.reset()
  File "/home/rees/Desktop/gym-mouse/MouseGym/envs/mouse_env.py", line 41, in reset
    self.observation = np.array()
TypeError: Required argument 'object' (pos 1) not found

Expected Result:

I expect the mouse to move based on the agent's input/actions, and the observation to be a live video feed of the screen...


Solution

  • The issue is that in your reset function, when you try to initialize self.observation with an empty numpy array, you do not pass any arguments to np.array(). You have two options here, pass an empty list to the function:

    self.observation = np.array([])
    

    or use np.empty, which will create an empty numpy array:

    self.observation = np.empty(0)