Parse any type of bounding box format. Args: fmt (str or class): Format from the :mod:`brambox.boxes.format ` dictionary box_file (list or string): Bounding box filename or array of bounding box file names identify (function, optional): Function to create
(fmt, box_file, identify=None, offset=0, stride=1, **kwargs)
| 13 | |
| 14 | |
| 15 | def parse(fmt, box_file, identify=None, offset=0, stride=1, **kwargs): |
| 16 | """ Parse any type of bounding box format. |
| 17 | |
| 18 | Args: |
| 19 | fmt (str or class): Format from the :mod:`brambox.boxes.format <brambox.boxes>` dictionary |
| 20 | box_file (list or string): Bounding box filename or array of bounding box file names |
| 21 | identify (function, optional): Function to create an image identifier |
| 22 | offset (int, optional): Skip images untill offset; Default **0** |
| 23 | stride (int, optional): Only read every n'th file; Default **1** |
| 24 | **kwargs: Keyword arguments that are passed to the parser |
| 25 | |
| 26 | Returns: |
| 27 | dict: Dictionary containing the bounding boxes for every image ``{"image_id": [box, box, ...], ...}`` |
| 28 | |
| 29 | Note: |
| 30 | The ``identify`` function will be used to generate ``image_id`` tags. |br| |
| 31 | If the format is of the type :any:`brambox.boxes.box.ParserType.SINGLE_FILE`, |
| 32 | the identify function gets the existing ``image_id`` tags as input. The default is to not change the tags. |br| |
| 33 | If the format is of the type :any:`brambox.boxes.box.ParserType.MULTI_FILE`, |
| 34 | the identify function gets the path of the current file as input. The default is to get the name of the file without extensions. |
| 35 | |
| 36 | Warning: |
| 37 | The ``box_file`` parameter can be either a list or string. |br| |
| 38 | If the format is of the type :any:`brambox.boxes.box.ParserType.SINGLE_FILE`, |
| 39 | then only a string is accepted and this is used as the filename. |br| |
| 40 | If the format is of the type :any:`brambox.boxes.box.ParserType.MULTI_FILE`, |
| 41 | then you can either pass a list or a string. |
| 42 | A list will be used as is, namely every string of the list gets used as a filename. |
| 43 | If you use a string, it will first be expanded with the :func:`~brambox.boxes.expand` function |
| 44 | to generate a list of strings. This expand function can take optional stride and offset parameters, |
| 45 | which can be passed via keyword arguments. |
| 46 | """ |
| 47 | |
| 48 | # Create parser |
| 49 | if type(fmt) is str: |
| 50 | try: |
| 51 | parser = formats[fmt](**kwargs) |
| 52 | except KeyError: |
| 53 | raise TypeError(f'Invalid parser {fmt}') |
| 54 | elif issubclass(fmt, Parser): |
| 55 | parser = fmt(**kwargs) |
| 56 | else: |
| 57 | raise TypeError(f'Invalid parser {fmt}') |
| 58 | |
| 59 | # Parse bounding boxes |
| 60 | if parser.parser_type == ParserType.SINGLE_FILE: |
| 61 | if type(box_file) is not str: |
| 62 | raise TypeError(f'Parser <{parser.__class__.__name__}> requires a single annotation file') |
| 63 | with open(box_file, parser.read_mode) as f: |
| 64 | data = parser.deserialize(f.read()) |
| 65 | |
| 66 | # Offset |
| 67 | if offset > 0: |
| 68 | keys = sorted(list(data.keys())) |
| 69 | while offset > 0: |
| 70 | offset -= 1 |
| 71 | del data[keys[offset]] |
| 72 |
nothing calls this directly
no test coverage detected