OpenCV+gymretro:输入图像中的通道数无效

2024-05-18 08:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

一直在玩健身房复古和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\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

更新: 这是缩小图像的输出。似乎有颜色。 snippet of observation window


Tags: inpyimportenvconfigevalnpcv2
1条回答
网友
1楼 · 发布于 2024-05-18 08:19:20

您正在创建一个具有形状(inx,iny)的cv2对象

   ob = cv2.resize(ob, (inx,iny)) # 1
   ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY) # 2

cv2.COLOR\u BGR2GRAY希望彩色图像具有形状(inx,iny,3),因此请检查您的“ob”需要什么形状,第2行用于什么

相关问题 更多 >