forked from robmarkcole/deepstack-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_deepstack.py
More file actions
172 lines (140 loc) · 4.66 KB
/
test_deepstack.py
File metadata and controls
172 lines (140 loc) · 4.66 KB
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
166
167
168
169
170
171
172
import deepstack.core as ds
import requests
import requests_mock
import pytest
MOCK_IP_ADDRESS = "localhost"
MOCK_PORT = 5000
OBJ_URL = "http://{}:{}/v1/vision/detection".format(MOCK_IP_ADDRESS, MOCK_PORT)
SCENE_URL = "http://{}:{}/v1/vision/scene".format(MOCK_IP_ADDRESS, MOCK_PORT)
FACE_DETECTION_URL = "http://{}:{}/v1/vision/face".format(MOCK_IP_ADDRESS, MOCK_PORT)
CONFIDENCE_THRESHOLD = 0.7
MOCK_BYTES = b"Test"
MOCK_API_KEY = "mock_api_key"
MOCK_TIMEOUT = 8
MOCK_SCENE_RESPONSE = {"success": True, "label": "street", "confidence": 0.86745402}
MOCK_OBJECT_DETECTION_RESPONSE = {
"success": True,
"predictions": [
{
"confidence": 0.6998661,
"label": "person",
"y_min": 0,
"x_min": 258,
"y_max": 676,
"x_max": 485,
},
{
"confidence": 0.7996547,
"label": "person",
"y_min": 0,
"x_min": 405,
"y_max": 652,
"x_max": 639,
},
{
"confidence": 0.59745613,
"label": "dog",
"y_min": 311,
"x_min": 624,
"y_max": 591,
"x_max": 825,
},
],
}
MOCK_OBJECT_PREDICTIONS = MOCK_OBJECT_DETECTION_RESPONSE["predictions"]
MOCK_OBJECT_CONFIDENCES = [0.6998661, 0.7996547]
MOCK_FACE_RECOGNITION_RESPONSE = {
"success": True,
"predictions": [
{
"confidence": 0.74999994,
"userid": "Idris Elba",
"y_min": 176,
"x_min": 209,
"y_max": 825,
"x_max": 677,
},
{
"confidence": 0,
"userid": "unknown",
"y_min": 230,
"x_min": 867,
"y_max": 729,
"x_max": 1199,
},
],
}
MOCK_FACE_DETECTION_RESPONSE = {
"success": True,
"predictions": [
{
"confidence": 0.9999999,
"y_min": 173,
"x_min": 203,
"y_max": 834,
"x_max": 667,
}
],
}
MOCK_RECOGNISED_FACES = {"Idris Elba": 75.0}
def test_DeepstackObject_detect():
"""Test a good response from server."""
with requests_mock.Mocker() as mock_req:
mock_req.post(
OBJ_URL, status_code=ds.HTTP_OK, json=MOCK_OBJECT_DETECTION_RESPONSE
)
dsobject = ds.DeepstackObject(MOCK_IP_ADDRESS, MOCK_PORT)
dsobject.detect(MOCK_BYTES)
assert dsobject.predictions == MOCK_OBJECT_PREDICTIONS
def test_DeepstackObject_detect_timeout():
"""Test a timeout. THIS SHOULD FAIL"""
with pytest.raises(ds.DeepstackException) as excinfo:
with requests_mock.Mocker() as mock_req:
mock_req.post(OBJ_URL, exc=requests.exceptions.ConnectTimeout)
dsobject = ds.DeepstackObject(MOCK_IP_ADDRESS, MOCK_PORT)
dsobject.detect(MOCK_BYTES)
assert False
assert "SHOULD FAIL" in str(excinfo.value)
def test_DeepstackScene():
"""Test a good response from server."""
with requests_mock.Mocker() as mock_req:
mock_req.post(SCENE_URL, status_code=ds.HTTP_OK, json=MOCK_SCENE_RESPONSE)
dsscene = ds.DeepstackScene(MOCK_IP_ADDRESS, MOCK_PORT)
dsscene.detect(MOCK_BYTES)
assert dsscene.predictions == MOCK_SCENE_RESPONSE
def test_DeepstackFace():
"""Test a good response from server."""
with requests_mock.Mocker() as mock_req:
mock_req.post(
FACE_DETECTION_URL,
status_code=ds.HTTP_OK,
json=MOCK_FACE_DETECTION_RESPONSE,
)
dsface = ds.DeepstackFace(MOCK_IP_ADDRESS, MOCK_PORT)
dsface.detect(MOCK_BYTES)
assert dsface.predictions == MOCK_FACE_DETECTION_RESPONSE["predictions"]
def test_get_objects():
"""Cant always be sure order of returned list items."""
objects = ds.get_objects(MOCK_OBJECT_PREDICTIONS)
assert type(objects) is list
assert "dog" in objects
assert "person" in objects
assert len(objects) == 2
def test_get_objects_summary():
objects_summary = ds.get_objects_summary(MOCK_OBJECT_PREDICTIONS)
assert objects_summary == {"dog": 1, "person": 2}
def test_get_object_confidences():
object_confidences = ds.get_object_confidences(MOCK_OBJECT_PREDICTIONS, "person")
assert object_confidences == MOCK_OBJECT_CONFIDENCES
def test_get_confidences_above_threshold():
assert (
len(
ds.get_confidences_above_threshold(
MOCK_OBJECT_CONFIDENCES, CONFIDENCE_THRESHOLD
)
)
== 1
)
def test_get_recognised_faces():
predictions = MOCK_FACE_RECOGNITION_RESPONSE["predictions"]
assert ds.get_recognised_faces(predictions) == MOCK_RECOGNISED_FACES