Helper methods
logger = logging.getLogger()
with measure_time() as timer:
time.sleep(0.2)
print(f"Elapsed {timer.elapsed}")
@log_elapsed_time(lambda t: print(f'Elapsed: {t.elapsed}'))
def f():
time.sleep(0.1)
f()
s = sort_dict(
{
'images': [{'id': 3}, {'id': 1}, {'id': 2}],
'annotations': [{'id': 11}, {'id': 12}, {'id': 13}],
'categories': [{'id': 101}, {'id': 103}, {'id': 102}],
'licenses': [{'id': 1002}, {'id': 1001}],
'info': {'key': 'value'},
}
)
display(s)
j = json.dumps(s)
assert j == (
'{"annotations": [{"id": 11}, {"id": 12}, {"id": 13}], '
'"categories": [{"id": 101}, {"id": 102}, {"id": 103}], '
'"images": [{"id": 1}, {"id": 2}, {"id": 3}], '
'"info": {"key": "value"}, '
'"licenses": [{"id": 1001}, {"id": 1002}]}'
), j
sanitize_filename('a b «ccc ddd» Ef жиза, 3*50 г НОВИНКА!!!')
anns = !ls ../examples/coco_chunk/json_tree/annotations
for x in anns:
print(repr(x))
# import tempfile
# null = None
# BASE = Path('/home/ay/Downloads/outforz-lactalis/output/clean/2020-10-01_2020-11-01')
# ann = {"id": 2131512, "image_id": 102237, "category_id": "4 823 065 722 757", "bbox": [0.2347949080622348, 0.5921750663129973, 0.09335219236209336, 0.03183023872679047], "supercategory": null, "area": null, "iscrowd": null}
# img = json.loads((BASE/'json_tree/images/102237.json').read_text())
# print(img)
# tmp = Path(tempfile.mktemp())
# print(tmp)
# image = read_image(tmp, img['coco_url'])
# image_box = cut_bbox(image, ann['bbox'])
# draw_image(image_box, figsize=(8, 8))
import json
from pathlib import Path
import tempfile
JSON_TREE_PATH = '../examples/coco_chunk/json_tree/'
CROP_TREE_PATH = tempfile.mktemp()
ann_files = !ls {JSON_TREE_PATH}/annotations
for ann_file in ann_files:
ANN = f'{JSON_TREE_PATH}/annotations/{ann_file}'
#! cat {ANN}
ann = json.loads(Path(ANN).read_text())
box = ann['bbox']
IMG = f'{JSON_TREE_PATH}/images/{ann["image_id"]}.json'
#! cat {IMG}
img = json.loads(Path(IMG).read_text())
CAT = f'{JSON_TREE_PATH}/categories/{ann["category_id"]}.json'
! cat {CAT}
cat = json.loads(Path(CAT).read_text())
IMG_PATH = f'{CROP_TREE_PATH}/images/{img["file_name"]}'
IMG_URL = img["coco_url"]
image = read_image(IMG_PATH, IMG_URL)
display(f'image: shape={image.shape} size={image.size}')
image_box = cut_bbox(image, box)
display(f'box: shape={image_box.shape} size={image_box.size}')
draw_image(image_box, figsize=(8, 8))
cat_dir_name = f'{sanitize_filename(cat["name"])}--{cat["id"]}'
CROP_PATH = f'{CROP_TREE_PATH}/crops/{cat_dir_name}/{ann["id"]}.png'
write_image(image_box, CROP_PATH)
display(f'saved to {CROP_PATH}')