问题描述
热衷于复古健身房和OpenCV。我一直在看其他代码示例和教程。他们中的几个似乎以相同的方式编码,但是当我这样做时,出现以下错误。是否进行过某种类型的更新?欢迎提出任何建议。我可以注释一下重塑和转换为灰度的方法,并且可以正常工作。但是后来我向NN提供了太多信息。
import retro
import numpy as np
import cv2
import neat
import pickle
env = 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,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.py
Traceback (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,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
更新: 这是缩小图像的输出。似乎有颜色。 snippet of observation window
解决方法
您要在此处创建形状为(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行需要什么?