1- # Placeholder
1+ # Placeholder
2+
3+ import deepstack .core as ds
4+ import requests
5+ import requests_mock
6+ import pytest
7+
8+ MOCK_IP_ADDRESS = "localhost"
9+ MOCK_PORT = 5000
10+ MOCK_URL = "http://{}:{}/v1/vision/detection" .format (MOCK_IP_ADDRESS , MOCK_PORT )
11+
12+ MOCK_BYTES = b"Test"
13+ MOCK_API_KEY = "mock_api_key"
14+ MOCK_TIMEOUT = 8
15+
16+ MOCK_RESPONSE = {
17+ "success" : True ,
18+ "predictions" : [
19+ {
20+ "confidence" : 0.6998661 ,
21+ "label" : "person" ,
22+ "y_min" : 0 ,
23+ "x_min" : 258 ,
24+ "y_max" : 676 ,
25+ "x_max" : 485 ,
26+ },
27+ {
28+ "confidence" : 0.7996547 ,
29+ "label" : "person" ,
30+ "y_min" : 0 ,
31+ "x_min" : 405 ,
32+ "y_max" : 652 ,
33+ "x_max" : 639 ,
34+ },
35+ {
36+ "confidence" : 0.59745613 ,
37+ "label" : "dog" ,
38+ "y_min" : 311 ,
39+ "x_min" : 624 ,
40+ "y_max" : 591 ,
41+ "x_max" : 825 ,
42+ },
43+ ],
44+ }
45+
46+ MOCK_PREDICTIONS = MOCK_RESPONSE ["predictions" ]
47+ MOCK_CONFIDENCES = [0.6998661 , 0.7996547 ]
48+ CONFIDENCE_THRESHOLD = 0.7
49+
50+
51+ def test_DeepstackObject_process_image_bytes ():
52+ """Test a good response from server."""
53+ with requests_mock .Mocker () as mock_req :
54+ mock_req .post (MOCK_URL , status_code = ds .HTTP_OK , json = MOCK_RESPONSE )
55+
56+ dsobject = ds .DeepstackObject (MOCK_IP_ADDRESS , MOCK_PORT )
57+ dsobject .process_image_bytes (MOCK_BYTES )
58+ assert dsobject .predictions == MOCK_PREDICTIONS
59+
60+
61+ def test_DeepstackObject_process_image_bytes_timeout ():
62+ """Test a timeout. THIS SHOULD FAIL"""
63+ with pytest .raises (ds .DeepstackException ) as excinfo :
64+ with requests_mock .Mocker () as mock_req :
65+ mock_req .post (MOCK_URL , exc = requests .exceptions .ConnectTimeout )
66+ dsobject = ds .DeepstackObject (MOCK_IP_ADDRESS , MOCK_PORT )
67+ dsobject .process_image_bytes (MOCK_BYTES )
68+ assert False
69+ assert "SHOULD FAIL" in str (excinfo .value )
70+
71+
72+ def test_get_object_labels ():
73+ """Cant always be sure order of returned list items."""
74+ object_labels = ds .get_object_labels (MOCK_PREDICTIONS )
75+ assert type (object_labels ) is list
76+ assert "dog" in object_labels
77+ assert "person" in object_labels
78+ assert len (object_labels ) == 2
79+
80+
81+ def test_get_objects_summary ():
82+ objects_summary = ds .get_objects_summary (MOCK_PREDICTIONS )
83+ assert objects_summary == {"dog" : 1 , "person" : 2 }
84+
85+
86+ def test_get_label_confidences ():
87+ label_confidences = ds .get_label_confidences (MOCK_PREDICTIONS , "person" )
88+ assert label_confidences == MOCK_CONFIDENCES
89+
90+
91+ def test_get_confidences_above_threshold ():
92+ assert (
93+ len (ds .get_confidences_above_threshold (MOCK_CONFIDENCES , CONFIDENCE_THRESHOLD ))
94+ == 1
95+ )
0 commit comments