OpenCV + gym-retro:输入图像中的通道数无效
OpenCV + gym-retro: Invalid number of channels in input image
一直在摆弄 gym-retro 和 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, (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\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
更新:
这是缩小图像的输出。好像有颜色。
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 行需要什么对于?
一直在摆弄 gym-retro 和 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, (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\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
更新: 这是缩小图像的输出。好像有颜色。 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 行需要什么对于?