def create_dataset(self):
        """
        Creates a new dataset
        :return:
        """
        request = self._get_validated_request()

        if 'name' not in request:
            raise InvalidRequest("Name is required for a new dataset", status_code=400)

        name = request['name']

        if 'dataset_type' in request:
            if request['dataset_type'] not in DATASET_TYPES:
                raise InvalidRequest("The specified type of dataset is not valid.")

            dataset_type = DATASET_TYPES[request['dataset_type']]

        else:
            dataset_type = GenericDataset

        logging.info("Creating dataset with name {}".format(name))

        if self.dataset_factory.create_dataset(name, dataset_type=dataset_type):
            logging.info("Dataset with name {} initialized".format(name))
        else:
            logging.error("Dataset name {} is already taken".format(name))
            raise InvalidRequest("Name already taken.", 401)

        return ""
    def make_web_screenshot(self):
        """
        Retrieves the screenshot for the specified page
        """
        json_request = request.get_json(force=True, silent=True, cache=False)

        try:
            url = json_request['url']

        except Exception as ex:
            raise InvalidRequest("url is missing in the request JSON.")

        if not (url.lower().startswith("http://")
                or url.lower().startswith("https://")):
            raise InvalidRequest(
                "Specified URL is not valid, must start with http:// or https://"
            )

        service = self.available_services['web_screenshoot_processor']

        promise = service.queue_request(url)

        binary_result = promise.get_result()

        img_io = BytesIO(binary_result)
        img_io.seek(0)

        return send_file(img_io, mimetype='image/jpeg')
    def get_batch(self, batch_id):

        service = self.available_services['batch_screenshoot_processor']

        try:
            zip_uri = service.get_batch_zip_uri(batch_id)
            percentage, is_zipped = service.get_processed_percentage(batch_id)
        except:
            raise InvalidRequest(
                "Batch ID not valid or not available for cancelling.")

        if not is_zipped:
            raise InvalidRequest("Batch is not ready yet.")

        return send_file(zip_uri)
    def batch_web_screenshot(self):

        json_request = request.get_json(force=True, silent=True, cache=False)

        try:
            urls = json_request['urls']

        except Exception as ex:
            raise InvalidRequest("url is missing in the request JSON.")

        service = self.available_services['batch_screenshoot_processor']

        if len(urls) == 0:
            raise InvalidRequest("Required at least 1 URL in the 'url' list")

        batch_id = service.new_batch(urls)

        return jsonify({"batch_id": batch_id})
Пример #5
0
    def _validate_float(var_name, float_number):
        """
        Checks whether the specified parameter is a number or not. If not, it will raise a exception.
        :param var_name: Name of the var to show in the exception (in order to reference the error).
        :param number: Number to validate.
        """

        try:
            float(float_number)  # for int, long and float
        except ValueError:
            raise InvalidRequest(
                "The parameter {} is not a valid float.".format(var_name))
    def remove_batch(self, batch_id):

        service = self.available_services['batch_screenshoot_processor']

        try:
            service.remove_batch(batch_id)
        except Exception as ex:
            raise InvalidRequest(
                "Batch ID not valid or not available for removing. {}".format(
                    ex))

        return jsonify({'status': "done"})
Пример #7
0
    def _get_raw_content_validated(is_base64=False):
        """
        Validates the content of the request to ensure that it is readable and returns it.
        Override this method to change or extend the default validation of the incoming request.

        :param is_base64:   specifies if the content is encoded in base64 or not. In case it is encoded, it will try
        to decode it.
        :return:    the raw content (decoded content in case of base64)
        """
        content = request.stream.read()

        if len(content) == 0:
            raise InvalidRequest("Request without content.")

        if is_base64:
            try:
                content = base64.b64decode(content)
            except Exception as ex:
                raise InvalidRequest("Content is not valid Base64.")

        return content
Пример #8
0
    def _retrieve_result_metadata(resource_result):
        """
        Applies the algorithm of the service to the resource and gets the result.
        :param resource_result: Result of the process from a service.
        :return: the metadata result of the process.
        """

        if resource_result.get_uri() == 'error':
            raise InvalidRequest("Content of file not valid: {}".format(
                resource_result.get_id()))

        return resource_result.get_metadata()
    def get_batch_status(self, batch_id):

        service = self.available_services['batch_screenshoot_processor']

        try:
            percentage, is_zipped = service.get_processed_percentage(batch_id)
        except Exception as ex:
            raise InvalidRequest(
                "Batch ID not valid or not available for checking.")

        return jsonify({
            'percentage_completed': percentage,
            'is_zipped': is_zipped
        })
