[英]invalid syntax when importing ROM in gym-retro (google colab)
[英]OpenCV + gym-retro: Invalid number of channels in input image
一直在使用健身房复古和 OpenCV。 我一直在查看其他代码示例和教程。 其中一些似乎以相同的方式编码,但是当我这样做时,我收到以下错误。 是否有某种类型的更新或什么? 欢迎任何有关修复的建议。 我可以注释掉重塑和转换为灰度并且它有效。 但是,我向我的神经网络提供了太多信息。
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, (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.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,-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 行做什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.