繁体   English   中英

FaceAlign AttributeError: 'str' object 没有属性 'shape'

[英]FaceAlign AttributeError: 'str' object has no attribute 'shape'

我正在使用 Face Align 并为每个图像获取以下错误:

AttributeError: 'str' object has no attribute 'shape'

我将此解释为意味着我的代码需要一个图像 object 而是接收一个字符串,对吗?

违规代码:

def getAligns(self,
                img,
                use_cnn = False,
                savepath = None,
                return_info = False):
    """
    get face alignment picture
    :param img: original BGR image or a path to it
    :param use_cnn: using CNN to extract aligned faces, if set, dlib 
                    be compiled with cuda support
    :param savepath: savepath, format "xx/xx/xx.png"
    :param return_info: if set, return face positinos [(x, y, w, h)] 
    :return: aligned faces, (opt) rects
    """
    print(img.shape)
    if type(img) == str:
      try:
          img = cv2.imread(img)
          img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
      except:
          shutil.copy2(img, 'temp.jpg')
          img = cv2.imread('temp.jpg')
          img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
          os.remove('temp.jpg')

来自 Align_Faces 的相关代码:

  if clean: clear_dir(config.aligned)
  os.chdir(config.labeled)
  jobs = glob.glob("*.jpg")
  print(len(jobs))
##  # un-parallel
  for picname in jobs:
      print(picname)
      aligned = FL.getAligns(picname)
      if len(aligned) != 1:
        print(config.aligned)
        print(picname)
        print(aligned[0])
        return cv2.imwrite(config.aligned + picname, aligned[0])

完整的 output: 在此处输入图像描述

查看您的数据流:

jobs = glob.glob("*.jpg")

##  # un-parallel
for picname in jobs:
    print(picname)
    aligned = FL.getAligns(picname)

def getAligns(self,
            img,
            use_cnn = False,
            savepath = None,
            return_info = False):
    print(img.shape)

您发布的 output 表明您已将文件名保留为字符串0_0_ErinMurphy.jpg ,并将该字符串传递给getAligns 字符串没有shape属性。 您错过了转换步骤,例如读取图像。

您将带有图像名称的字符串传递给 function,然后询问字符串的形状。

print(picname)

返回0_0_ErinMurphy.jpg这是一个字符串。

您需要导入图像,然后将其转换为像素,以便您可以读取其形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM