简体   繁体   中英

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

I'm using Face Align and getting the following error for every image:

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?

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:

  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: 在此处输入图像描述

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 . A string has no shape attribute. 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.

print(picname)

returns 0_0_ErinMurphy.jpg which is a string.

You need to import the image and then convert it to the pixels so that you can read its shape.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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