Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_img_and_lbl():
img, data = _get_img_and_data()
label_name_to_value = {'__background__': 0}
for shape in data['shapes']:
label_name = shape['label']
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
n_labels = max(label_name_to_value.values()) + 1
label_names = [None] * n_labels
for label_name, label_value in label_name_to_value.items():
label_names[label_value] = label_name
lbl = labelme.utils.shapes_to_label(
img.shape, data['shapes'], label_name_to_value)
return img, lbl, label_names
def _get_img_and_data():
json_file = osp.join(data_dir, 'apc2016_obj3.json')
data = json.load(open(json_file))
img_b64 = data['imageData']
img = labelme.utils.img_b64_to_arr(img_b64)
return img, data
def test_img_arr_to_b64():
img_file = osp.join(data_dir, 'apc2016_obj3.jpg')
img_arr = np.asarray(PIL.Image.open(img_file))
img_b64 = labelme.utils.img_arr_to_b64(img_arr)
img_arr2 = labelme.utils.img_b64_to_arr(img_b64)
np.testing.assert_allclose(img_arr, img_arr2)
def test_draw_label():
img, lbl, label_names = _get_img_and_lbl()
viz = labelme.utils.draw_label(lbl, img, label_names=label_names)
assert viz.shape[:2] == img.shape[:2] == lbl.shape[:2]
assert viz.dtype == np.uint8
def test_draw_instances():
img, lbl, label_names = _get_img_and_lbl()
labels_and_masks = {l: lbl == l for l in np.unique(lbl) if l != 0}
labels, masks = zip(*labels_and_masks.items())
masks = np.asarray(masks)
bboxes = labelme.utils.masks_to_bboxes(masks)
captions = [label_names[l] for l in labels]
viz = labelme.utils.draw_instances(img, bboxes, labels, captions=captions)
assert viz.shape[:2] == img.shape[:2]
assert viz.dtype == np.uint8
def test_shape_to_mask():
img, data = _get_img_and_data()
for shape in data['shapes']:
points = shape['points']
mask = labelme.utils.shape_to_mask(img.shape[:2], points)
assert mask.shape == img.shape[:2]
license=0,
url=None,
file_name=osp.relpath(out_img_file, osp.dirname(out_ann_file)),
height=img.shape[0],
width=img.shape[1],
date_captured=None,
id=image_id,
))
masks = {} # for area
segmentations = collections.defaultdict(list) # for segmentation
for shape in label_data['shapes']:
points = shape['points']
label = shape['label']
shape_type = shape.get('shape_type', None)
mask = labelme.utils.shape_to_mask(
img.shape[:2], points, shape_type
)
if label in masks:
masks[label] = masks[label] | mask
else:
masks[label] = mask
points = np.asarray(points).flatten().tolist()
segmentations[label].append(points)
for label, mask in masks.items():
cls_name = label.split('-')[0]
if cls_name not in class_name_to_id:
continue
cls_id = class_name_to_id[cls_name]
help='keep annotation of previous frame',
default=argparse.SUPPRESS,
)
parser.add_argument(
'--epsilon',
type=float,
help='epsilon to find nearest vertex on canvas',
default=argparse.SUPPRESS,
)
args = parser.parse_args()
if args.version:
print('{0} {1}'.format(__appname__, __version__))
sys.exit(0)
logger.setLevel(getattr(logging, args.logger_level.upper()))
if hasattr(args, 'flags'):
if os.path.isfile(args.flags):
with codecs.open(args.flags, 'r', encoding='utf-8') as f:
args.flags = [l.strip() for l in f if l.strip()]
else:
args.flags = [l for l in args.flags.split(',') if l]
if hasattr(args, 'labels'):
if os.path.isfile(args.labels):
with codecs.open(args.labels, 'r', encoding='utf-8') as f:
args.labels = [l.strip() for l in f if l.strip()]
else:
args.labels = [l for l in args.labels.split(',') if l]
if hasattr(args, 'label_flags'):
if os.path.isfile(args.label_flags):
with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
args.label_flags = yaml.load(f)
else:
args.label_flags = yaml.load(args.label_flags)
config_from_args = args.__dict__
config_from_args.pop('version')
reset_config = config_from_args.pop('reset_config')
filename = config_from_args.pop('filename')
output = config_from_args.pop('output')
config_file = config_from_args.pop('config_file')
config = get_config(config_from_args, config_file)
if not config['labels'] and config['validate_label']:
logger.error('--labels must be specified with --validatelabel or '
'validate_label: true in the config file '
'(ex. ~/.labelmerc).')
sys.exit(1)
output_file = None
output_dir = None
if output is not None:
if output.endswith('.json'):
output_file = output
else:
output_dir = output
translator = QtCore.QTranslator()
translator.load(
QtCore.QLocale.system().name(),
osp.dirname(osp.abspath(__file__)) + '/translate'
output_file = None
output_dir = None
if output is not None:
if output.endswith('.json'):
output_file = output
else:
output_dir = output
translator = QtCore.QTranslator()
translator.load(
QtCore.QLocale.system().name(),
osp.dirname(osp.abspath(__file__)) + '/translate'
)
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName(__appname__)
app.setWindowIcon(newIcon('icon'))
app.installTranslator(translator)
win = MainWindow(
config=config,
filename=filename,
output_file=output_file,
output_dir=output_dir,
)
if reset_config:
logger.info('Resetting Qt config: %s' % win.settings.fileName())
win.settings.clear()
sys.exit(0)
win.show()
win.raise_()
sys.exit(app.exec_())