From cc0fb8df6d35d3d757f54857aa8cb17803b405a5 Mon Sep 17 00:00:00 2001 From: Zhiltsov Max Date: Mon, 3 Aug 2020 12:10:42 +0300 Subject: [PATCH] handle image absence --- .../datumaro/cli/contexts/project/__init__.py | 5 ++-- datumaro/datumaro/components/operations.py | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/datumaro/datumaro/cli/contexts/project/__init__.py b/datumaro/datumaro/cli/contexts/project/__init__.py index 6b3acca0540b..99f5fe82467f 100644 --- a/datumaro/datumaro/cli/contexts/project/__init__.py +++ b/datumaro/datumaro/cli/contexts/project/__init__.py @@ -650,13 +650,14 @@ def stats_command(args): project = load_project(args.project_dir) dataset = project.make_dataset() - stats = compute_image_statistics(dataset) + stats = {} + stats.update(compute_image_statistics(dataset)) stats.update(compute_ann_statistics(dataset)) dst_file = generate_next_file_name('statistics', ext='.json') log.info("Writing project statistics to '%s'" % dst_file) with open(dst_file, 'w') as f: - json.dump(stats, f) + json.dump(stats, f, indent=4, sort_keys=True) def build_info_parser(parser_ctor=argparse.ArgumentParser): parser = parser_ctor(help="Get project info", diff --git a/datumaro/datumaro/components/operations.py b/datumaro/datumaro/components/operations.py index 37ef425ac761..7961775e8b90 100644 --- a/datumaro/datumaro/components/operations.py +++ b/datumaro/datumaro/components/operations.py @@ -3,6 +3,7 @@ # # SPDX-License-Identifier: MIT +import logging as log from copy import deepcopy import cv2 @@ -94,13 +95,31 @@ def compute_image_statistics(dataset): } def _extractor_stats(extractor): - mean, std = mean_std(extractor) - return { + available = True + for item in extractor: + if not (item.has_image and item.image.has_data): + available = False + log.warn("Item %s has no image. Image stats won't be computed", + item.id) + break + + stats = { 'images count': len(extractor), - 'image mean': [float(n) for n in mean[::-1]], - 'image std': [float(n) for n in std[::-1]], } + if available: + mean, std = mean_std(extractor) + stats.update({ + 'image mean': [float(n) for n in mean[::-1]], + 'image std': [float(n) for n in std[::-1]], + }) + else: + stats.update({ + 'image mean': 'n/a', + 'image std': 'n/a', + }) + return stats + stats['dataset'].update(_extractor_stats(dataset)) subsets = dataset.subsets() or [None]