Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YOLOv5 PyTorch Hub results.save() method retains filenames #2194

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ def forward(self, imgs, size=640, augment=False, profile=False):

# Pre-process
n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
shape0, shape1 = [], [] # image and inference shapes
shape0, shape1, files = [], [], [] # image and inference shapes, filenames
for i, im in enumerate(imgs):
if isinstance(im, str): # filename or uri
im = Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im) # open
files.append(Path(im.filename).with_suffix('.jpg').name if isinstance(im, Image.Image) else f'image{i}.jpg')
im = np.array(im) # to numpy
if im.shape[0] < 5: # image in CHW
im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
Expand All @@ -224,18 +225,19 @@ def forward(self, imgs, size=640, augment=False, profile=False):
for i in range(n):
scale_coords(shape1, y[i][:, :4], shape0[i])

return Detections(imgs, y, self.names)
return Detections(imgs, y, files, self.names)


class Detections:
# detections class for YOLOv5 inference results
def __init__(self, imgs, pred, names=None):
def __init__(self, imgs, pred, files, names=None):
super(Detections, self).__init__()
d = pred[0].device # device
gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations
self.imgs = imgs # list of images as numpy arrays
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
self.names = names # class names
self.files = files # image filenames
self.xyxy = pred # xyxy pixels
self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
Expand All @@ -258,9 +260,9 @@ def display(self, pprint=False, show=False, save=False, render=False, save_dir='
if pprint:
print(str.rstrip(', '))
if show:
img.show(f'image {i}') # show
img.show(self.files[i]) # show
if save:
f = Path(save_dir) / f'results{i}.jpg'
f = Path(save_dir) / self.files[i]
img.save(f) # save
print(f"{'Saving' * (i == 0)} {f},", end='' if i < self.n - 1 else ' done.\n')
if render:
Expand All @@ -272,7 +274,8 @@ def print(self):
def show(self):
self.display(show=True) # show results

def save(self, save_dir=''):
def save(self, save_dir='results/'):
Path(save_dir).mkdir(exist_ok=True)
self.display(save=True, save_dir=save_dir) # save results

def render(self):
Expand Down