简体   繁体   English

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

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

I'm using Face Align and getting the following error for every image:我正在使用 Face Align 并为每个图像获取以下错误:

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

I interpret this to mean that my code is expecting an image object and instead receiving a string, correct?我将此解释为意味着我的代码需要一个图像 object 而是接收一个字符串,对吗?

The offending code:违规代码:

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')

Relevant code from Align_Faces:来自 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])

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

Look at your data flow:查看您的数据流:

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)

Your posted output shows that you've maintained the file name as a string 0_0_ErinMurphy.jpg , and passed that string into getAligns .您发布的 output 表明您已将文件名保留为字符串0_0_ErinMurphy.jpg ,并将该字符串传递给getAligns A string has no shape attribute.字符串没有shape属性。 You've missed a conversion step, such as reading in the image.您错过了转换步骤,例如读取图像。

You are passing the string with the image name to the function and then asking what is the shape of the string.您将带有图像名称的字符串传递给 function,然后询问字符串的形状。

print(picname)

returns 0_0_ErinMurphy.jpg which is a string.返回0_0_ErinMurphy.jpg这是一个字符串。

You need to import the image and then convert it to the pixels so that you can read its shape.您需要导入图像,然后将其转换为像素,以便您可以读取其形状。

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

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