glenn-jocher
commited on
Commit
•
74276d5
1
Parent(s):
9ccfa85
Updated filename attributes for YOLOv5 Hub results (#2708)
Browse filesProposed fix for 'Model predict with forward will fail if PIL image does not have filename attribute' #2702
- 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
|
259 |
-
|
260 |
-
|
261 |
-
|
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
|