diff --git a/deepstack/core.py b/deepstack/core.py index 3c41827..f4df744 100644 --- a/deepstack/core.py +++ b/deepstack/core.py @@ -10,6 +10,7 @@ DEFAULT_TIMEOUT = 10 # seconds DEFAULT_IP = "localhost" DEFAULT_PORT = 80 +DEFAULT_MIN_CONFIDENCE = 0.45 ## HTTP codes HTTP_OK = 200 @@ -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 ) @@ -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: @@ -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.""" @@ -170,6 +176,7 @@ 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: @@ -177,7 +184,12 @@ def __init__( 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): @@ -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"] @@ -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, ) @@ -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"] @@ -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, @@ -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"] @@ -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}, ) @@ -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, ) diff --git a/usage-face-recognition.ipynb b/usage-face-recognition.ipynb index ac1ad73..d34a2af 100644 --- a/usage-face-recognition.ipynb +++ b/usage-face-recognition.ipynb @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -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" ] } ], @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -128,7 +128,7 @@ " 'x_max': 1988}]" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -139,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -148,7 +148,7 @@ "2" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -175,8 +175,8 @@ "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" ] } ], @@ -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" ] } ], @@ -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", @@ -243,7 +243,7 @@ " 'x_max': 1988}]" ] }, - "execution_count": 11, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -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" } diff --git a/usage-object-detection.ipynb b/usage-object-detection.ipynb index 39baa0b..01316b4 100644 --- a/usage-object-detection.ipynb +++ b/usage-object-detection.ipynb @@ -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" ] }, { @@ -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)" ] }, { @@ -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" ] } ], @@ -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", @@ -167,7 +180,7 @@ { "data": { "text/plain": [ - "['dog', 'person']" + "['dog', 'frisbee', 'handbag', 'person']" ] }, "execution_count": 7, @@ -194,7 +207,7 @@ { "data": { "text/plain": [ - "{'dog': 1, 'person': 2}" + "{'dog': 1, 'frisbee': 1, 'handbag': 1, 'person': 2}" ] }, "execution_count": 8, @@ -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" ] } ], diff --git a/usage-scene-detection.ipynb b/usage-scene-detection.ipynb index e9d405f..7726c43 100644 --- a/usage-scene-detection.ipynb +++ b/usage-scene-detection.ipynb @@ -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" ] } ],