首页 > 解决方案 > Python3 cv2: 错误: (-215) scn == 3 || 函数 cv::cvtColor 中的 scn == 4 仅在脚本文件中

问题描述

此错误error: (-215) scn == 3 || scn == 4 in function cv::cvtColor仅在我的脚本中,但如果我在 IPython shell 中运行代码,一切正常。该代码用于减少运行的 DQN(Deeq-Q-network)的尺寸,openai-gym我不知道为什么这不在我的脚本中而是在我的 shell 中。
如果出现错误,我添加了一条评论。如果需要,可以添加完整的代码。
部分代码:

def preprocess(self,inp):
    inp=cv2.resize(inp,(84, 110))
    inp=cv2.cvtColor(inp, cv2.COLOR_BGR2GRAY)
    inp=inp[26:110,:]
    ret,obs=cv2.threshold(inp,1,255,cv2.THRESH_BINARY)
    return np.reshape(obs,(84,84,1))

def Q_Learning(self,modell_path="Your path here"):
    observation=self.env.reset()
    for i in range(self.itertime):
        observation=self.preprocess(observation)#!Error appers here!
        if np.random.rand()<=self.random_move_prop:
            action=np.random.randint(low=0,high=self.env.action_space.n)
        else:
            action=self.model.predict(observation)
            action=np.argmax(action)
        new_observation, reward, done, info=self.env.step(action)
        new_observation=self.preprocess(new_observation)
        self.storage.append([observation,action,reward,new_observation,done])
        observation=new_observation
        if done:
            self.env.reset()

标签: python-3.xopencvopenai-gym

解决方案


推荐阅读