Refactor DeepStack plugin with client initialization and closing

This commit is contained in:
Sergey Krashevich 2023-07-06 20:43:14 +03:00
parent c7d2fbeeb1
commit 35d23a0044
No known key found for this signature in database
GPG Key ID: 625171324E7D3856

View File

@ -32,6 +32,7 @@ class DeepStack(DetectionApi):
self.api_timeout = detector_config.api_timeout self.api_timeout = detector_config.api_timeout
self.api_key = detector_config.api_key self.api_key = detector_config.api_key
self.labels = detector_config.model.merged_labelmap self.labels = detector_config.model.merged_labelmap
self.client = httpx.Client()
def get_label_index(self, label_value): def get_label_index(self, label_value):
if label_value.lower() == "truck": if label_value.lower() == "truck":
@ -50,14 +51,14 @@ class DeepStack(DetectionApi):
image_bytes = output.getvalue() image_bytes = output.getvalue()
data = {"api_key": self.api_key} data = {"api_key": self.api_key}
try: try:
response = httpx.post( response = self.client.post(
self.api_url, self.api_url,
data=data, data=data,
files={"image": image_bytes}, files={"image": image_bytes},
timeout=self.api_timeout, timeout=self.api_timeout,
) )
response_json = response.json() response_json = response.json()
except httpx.RequestError as e: except (httpx.RequestError, httpx.TimeoutException) as e:
logger.error(f"An error occurred while making the request: {e}") logger.error(f"An error occurred while making the request: {e}")
return np.zeros((20, 6), np.float32) return np.zeros((20, 6), np.float32)
@ -85,3 +86,6 @@ class DeepStack(DetectionApi):
] ]
return detections return detections
def close(self):
self.client.close()