glenn-jocher commited on
Commit
74276d5
1 Parent(s): 9ccfa85

Updated filename attributes for YOLOv5 Hub results (#2708)

Browse files

Proposed fix for 'Model predict with forward will fail if PIL image does not have filename attribute' #2702

Files changed (1) hide show
  1. models/common.py +5 -5
models/common.py CHANGED
@@ -254,12 +254,12 @@ class autoShape(nn.Module):
254
  n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
255
  shape0, shape1, files = [], [], [] # image and inference shapes, filenames
256
  for i, im in enumerate(imgs):
 
257
  if isinstance(im, str): # filename or uri
258
- im, f = Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im), im # open
259
- im.filename = f # for uri
260
- files.append(Path(im.filename).with_suffix('.jpg').name if isinstance(im, Image.Image) else f'image{i}.jpg')
261
- if not isinstance(im, np.ndarray):
262
- im = np.asarray(im) # to numpy
263
  if im.shape[0] < 5: # image in CHW
264
  im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
265
  im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input
 
254
  n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
255
  shape0, shape1, files = [], [], [] # image and inference shapes, filenames
256
  for i, im in enumerate(imgs):
257
+ f = f'image{i}' # filename
258
  if isinstance(im, str): # filename or uri
259
+ im, f = np.asarray(Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)), im
260
+ elif isinstance(im, Image.Image): # PIL Image
261
+ im, f = np.asarray(im), getattr(im, 'filename', f)
262
+ files.append(Path(f).with_suffix('.jpg').name)
 
263
  if im.shape[0] < 5: # image in CHW
264
  im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
265
  im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input