我一直在尝试使用gym-retro和OpenCV。我查看了其他代码示例和教程。很多看起来是用相同的方式编写的,但当我尝试时却得到了以下错误。是否有某些类型的更新或其他原因?欢迎任何修复建议。我可以注释掉重塑和转换为灰度的部分,这样它就能工作了。然而,这样我给神经网络提供的信息就太多了。
import retroimport numpy as npimport cv2import neatimport pickleenv = retro.make('SuperMarioBros3-Nes', '1Player.World1.Level1')def eval_genomes(genomes, config): for genome_id,genome in genomes: ob = env.reset() ac = env.action_space.sample() inx, iny, inc = env.observation_space.shape inx = int(inx/8) iny = int(iny/8) net = neat.nn.recurrent.RecurrentNetwork.create(genome, config) current_max_fitness = 0 fitness_current = 0 frame = 0 counter = 0 xpos = 0 xpos_max = 0 done = False while not done: env.render() frame += 1 #print(ob) ob = cv2.resize(ob, (inx,iny)) ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY) ob = np.reshape(ob, (inx,iny)) imgarray = np.ndarray.flatten(ob) nnOutput = net.activate(imgarray) ob, rew, done, info = env.step(nnOutput) #imgarray.clear()config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,neat.DefaultSpeciesSet,neat.DefaultStagnation,'config-feedforward')p = neat.Population(config)winner = p.run(eval_genomes)
(gameai) C:\Users\dgilk\anaconda3\envs\gameai>python mario.pyTraceback (most recent call last): File "mario.py", line 45, in <module> winner = p.run(eval_genomes) File "C:\Users\dgilk\anaconda3\envs\gameai\lib\site-packages\neat\population.py", line 89, in run fitness_function(list(iteritems(self.population)), self.config) File "mario.py", line 32, in eval_genomes ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)cv2.error: OpenCV(4.4.0) c:\users\appveyor\appdata\local\temp\1\pip-req-build-k8sx3e60\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xbf2c9cd3::Set<1,-1,-1>,struct cv::impl::A0xbf2c9cd3::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'> Invalid number of channels in input image:> 'VScn::contains(scn)'> where> 'scn' is 1
更新:这是缩小图像的输出。似乎有颜色。观察窗口片段
回答:
你在这里创建了一个形状为 (inx,iny) 的cv2对象
ob = cv2.resize(ob, (inx,iny)) # 1 ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY) # 2
cv2.COLOR_BGR2GRAY期望一个颜色图像,其形状应为(inx, iny, 3),所以检查你的’ob’需要什么形状,你需要第2行的代码做什么?