def __init__(
     self,
     ip_address,
     port,
     api_key,
     timeout,
     target,
     confidence,
     save_file_folder,
     camera_entity,
     name=None,
 ):
     """Init with the API key and model id."""
     super().__init__()
     self._dsobject = ds.DeepstackObject(ip_address, port, api_key, timeout)
     self._target = target
     self._confidence = confidence
     self._camera = camera_entity
     if name:
         self._name = name
     else:
         camera_name = split_entity_id(camera_entity)[1]
         self._name = "{} {}".format(CLASSIFIER, camera_name)
     self._state = None
     self._targets_confidences = []
     self._predictions = {}
     if save_file_folder:
         self._save_file_folder = save_file_folder
예제 #2
0
 def __init__(
     self,
     ip_address,
     port,
     api_key,
     timeout,
     target,
     confidence,
     save_file_folder,
     save_timestamped_file,
     camera_entity,
     name=None,
 ):
     """Init with the API key and model id."""
     super().__init__()
     self._dsobject = ds.DeepstackObject(ip_address, port, api_key, timeout)
     self._target = target
     self._confidence = confidence
     self._camera = camera_entity
     if name:
         self._name = name
     else:
         camera_name = split_entity_id(camera_entity)[1]
         self._name = "deepstack_object_{}".format(camera_name)
     self._state = None
     self._targets_confidences = [None] * len(self._target)
     self._targets_found = [0] * len(self._target)
     self._predictions = {}
     self._summary = {}
     self._last_detection = None
     self._image_width = None
     self._image_height = None
     if save_file_folder:
         self._save_file_folder = save_file_folder
     self._save_timestamped_file = save_timestamped_file
예제 #3
0
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)
예제 #4
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)
        predictions = dsobject.detect(MOCK_BYTES)
        assert predictions == MOCK_OBJECT_PREDICTIONS
예제 #5
0
 def __init__(self, config: Config):
     self._config = config
     self._detector = ds.DeepstackObject(
         ip=config.host,
         port=config.port,
         api_key=config.api_key,
         timeout=config.timeout,
         min_confidence=0.1,
         custom_model=config.custom_model,
     )
    def __init__(
        self,
        ip_address,
        port,
        api_key,
        timeout,
        targets,
        confidence,
        roi_y_min,
        roi_x_min,
        roi_y_max,
        roi_x_max,
        show_boxes,
        save_file_folder,
        save_timestamped_file,
        camera_entity,
        name=None,
    ):
        """Init with the API key and model id."""
        super().__init__()
        self._dsobject = ds.DeepstackObject(ip_address, port, api_key, timeout)
        self._targets = targets
        self._confidence = confidence
        self._camera = camera_entity
        if name:
            self._name = name
        else:
            camera_name = split_entity_id(camera_entity)[1]
            self._name = "deepstack_object_{}".format(camera_name)

        self._state = None
        self._objects = []  # The parsed raw data
        self._targets_found = []
        self._summary = {}

        self._roi_dict = {
            "y_min": roi_y_min,
            "x_min": roi_x_min,
            "y_max": roi_y_max,
            "x_max": roi_x_max,
        }

        self._show_boxes = show_boxes
        self._last_detection = None
        self._image_width = None
        self._image_height = None
        self._save_file_folder = save_file_folder
        self._save_timestamped_file = save_timestamped_file
    def __init__(
        self,
        ip_address,
        port,
        api_key,
        timeout,
        custom_model,
        targets,
        confidence,
        roi_y_min,
        roi_x_min,
        roi_y_max,
        roi_x_max,
        scale,
        show_boxes,
        save_file_folder,
        save_file_format,
        save_timestamped_file,
        always_save_latest_file,
        camera_entity,
        name=None,
    ):
        """Init with the API key and model id."""
        super().__init__()
        self._dsobject = ds.DeepstackObject(
            ip=ip_address,
            port=port,
            api_key=api_key,
            timeout=timeout,
            min_confidence=MIN_CONFIDENCE,
            custom_model=custom_model,
        )
        self._custom_model = custom_model
        self._confidence = confidence
        self._summary = {}
        self._targets = targets
        for target in self._targets:
            if CONF_CONFIDENCE not in target.keys():
                target.update({CONF_CONFIDENCE: self._confidence})
        self._targets_names = [target[CONF_TARGET] for target in targets
                               ]  # can be a name or a type
        self._camera = camera_entity
        if name:
            self._name = name
        else:
            camera_name = split_entity_id(camera_entity)[1]
            self._name = "deepstack_object_{}".format(camera_name)

        self._state = None
        self._objects = []  # The parsed raw data
        self._targets_found = []
        self._last_detection = None

        self._roi_dict = {
            "y_min": roi_y_min,
            "x_min": roi_x_min,
            "y_max": roi_y_max,
            "x_max": roi_x_max,
        }
        self._scale = scale
        self._show_boxes = show_boxes
        self._image_width = None
        self._image_height = None
        self._save_file_folder = save_file_folder
        self._save_file_format = save_file_format
        self._always_save_latest_file = always_save_latest_file
        self._save_timestamped_file = save_timestamped_file
        self._always_save_latest_file = always_save_latest_file
        self._image = None
예제 #8
0
)
ROI_DICT = {
    "x_min": ROI_X_MIN,
    "y_min": ROI_Y_MIN,
    "x_max": ROI_X_MAX,
    "y_max": ROI_Y_MAX,
}

## Process image
if img_file_buffer is not None:
    pil_image = Image.open(img_file_buffer)

else:
    pil_image = Image.open(TEST_IMAGE)

dsobject = ds.DeepstackObject(DEEPSTACK_IP, DEEPSTACK_PORT, DEEPSTACK_API_KEY,
                              DEEPSTACK_TIMEOUT)

predictions = process_image(pil_image, dsobject)
objects = utils.get_objects(predictions, pil_image.width, pil_image.height)
all_objects_names = set([obj["name"] for obj in objects])

# Filter objects for display
objects = [obj for obj in objects if obj["confidence"] > CONFIDENCE_THRESHOLD]
objects = [obj for obj in objects if obj["name"] in CLASSES_TO_INCLUDE]
objects = [
    obj for obj in objects if utils.object_in_roi(ROI_DICT, obj["centroid"])
]

# Draw object boxes
draw = ImageDraw.Draw(pil_image)
for obj in objects:
예제 #9
0
            options=const.CLASSES,
            default=const.CLASSES,
        )

    ## Process image
    if img_file_buffer is not None:
        pil_image = Image.open(img_file_buffer)

    else:
        pil_image = Image.open(OBJECT_TEST_IMAGE)

    if not DEEPSTACK_CUSTOM_MODEL:
        dsobject = ds.DeepstackObject(
            ip=DEEPSTACK_IP,
            port=DEEPSTACK_PORT,
            api_key=DEEPSTACK_API_KEY,
            timeout=DEEPSTACK_TIMEOUT,
            min_confidence=MIN_CONFIDENCE_THRESHOLD,
        )
    else:
        dsobject = ds.DeepstackObject(
            ip=DEEPSTACK_IP,
            port=DEEPSTACK_PORT,
            api_key=DEEPSTACK_API_KEY,
            timeout=DEEPSTACK_TIMEOUT,
            min_confidence=MIN_CONFIDENCE_THRESHOLD,
            custom_model=DEEPSTACK_CUSTOM_MODEL,
        )

    predictions = process_image_object(pil_image, dsobject)
    objects = utils.get_objects(predictions, pil_image.width, pil_image.height)