简体   繁体   中英

<built-in function imread> returned NULL without setting an error

    def image_descriptors(file):
      img = cv2.imread(file,0)
      img = cv2.resize(img, (256, 256))
      _ , descriptors = cv2.SIFT().detectAndCompute(img, None)
   return descriptors
   def folder_descriptors(folder):
     cv_img=[]
     for img in glob.glob("*.jpg"):
         n=cv2.imread(img)
         cv_img.append(n)
   print("Calculating descriptos. Number of images is", len(cv_img))
   return np.concatenate([image_descriptors(file) for file in cv_img])

I am getting the following in output screen: Calculating descriptos. Number of images is 274 SystemError: returned NULL without setting an error

Path.glob return list of Path object ( WindowsPath in windows or PosixPath in linux). and cv2.imread expect string .

you can get string of path from Path object using str class ( str(path_object) ) or build in magic method from Path object ( path_object.__str__() )

def image_descriptors(file):
  img = cv2.imread(file,0)
  img = cv2.resize(img, (256, 256))
  _ , descriptors = cv2.SIFT().detectAndCompute(img, None)
  return descriptors
def folder_descriptors(folder):
  cv_img=[]
  for img in glob.glob("*.jpg"):
     # convert `Path` object to string
     n=cv2.imread(str(img))
     cv_img.append(n)
  print("Calculating descriptos. Number of images is", len(cv_img))
  return np.concatenate([image_descriptors(file) for file in cv_img])

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