From 750932c52826c09041b02e4cf911a1a894f1d1a7 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Mon, 5 Apr 2021 22:14:31 +0200 Subject: [PATCH] Updated filename attributes for YOLOv5 Hub results Proposed fix for 'Model predict with forward will fail if PIL image does not have filename attribute' #2702 --- models/common.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/models/common.py b/models/common.py index 713297c14433..f6da7ad3113b 100644 --- a/models/common.py +++ b/models/common.py @@ -254,12 +254,12 @@ def forward(self, imgs, size=640, augment=False, profile=False): n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images shape0, shape1, files = [], [], [] # image and inference shapes, filenames for i, im in enumerate(imgs): + f = f'image{i}' # filename if isinstance(im, str): # filename or uri - im, f = Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im), im # open - im.filename = f # for uri - files.append(Path(im.filename).with_suffix('.jpg').name if isinstance(im, Image.Image) else f'image{i}.jpg') - if not isinstance(im, np.ndarray): - im = np.asarray(im) # to numpy + im, f = np.asarray(Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)), im + elif isinstance(im, Image.Image): # PIL Image + im, f = np.asarray(im), getattr(im, 'filename', f) + files.append(Path(f).with_suffix('.jpg').name) if im.shape[0] < 5: # image in CHW im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input