Skip to content

Commit

Permalink
Merge pull request #34 from robmarkcole/Support-min_confidence
Browse files Browse the repository at this point in the history
add min_confidence
  • Loading branch information
robmarkcole committed Dec 18, 2020
2 parents 333cfd0 + a05f1ca commit 0eb1091
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 42 deletions.
41 changes: 31 additions & 10 deletions deepstack/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DEFAULT_TIMEOUT = 10 # seconds
DEFAULT_IP = "localhost"
DEFAULT_PORT = 80
DEFAULT_MIN_CONFIDENCE = 0.45

## HTTP codes
HTTP_OK = 200
Expand Down Expand Up @@ -92,11 +93,10 @@ def get_objects_summary(predictions: List[Dict]):


def post_image(
url: str, image_bytes: bytes, api_key: str, timeout: int, data: dict = {}
url: str, image_bytes: bytes, timeout: int, data: dict
) -> requests.models.Response:
"""Post an image to Deepstack. Only handles exceptions."""
try:
data["api_key"] = api_key # Insert the api_key
return requests.post(
url, files={"image": image_bytes}, data=data, timeout=timeout
)
Expand All @@ -111,12 +111,17 @@ def post_image(


def process_image(
url: str, image_bytes: bytes, api_key: str, timeout: int, data: dict = {}
url: str,
image_bytes: bytes,
api_key: str,
min_confidence: float,
timeout: int,
data: dict = {},
) -> Dict:
"""Process image_bytes and detect. Handles common status codes"""
response = post_image(
url=url, image_bytes=image_bytes, api_key=api_key, timeout=timeout, data=data
)
data["api_key"] = api_key
data["min_confidence"] = min_confidence
response = post_image(url=url, image_bytes=image_bytes, timeout=timeout, data=data)
if response.status_code == HTTP_OK:
return response.json()
elif response.status_code == BAD_URL:
Expand All @@ -136,17 +141,18 @@ def __init__(
port: int = DEFAULT_PORT,
api_key: str = DEFAULT_API_KEY,
timeout: int = DEFAULT_TIMEOUT,
min_confidence: float = DEFAULT_MIN_CONFIDENCE,
url_detect: str = "",
url_recognize: str = "",
url_register: str = "",
):

self._api_key = api_key
self._timeout = timeout
self._min_confidence = min_confidence
self._url_base = URL_BASE_VISION.format(ip=ip, port=port)
self._url_detect = self._url_base + url_detect
self._url_recognize = self._url_base + url_recognize
self._url_register = self._url_base + url_register
self._api_key = api_key
self._timeout = timeout

def detect(self):
"""Process image_bytes and detect."""
Expand All @@ -170,14 +176,20 @@ def __init__(
port: int = DEFAULT_PORT,
api_key: str = DEFAULT_API_KEY,
timeout: int = DEFAULT_TIMEOUT,
min_confidence: float = DEFAULT_MIN_CONFIDENCE,
custom_model: str = "",
):
if custom_model:
url_detect = URL_CUSTOM.format(custom_model=custom_model)
else:
url_detect = URL_OBJECT_DETECTION
super().__init__(
ip=ip, port=port, api_key=api_key, timeout=timeout, url_detect=url_detect,
ip=ip,
port=port,
api_key=api_key,
timeout=timeout,
min_confidence=min_confidence,
url_detect=url_detect,
)

def detect(self, image_bytes: bytes):
Expand All @@ -186,6 +198,7 @@ def detect(self, image_bytes: bytes):
url=self._url_detect,
image_bytes=image_bytes,
api_key=self._api_key,
min_confidence=self._min_confidence,
timeout=self._timeout,
)
return response["predictions"]
Expand All @@ -200,12 +213,14 @@ def __init__(
port: int = DEFAULT_PORT,
api_key: str = DEFAULT_API_KEY,
timeout: int = DEFAULT_TIMEOUT,
min_confidence: float = DEFAULT_MIN_CONFIDENCE,
):
super().__init__(
ip=ip,
port=port,
api_key=api_key,
timeout=timeout,
min_confidence=min_confidence,
url_recognize=URL_SCENE_RECOGNIZE,
)

Expand All @@ -215,6 +230,7 @@ def recognize(self, image_bytes: bytes):
url=self._url_recognize,
image_bytes=image_bytes,
api_key=self._api_key,
min_confidence=self._min_confidence,
timeout=self._timeout,
)
del response["success"]
Expand All @@ -230,12 +246,14 @@ def __init__(
port: int = DEFAULT_PORT,
api_key: str = DEFAULT_API_KEY,
timeout: int = DEFAULT_TIMEOUT,
min_confidence: float = DEFAULT_MIN_CONFIDENCE,
):
super().__init__(
ip=ip,
port=port,
api_key=api_key,
timeout=timeout,
min_confidence=min_confidence,
url_detect=URL_FACE_DETECTION,
url_register=URL_FACE_REGISTER,
url_recognize=URL_FACE_RECOGNIZE,
Expand All @@ -247,6 +265,7 @@ def detect(self, image_bytes: bytes):
url=self._url_detect,
image_bytes=image_bytes,
api_key=self._api_key,
min_confidence=self._min_confidence,
timeout=self._timeout,
)
return response["predictions"]
Expand All @@ -259,6 +278,7 @@ def register(self, name: str, image_bytes: bytes):
url=self._url_register,
image_bytes=image_bytes,
api_key=self._api_key,
min_confidence=self._min_confidence,
timeout=self._timeout,
data={"userid": name},
)
Expand All @@ -278,6 +298,7 @@ def recognize(self, image_bytes: bytes):
url=self._url_recognize,
image_bytes=image_bytes,
api_key=self._api_key,
min_confidence=self._min_confidence,
timeout=self._timeout,
)