Пример #10
0
    def _get_common__parameters(self, should_have_uri=False):
        """
        Retrieves the common parameters for face ensemble requests.
        :param should_have_uri: if the flag is True, the validation of the request will check that an URI is
        present inside the request. This is a flag forwarded to _get_validated_request() method.
        :return: request, service_face_detection, service_age_estimation,
                 service_gender_estimation, work_in_gray, limit_estimations,
                 bounding_box_expansion
        """
        face_controller = age_controller = gender_controller = None

        request = FaceEnsembleController._get_validated_request(
            should_have_uri=should_have_uri)

        limit_estimations = int(request.get('limit_estimations', 3))
        bounding_box_expansion = float(
            request.get('bounding_box_expansion', 0.8))
        work_in_gray = request.get('work_in_gray', "true") == "true"

        self._validate_number("limit_estimations", limit_estimations)
        self._validate_float("bounding_box_expansion", bounding_box_expansion)

        service_face_name = request.get('service_face', 'default')
        service_age_name = request.get('service_age', 'default')
        service_gender_name = request.get('service_gender', 'default')

        if 'face_detection_controller' in self.controllers_dict:
            face_controller = self.controllers_dict[
                'face_detection_controller']
        if 'age_estimation_controller' in self.controllers_dict:
            age_controller = self.controllers_dict['age_estimation_controller']
        if 'gender_estimation_controller' in self.controllers_dict:
            gender_controller = self.controllers_dict[
                'gender_estimation_controller']

        service_face_detection = self._get_most_suitable_service(
            service_face_name, face_controller)
        service_age_estimation = self._get_most_suitable_service(
            service_age_name, age_controller)
        service_gender_estimation = self._get_most_suitable_service(
            service_gender_name, gender_controller)

        if not service_face_detection:
            raise InvalidRequest("No services for face detection found.")

        return request, service_face_detection, service_age_estimation, \
               service_gender_estimation, work_in_gray, limit_estimations, \
               bounding_box_expansion
Пример #11
0
    def _get_validated_request(should_have_uri=False):
        """
        Validates the request to fit a basic protocol. If the request does not fit the protocol, it will raise
        an exception.
        Override this method to change or extend the default validation of the incoming request.

        :param should_have_uri: flag to specify if the request should contain an URI or not.
        :return: the request validated.
        """

        request_args = request.args

        if should_have_uri and request_args.get("uri", '') == '':
            raise InvalidRequest("Request URI is missing.")

        return request_args
Пример #12
0
    def _validate_number(var_name, number):
        """
        Checks whether the specified parameter is a number or not. If not, it will raise a exception.
        :param var_name: Name of the var to show in the exception (in order to reference the error).
        :param number: Number to validate.
        """

        try:
            num = int(number)  # for int, long and float

            if num < 0:
                raise ValueError("Negative integers not allowed.")

        except ValueError:
            raise InvalidRequest(
                "The parameter {} is not a valid natural integer.".format(
                    var_name))
Пример #13
0
    def _get_most_suitable_service(self, service_name):
        """
        Searchs for the most suitable service for the requested service name.
        If a service name is provided but no services matches it, it will return an InvalidRequest exception.
        :param service_name: name of the service to match. If it is 'default', it will fall back to the
                             default service for this controller.
        :return: the most suitable service for the service name.
        """

        if service_name == "default":
            service = self.default_service

        else:
            if service_name not in self.available_services:
                raise InvalidRequest(
                    "Service name '{}' not valid.".format(service_name))

            service = self.available_services[service_name]

        return service
    def _crop_by_bounding_box(image, bounding_box_text=None):
        """
        Crops the image by the specified bounding box. If no bounding box is specified, the image
        will be returned without any filtering.
        :param image: image to crop.
        :param bounding_box_text: bounding box to crop by. None to disable cropping.
        :return: the image filtered or the original image in case the bounding box is None.
        """

        if bounding_box_text is not None:
            try:
                bounding_box_text = BoundingBox.from_string(bounding_box_text)
                bounding_box_text.fit_in_size(image.get_size())
                image = image.crop_image(bounding_box_text,
                                         "Cropped by boundingbox")

            except Exception as ex:
                raise InvalidRequest(
                    "The bounding box format is not valid. "
                    "Format must be: bounding_box=x,y,width,height")

        return image
Пример #15
0
 def custom_route_exception_example(self):
     """
     Retrieves the services available for face detection.
     """
     raise InvalidRequest("Raising exception as a test", status_code=505)