예제 #1
0
 def load_image(self, i):
     # Loads 1 image from dataset index 'i', returns (im, original hw, resized hw)
     im, f, fn = self.ims[i], self.im_files[i], self.npy_files[i],
     if im is None:  # not cached in RAM
         if fn.exists():  # load npy
             im = np.load(fn)
         else:  # read image
             im = cv2.imread(f)  # BGR
             assert im is not None, f'Image Not Found {f}'
         h0, w0 = im.shape[:2]  # orig hw
         r = self.img_size / max(h0, w0)  # ratio
         if r != 1:  # if sizes are not equal
             interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA
             im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp)
         return im, (h0, w0), im.shape[:2]  # im, hw_original, hw_resized
     return self.ims[i], self.im_hw0[i], self.im_hw[i]  # im, hw_original, hw_resized
예제 #2
0
 def _hub_ops(f, max_dim=1920):
     # HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing
     f_new = im_dir / Path(f).name  # dataset-hub image filename
     try:  # use PIL
         im = Image.open(f)
         r = max_dim / max(im.height, im.width)  # ratio
         if r < 1.0:  # image too large
             im = im.resize((int(im.width * r), int(im.height * r)))
         im.save(f_new, 'JPEG', quality=75, optimize=True)  # save
     except Exception as e:  # use OpenCV
         print(f'WARNING: HUB ops PIL failure {f}: {e}')
         im = cv2.imread(f)
         im_height, im_width = im.shape[:2]
         r = max_dim / max(im_height, im_width)  # ratio
         if r < 1.0:  # image too large
             im = cv2.resize(im, (int(im_width * r), int(im_height * r)), interpolation=cv2.INTER_AREA)
         cv2.imwrite(str(f_new), im)