Пример #1
0
    def it_raises_if_cuboid_has_missing_point(annotations_file_path: Path,
                                              classes_file_path: Path):
        annotations_json: str = """
          {
             "instances": [
                {
                  "type": "cuboid",
                  "classId": 1,
                  "points": {
                     "f2": {"x": 3023.31, "y": 2302.75},
                     "r1": {"x": 1826.19, "y": 1841.44},
                     "r2": {"x": 2928, "y": 2222.69}
                  }
               }
             ],
             "tags": [],
             "metadata": {"name": "demo-image-0.jpg"}
          }
         """
        classes_json: str = """[]"""
        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

        assert "'cuboid' is not one of ['point']" in str(error.value)
Пример #2
0
    def it_raises_if_tags_is_missing(annotations_file_path: Path,
                                     classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x1": 1642.9, "x2": 1920, "y1": 516.5, "y2": 734},
                  "attributes": []
               }
            ],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
         {"attribute_groups": [], "id": 1, "name": "Person"}
      ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

        assert "'tags' is a required property" in str(error.value)
Пример #3
0
    def it_raises_if_annotation_has_no_class_id(annotations_file_path: Path,
                                                classes_file_path: Path):
        annotations_json: str = """
          {
             "instances": [
                {
                   "type": "point",
                   "x": 1,
                   "y": 0
                }
             ],
             "metadata": {
                "name": "demo-image-0.jpg"
             }
          }
         """
        classes_json: str = """[]"""

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

            assert "'classId' is a required property" in str(error.value)
Пример #4
0
    def it_raises_if_folder_has_no_classes_file(annotations_file_path: Path):
        annotations_json: str = """
         {
            "instances": [],
            "metadata": {
               "name": "demo-image-0.jpg"
            }
         }
         """
        annotations_file_path.write_text(annotations_json)

        with pytest.raises(ValueError) as error:
            parse_path(annotations_file_path)

        assert "Folder must contain a 'classes.json'" in str(error.value)
