File size: 5,679 Bytes
3f7dd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
This file contains tests for the API of your model. You can run these tests by installing test requirements:

```bash
pip install -r requirements-test.txt
```
"""

import pytest
import json

from label_studio_ml.utils import compare_nested_structures
from .test_common import client


label_configs = [
    # test 1: one control tag with rectangle labels
    """
    <View>
      <Image name="image" value="$image"/>
      <RectangleLabels name="label" toName="image" model_score_threshold="0.25">
        <Label value="Airplane" background="green"/>
        <Label value="Car" background="blue" predicted_values="car, truck"/>
      </RectangleLabels>
    </View>
    """,
    # test 2: two control tags with rectangle labels and two images
    """
    <View>
      <Image name="image" value="$image"/>
      <RectangleLabels name="label" toName="image" model_score_threshold="0.30">
        <Label value="Airplane" background="green"/>
        <Label value="Car" background="blue" predicted_values="car, truck"/>
      </RectangleLabels>
      
      <Image name="image2" value="$image2"/>
      <RectangleLabels name="label2" toName="image2" model_score_threshold="0.90">
        <Label value="Person" background="green"/>
        <Label value="Animal" background="blue" predicted_values="cat,dog"/>
      </RectangleLabels>
    </View>
    """,
]

tasks = [
    # test 1: one control tag with rectangle labels
    {
        "data": {
            "image": "https://s3.amazonaws.com/htx-pub/datasets/mmdetection-ml-test/001bebecea382500.jpg"
        }
    },
    # test 2: two control tags with rectangle labels and two images
    {
        "data": {
            "image": "https://s3.amazonaws.com/htx-pub/datasets/mmdetection-ml-test/001bebecea382500.jpg",
            "image2": "https://s3.amazonaws.com/htx-pub/datasets/mmdetection-ml-test/001bebecea382500.jpg",
        }
    },
]

expected = [
    # test 1: one control tag with rectangle labels
    [
        {
            "model_version": "yolo",
            "result": [
                {
                    "from_name": "label",
                    "score": 0.5791077017784119,
                    "to_name": "image",
                    "type": "rectanglelabels",
                    "value": {
                        "height": 77.13761925697327,
                        "rectanglelabels": ["Car"],
                        "width": 69.33701038360596,
                        "x": 21.9377338886261,
                        "y": 7.984769344329834,
                    },
                },
                {
                    "from_name": "label",
                    "score": 0.31354132294654846,
                    "to_name": "image",
                    "type": "rectanglelabels",
                    "value": {
                        "height": 25.369155406951904,
                        "rectanglelabels": ["Car"],
                        "width": 18.623733520507812,
                        "x": 81.27312660217285,
                        "y": 0.10521858930587769,
                    },
                },
            ],
            "score": 0.44632451236248016,
        }
    ],
    # test 2: two control tags with rectangle labels and two images
    [
        {
            "model_version": "yolo",
            "result": [
                {
                    "from_name": "label",
                    "score": 0.5791077017784119,
                    "to_name": "image",
                    "type": "rectanglelabels",
                    "value": {
                        "height": 77.13761925697327,
                        "rectanglelabels": ["Car"],
                        "width": 69.33701038360596,
                        "x": 21.9377338886261,
                        "y": 7.984769344329834,
                    },
                },
                {
                    "from_name": "label",
                    "score": 0.31354132294654846,
                    "to_name": "image",
                    "type": "rectanglelabels",
                    "value": {
                        "height": 25.369155406951904,
                        "rectanglelabels": ["Car"],
                        "width": 18.623733520507812,
                        "x": 81.27312660217285,
                        "y": 0.10521858930587769,
                    },
                },
                {
                    "from_name": "label2",
                    "score": 0.9059886932373047,
                    "to_name": "image2",
                    "type": "rectanglelabels",
                    "value": {
                        "height": 39.60925042629242,
                        "rectanglelabels": ["Person"],
                        "width": 10.503808408975601,
                        "x": 89.45398144423962,
                        "y": 6.985808908939362,
                    },
                },
            ],
            "score": 0.5995459059874216,
        }
    ],
]


@pytest.mark.parametrize(
    "label_config, task, expect", zip(label_configs, tasks, expected)
)
def test_rectanglelabels_predict(client, label_config, task, expect):
    data = {"schema": label_config, "project": "42"}
    response = client.post(
        "/setup", data=json.dumps(data), content_type="application/json"
    )
    assert response.status_code == 200, "Error while setup: " + str(response.content)

    data = {"tasks": [task], "label_config": label_config}
    response = client.post(
        "/predict", data=json.dumps(data), content_type="application/json"
    )
    assert response.status_code == 200, "Error while predict"
    data = response.json
    compare_nested_structures(data["results"], expect)