【问题标题】:why is showing a error AttributeError: 'list' object has no attribute 'shape'为什么显示错误 AttributeError: 'list' object has no attribute 'shape'
【发布时间】:2020-10-17 14:31:33
【问题描述】:

在这里,我正在尝试获取真实图像之间的结构相似性 (SSIM),但出现错误。 如何解决这个问题? 代码应该在哪里改?

真实图片代码:

    def frame_1(ori_id):
        frame_1=[]
        for i in zip(ori_id):
            ori_id = os.path.join(root_dir,"{}.mp4".format(','.join(i)))
            
            #ori_id means original video

            capture_ori = cv2.VideoCapture(ori_id)
            frames_num = int(capture_ori.get(cv2.CAP_PROP_FRAME_COUNT)) 
            for j in range(frames_num):
                capture_ori.grab()
                if j % 10 != 0:
                    continue
                success, frame_1 = capture_ori.retrieve()
                #print(frame_1)
                if not success:
                    continue
                id = os.path.splitext(os.path.basename(ori_id))[0]
                frame_1=[i for i in zip(frame_1)]
                frame_1.append(frame_1)
                 
        return frame_1

这里的ori_id表示root原始video_path:

    frame_1=frame_1(ori_id)

假货图片代码:

    def frame_2(fake_id):    
        frame_2=[]
        for i in zip(fake_id):
            fake_id = os.path.join(root_dir,"{}.mp4".format(','.join(i)))
            #fake_id means fake video
            capture_fake = cv2.VideoCapture(fake_id)
            frames_num = int(capture_fake.get(cv2.CAP_PROP_FRAME_COUNT))
            #print(frames_num)
            for j in range(frames_num):
                capture_fake.grab()
                if j % 10 != 0:
                    continue
                success, frame_2 = capture_fake.retrieve()
                #print(frame_2)
                if not success:
                    continue
                frame_2=[i for i in zip(frame_2)]
                frame_2.append(frame_2)               
        return frame_2

这里的 fake_id 表示 root fake video_path,

    frame_2=frame_2(fake_id)

对于结构相似性(SSIM)代码:

    d, a = compare_ssim(frame_1,frame_2, multichannel=True, full=True)
    a = 1 - a
    diff = (a * 255).astype(np.uint8)
    diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    diff_path='F:/deepfake_data/ssim_diff.jpg'
    #Here diff_path means save image path
    cv2.imwrite(diff_path, diff)

返回这样的错误:

*AttributeError                            Traceback (most recent call last)
      
<ipython-input-15-9eecfad7aa58> in save_diffs(pair, root_dir)
    147     #..........SSIM..........................
    148     #try:
--> 149     d, a = compare_ssim(frame_1,frame_2, multichannel=True, full=True)
    150     a = 1 - a
    151     diff = (a * 255).astype(np.uint8)

F:\ProgramData\Anaconda3\lib\site-packages\skimage\measure\_structural_similarity.py in compare_ssim(X, Y, win_size, gradient, data_range, multichannel, gaussian_weights, full, **kwargs)
     81 
     82     """
---> 83     if not X.shape == Y.shape:
     84         raise ValueError('Input images must have the same dimensions.')
     85 

AttributeError: 'list' object has no attribute 'shape'*

            

【问题讨论】:

    标签: python python-3.x list function


    【解决方案1】:

    根据This piece of documentation compare_ssim 输入应该是ndarrays。

    解决它的简单方法是转换 ndarrays 中的帧,比方说,在 return 语句中,如下所示:

    return np.array(frame_1)
    

    和

    return np.array(frame_2)
    

    不过,我认为这可能会解决这个特定错误,但我不确定比较一堆随时间变化的图像是否有意义,也许逐个比较会很有用

    [compare_ssim(frame_1[i],frame_2[i]) for i in range(0, min(len(frame_1), len(frame_2)))]
    

    【讨论】:

    • 返回错误 in frame_1(ori_id) 84 id = os.path.splitext(os.path.basename(ori_id))[0] 85 #frame_1= [i for i in zip(frame_1)] ---> 86 frame_1=np.ndarray(frame_1) 87 frame_1.append(frame_1) 88 #frame_1=np.array(frame_1) ValueError: ndarray 支持的最大维度为 32 , 找到 1080
    • 我的错误,np.ndarray 做了完全不同的事情,让我们试试 np.array 代替
    • 再次显示错误,每当我添加 np.array(frame_2) 时,返回一个 AttributeError: 'tuple' object has no attribute 'shape'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多