Пример #5
0
    def it_imports_ellipse_vectors(annotations_file_path: Path,
                                   classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "ellipse",
                  "classId": 1,
                  "cx": 922.1,
                  "cy": 475.8,
                  "rx": 205.4,
                  "ry": 275.7,
                  "angle": 0,
                  "attributes": []
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
          {"name": "Person", "id": 1, "attribute_groups": []}
       ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        ellipse_annotation: Annotation = cast(
            Annotation, annotation_file.annotations.pop())
        assert_ellipse(
            ellipse_annotation, {
                "angle": 0,
                "center": {
                    "x": 922.1,
                    "y": 475.8
                },
                "radius": {
                    "x": 205.4,
                    "y": 275.7
                }
            })

        annotation_class = ellipse_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-ellipse", "ellipse")
Пример #6
0
    def it_raises_if_an_attribute_from_a_group_is_missing(
            annotations_file_path: Path, classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x1": 1642.9, "x2": 1920, "y1": 516.5, "y2": 734},
                  "attributes": [{"id": 2, "groupId": 1}]
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
         {
            "attribute_groups": [
                  {
                     "id": 1,
                     "name": "Sex",
                     "attributes": [
                        {"id": 1, "name": "Male"}
                     ]
                  }
            ],
            "id": 1,
            "name": "Person"
         }
      ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValueError) as error:
            parse_path(annotations_file_path)

        assert "No attribute data found for {'id': 2, 'groupId': 1}." in str(
            error.value)
Пример #7
0
    def it_raises_if_ellipse_has_missing_coordinate(
            annotations_file_path: Path, classes_file_path: Path):
        annotations_json: str = """
          {
             "instances": [
                {"type": "ellipse", "cy": 0, "cx": 0, "rx": 0, "angle": 0}
             ],
             "tags": [],
             "metadata": {"name": "demo-image-0.jpg"}
          }
         """
        classes_json: str = """[]"""
        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

        assert "'ellipse' is not one of ['point']" in str(error.value)
Пример #8
0
    def it_imports_polyline_vectors(annotations_file_path: Path,
                                    classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "attributes": [],
                  "type": "polyline",
                  "classId": 1,
                  "points": [1053, 587.2, 1053.1, 586, 1053.8, 585.4]
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
          {"name": "Person", "id": 1, "attribute_groups": []}
       ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        line_annotation: Annotation = cast(Annotation,
                                           annotation_file.annotations.pop())
        assert_line(
            line_annotation,
            [{
                "x": 1053,
                "y": 587.2
            }, {
                "x": 1053.1,
                "y": 586
            }, {
                "x": 1053.8,
                "y": 585.4
            }],
        )

        annotation_class = line_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-polyline", "line")
Пример #9
0
    def it_raises_if_metadata_is_missing_name(annotations_file_path: Path,
                                              classes_file_path: Path):
        annotations_json: str = """
          {
             "instances": [
                {
                   "type": "point",
                   "x": 1,
                   "y": 0
                }
             ],
             "metadata": { }
          }
         """
        classes_json: str = """[]"""
        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

            assert "'name' is a required property" in str(error.value)
Пример #10
0
    def it_raises_if_bbox_has_missing_points(annotations_file_path: Path,
                                             classes_file_path: Path):
        annotations_json: str = """
          {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x2": 1920, "y1": 516.5, "y2": 734}
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
         """
        classes_json: str = """[]"""
        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        with pytest.raises(ValidationError) as error:
            parse_path(annotations_file_path)

        assert "'bbox' is not one of ['point']" in str(error.value)
Пример #11
0
    def it_imports_tags(annotations_file_path: Path, classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x1": 1642.9, "x2": 1920, "y1": 516.5, "y2": 734},
                  "attributes": []
               }
            ],
            "tags": ["street", "night"],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
         {
            "attribute_groups": [],
            "id": 1,
            "name": "Person"
         }
      ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        street_tag: Annotation = cast(Annotation,
                                      annotation_file.annotations[1])
        assert_annotation_class(street_tag.annotation_class, "street", "tag")

        night_tag: Annotation = cast(Annotation,
                                     annotation_file.annotations[2])
        assert_annotation_class(night_tag.annotation_class, "night", "tag")
Пример #12
0
    def it_imports_bbox_vectors(annotations_file_path: Path,
                                classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x1": 1642.9, "x2": 1920, "y1": 516.5, "y2": 734},
                  "attributes": []
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
          {"name": "Person", "id": 1, "attribute_groups": []}
       ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        bbox_annotation: Annotation = cast(Annotation,
                                           annotation_file.annotations.pop())
        assert_bbox(bbox_annotation, 1642.9, 516.5, 217.5, 277.1)

        annotation_class = bbox_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-bbox",
                                "bounding_box")
Пример #13
0
    def it_returns_empty_file_if_there_are_no_annotations(
            annotations_file_path: Path, classes_file_path: Path):
        annotations_json: str = """
         {
            "instances": [],
            "tags": [],
            "metadata": {
               "name": "demo-image-0.jpg"
            }
         }
         """
        classes_json: str = """[]"""

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        assert parse_path(annotations_file_path) == AnnotationFile(
            annotations=[],
            path=annotations_file_path,
            filename="demo-image-0.jpg",
            annotation_classes=set(),
            remote_path="/",
        )
Пример #14
0
    def it_imports_point_vectors(annotations_file_path: Path,
                                 classes_file_path: Path):

        annotations_json: str = """
       {
          "instances": [
             {"type": "point", "x": 1.93, "y": 0.233, "classId": 1, "attributes": []}
          ],
          "tags": [],
         "metadata": {"name": "demo-image-0.jpg"}
       }
      """
        classes_json: str = """
       [
          {"name": "Person", "id": 1, "attribute_groups": []}
       ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        point_annotation: Annotation = cast(Annotation,
                                            annotation_file.annotations.pop())
        assert_point(point_annotation, {"x": 1.93, "y": 0.233})

        annotation_class = point_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-point", "keypoint")
Пример #15
0
    def it_imports_attributes(annotations_file_path: Path,
                              classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "type": "bbox",
                  "classId": 1,
                  "points": {"x1": 1642.9, "x2": 1920, "y1": 516.5, "y2": 734},
                  "attributes": [{"id": 2, "groupId": 1}, {"id": 3, "groupId": 2}]
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
         {
            "attribute_groups": [
                  {
                     "id": 1,
                     "name": "Sex",
                     "attributes": [
                        {"id": 1, "name": "Male"},
                        {"id": 2, "name": "Female"}
                     ]
                  },
                  {
                     "id": 2,
                     "name": "Emotion",
                     "attributes": [
                        {"id": 3, "name": "Smiling"},
                        {"id": 4, "name": "Sadness"},
                        {"id": 5, "name": "Crying"}
                     ]
                  }
            ],
            "id": 1,
            "name": "Person"
         }
      ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        bbox_annotation: Annotation = cast(Annotation,
                                           annotation_file.annotations.pop())
        assert_bbox(bbox_annotation, 1642.9, 516.5, 217.5, 277.1)

        annotation_class = bbox_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-bbox",
                                "bounding_box")

        assert_subannotations(
            bbox_annotation.subs,
            [SubAnnotation("attributes", ["Sex:Female", "Emotion:Smiling"])])
Пример #16
0
    def it_imports_cuboid_vectors(annotations_file_path: Path,
                                  classes_file_path: Path):

        annotations_json: str = """
         {
            "instances": [
               {
                  "attributes": [],
                  "type": "cuboid",
                  "classId": 1,
                  "points": {
                     "f1": {"x": 1742.31, "y": 1727.06},
                     "f2": {"x": 3023.31, "y": 2302.75},
                     "r1": {"x": 1826.19, "y": 1841.44},
                     "r2": {"x": 2928, "y": 2222.69}
                  }
               }
            ],
            "tags": [],
            "metadata": {"name": "demo-image-0.jpg"}
         }
      """
        classes_json: str = """
       [
          {"name": "Person", "id": 1, "attribute_groups": []}
       ]
       """

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        annotation_file: Optional[AnnotationFile] = parse_path(
            annotations_file_path)
        assert annotation_file is not None
        assert annotation_file.path == annotations_file_path
        assert annotation_file.filename == "demo-image-0.jpg"
        assert annotation_file.annotation_classes
        assert annotation_file.remote_path == "/"

        assert annotation_file.annotations

        cuboid_annotation: Annotation = cast(Annotation,
                                             annotation_file.annotations.pop())
        assert_cuboid(
            cuboid_annotation,
            {
                "back": {
                    "h": 381.25,
                    "w": 1101.81,
                    "x": 1826.19,
                    "y": 1841.44
                },
                "front": {
                    "h": 575.69,
                    "w": 1281.0,
                    "x": 1742.31,
                    "y": 1727.06
                },
            },
        )

        annotation_class = cuboid_annotation.annotation_class
        assert_annotation_class(annotation_class, "Person-cuboid", "cuboid")
Пример #17
0
 def it_returns_none_if_file_is_classes():
     bad_path = Path("/tmp/classes.json")
     assert parse_path(bad_path) is None
Пример #18
0
 def it_returns_none_if_file_is_not_json():
     bad_path = Path("/tmp/annotation.xml")
     assert parse_path(bad_path) is None