FaceAlign AttributeError:“ str”对象没有属性“ shape”

时间:2019-11-15 23:14:06

标签: python opencv

我正在使用“人脸对齐”,每张图像都会出现以下错误:

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

我将其解释为我的代码期望图像对象而不是接收字符串,对吗?

违规代码:

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

完整输出: enter image description here

2 个答案:

答案 0 :(得分:0)

查看您的数据流:

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)

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

答案 1 :(得分:0)

您要将带有图像名称的字符串传递给函数,然后询问字符串的形状是什么。

SELECT first_name, last_name, hire_date 
FROM employees
WHERE hire_date LIKE '1986%';

返回print(picname) ,它是一个字符串。

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