Expand Down
42 changes: 21 additions & 21 deletions usage-face-recognition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -87,15 +87,15 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 11.7 ms, sys: 10.4 ms, total: 22.1 ms\n",
"Wall time: 793 ms\n"
"CPU times: user 17.5 ms, sys: 26.5 ms, total: 44 ms\n",
"Wall time: 3.32 s\n"
]
}
],
Expand All @@ -110,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -128,7 +128,7 @@
" 'x_max': 1988}]"
]
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -139,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -148,7 +148,7 @@
"2"
]
},
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -167,16 +167,16 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"face updated\n",
"CPU times: user 6.61 ms, sys: 4.41 ms, total: 11 ms\n",
"Wall time: 703 ms\n"
"CPU times: user 5.15 ms, sys: 4.79 ms, total: 9.94 ms\n",
"Wall time: 1.17 s\n"
]
}
],
Expand All @@ -200,15 +200,15 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4.9 ms, sys: 3 ms, total: 7.91 ms\n",
"Wall time: 1.56 s\n"
"CPU times: user 5.39 ms, sys: 3.95 ms, total: 9.34 ms\n",
"Wall time: 2.38 s\n"
]
}
],
Expand All @@ -223,14 +223,14 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'confidence': 0,\n",
" 'userid': 'unknown',\n",
"[{'confidence': 0.58644783,\n",
" 'userid': 'idris',\n",
" 'y_min': 226,\n",
" 'x_min': 871,\n",
" 'y_max': 728,\n",
Expand All @@ -243,7 +243,7 @@
" 'x_max': 1988}]"
]
},
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -264,16 +264,16 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Idris Elba': 73.3}"
"{'idris': 58.6, 'Idris Elba': 73.3}"
]
},
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
31 changes: 22 additions & 9 deletions usage-object-detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"IP = 'localhost'\n",
"PORT = '80'\n",
"API_KEY = \"\"\n",
"TIMEOUT = 20 # Default is 10"
"TIMEOUT = 20 # Default is 10\n",
"MIN_CONFIDENCE = 0.01 # Default is 0.45"
]
},
{
Expand All @@ -50,7 +51,7 @@
"metadata": {},
"outputs": [],
"source": [
"dsobject = ds.DeepstackObject(ip=IP, port=PORT, api_key=API_KEY, timeout=TIMEOUT)"
"dsobject = ds.DeepstackObject(ip=IP, port=PORT, api_key=API_KEY, timeout=TIMEOUT, min_confidence=MIN_CONFIDENCE)"
]
},
{
Expand Down Expand Up @@ -93,8 +94,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 7.59 ms, sys: 4.3 ms, total: 11.9 ms\n",
"Wall time: 442 ms\n"
"CPU times: user 8.8 ms, sys: 5.8 ms, total: 14.6 ms\n",
"Wall time: 1.33 s\n"
]
}
],
Expand All @@ -115,7 +116,19 @@
{
"data": {
"text/plain": [
"[{'confidence': 0.90210795,\n",
"[{'confidence': 0.028640902,\n",
" 'label': 'handbag',\n",
" 'y_min': 114,\n",
" 'x_min': 444,\n",
" 'y_max': 522,\n",
" 'x_max': 605},\n",
" {'confidence': 0.037546907,\n",
" 'label': 'frisbee',\n",
" 'y_min': 309,\n",
" 'x_min': 419,\n",
" 'y_max': 333,\n",
" 'x_max': 437},\n",
" {'confidence': 0.90210795,\n",
" 'label': 'dog',\n",
" 'y_min': 348,\n",
" 'x_min': 650,\n",
Expand Down Expand Up @@ -167,7 +180,7 @@
{
"data": {
"text/plain": [
"['dog', 'person']"
"['dog', 'frisbee', 'handbag', 'person']"
]
},
"execution_count": 7,
Expand All @@ -194,7 +207,7 @@
{
"data": {
"text/plain": [
"{'dog': 1, 'person': 2}"
"{'dog': 1, 'frisbee': 1, 'handbag': 1, 'person': 2}"
]
},
"execution_count": 8,
Expand Down Expand Up @@ -316,8 +329,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4.19 ms, sys: 2.03 ms, total: 6.22 ms\n",
"Wall time: 273 ms\n"
"CPU times: user 4.84 ms, sys: 2.68 ms, total: 7.53 ms\n",
"Wall time: 1.08 s\n"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions usage-scene-detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 7.43 ms, sys: 3.98 ms, total: 11.4 ms\n",
"Wall time: 300 ms\n"
"CPU times: user 7.54 ms, sys: 3.85 ms, total: 11.4 ms\n",
"Wall time: 423 ms\n"
]
}
],
Expand Down

0 comments on commit 0eb1091

Please sign in to comment.