Ejemplo n.º 1
0
    def treat_inputs(self, base_command, request, arguments):
        """Treats gRPC inputs and assembles lua command. Specifically, checks if required field have been specified,
        if the values and types are correct and, for each input/input_type adds the argument to the lua command."""

        # Base command is the prefix of the command (e.g.: 'th test.lua ')
        file_index_str = ""
        command = base_command
        for field, values in arguments.items():
            var_type = values[0]
            # required = values[1] Not being used now but required for future automation steps
            default = values[2]

            # Tries to retrieve argument from gRPC request
            try:
                arg_value = eval("request.{}".format(field))
            except Exception as e:  # AttributeError if trying to access a field that hasn't been specified.
                log.error(e)
                return False

            print("Received request.{} = ".format(field))
            print(arg_value)

            # Deals with each field (or field type) separately. This is very specific to the lua command required.
            # If fields
            if field == "content":
                assert (request.content !=
                        ""), "Content image path should not be empty."
                image_path, file_index_str = service.treat_image_input(
                    arg_value, self.temp_dir, "{}".format(field))
                self.created_images.append(image_path)
                command += "-{} {} ".format(field, image_path)
            elif field == "style":
                assert (request.content !=
                        ""), "Style image path should not be empty."
                image_path, file_index_str = service.treat_image_input(
                    arg_value, self.temp_dir, "{}".format(field))
                self.created_images.append(image_path)
                command += "-{} {} ".format(field, image_path)
            elif field == "alpha":
                if arg_value == 0.0:
                    continue
                else:
                    try:
                        float_alpha = float(arg_value)
                    except Exception as e:
                        log.error(e)
                        return False
                    if float_alpha < 0.0 or float_alpha > 1.0:
                        log.error(
                            "Argument alpha should be a real number between 0 and 1."
                        )
                        return False
                    command += "-{} {} ".format(field,
                                                str(round(float_alpha, 2)))
            elif field == "saveExt":
                arg_value = arg_value.lower()
                if arg_value == "":
                    command += "-{} {} ".format(field, default)
                else:
                    if (arg_value == "jpg") or (arg_value == "png"):
                        command += "-{} {} ".format(field, arg_value)
                    else:
                        log.error(
                            "Field saveExt should either be jpg or png. Provided: {}."
                            .format(arg_value))
                        return False
            else:
                # If types
                if var_type == "bool":
                    if eval("request.{}".format(field)):
                        command += "-{} ".format(field)
                elif var_type == "int":
                    try:
                        int(eval("request.{}".format(field)))
                    except Exception as e:
                        log.error(e)
                    command += "-{} {} ".format(
                        field, eval("request.{}".format(field)))
        return command, file_index_str
Ejemplo n.º 2
0
    def treat_inputs(self, request, arguments, created_images):
        """Treats gRPC inputs and assembles lua command. Specifically, checks if required field have been specified,
        if the values and types are correct and, for each input/input_type adds the argument to the lua command."""

        model_path = self.model_dir
        # Base command is the prefix of the command (e.g.: 'th test.lua ')
        file_index_str = ""
        image_path = ""
        for field, values in arguments.items():
            # var_type = values[0]
            # required = values[1] Not being used now but required for future automation steps
            default = values[2]

            # Tries to retrieve argument from gRPC request
            try:
                arg_value = eval("request.{}".format(field))
            except Exception as e:  # AttributeError if trying to access a field that hasn't been specified.
                log.error(e)
                return False

            print("Received request.{} = ".format(field))
            print(arg_value)

            # Deals with each field (or field type) separately. This is very specific to the lua command required.
            if field == "input":
                log.debug("Treating input image field.")
                assert (request.input !=
                        ""), "Input image field should not be empty."
                try:
                    image_path, file_index_str = \
                        service.treat_image_input(arg_value, self.input_dir, "{}".format(field))
                    print("Image path: {}".format(image_path))
                    created_images.append(image_path)
                except Exception as e:
                    log.error(e)
                    raise
            elif field == "model":
                log.debug("Treating model field. Forcing model to be ESRGAN.")
                # if request.model == "ESRGAN":
                model_path += self.esrgan_model
                # else:
                #     log.error("Input field model not recognized. For now, only \"ESRGAN\" is accepted. Got: {}"
                #               .format(request.model))
            elif field == "scale":
                log.debug("Treating scale field. Forcing scale to be 4.")
                scale = 4
                model_path += str(scale)

                # If empty, fill with default, else check if valid
                # if request.scale == 0 or request.scale == "":
                #     scale = default
                # else:
                #     try:
                #         scale = int(request.scale)
                #     except Exception as e:
                #         log.error(e)
                #         raise
                # if scale in self.scale_dict[request.model]:
                #     model_path += str(scale)
                # else:
                #     log.error('Scale invalid. Should be one of {}.'.format(self.scale_dict[request.model]))
            else:
                log.error("Request field not found.")
                return False

        if image_path == "":
            log.error(
                "Empty image_path (filename). Something went wrong when treating input."
            )
        model_path += self.model_suffix

        log.debug("Successfully treated input.")

        return image_path, model_path, file_index_str
    def treat_inputs(self, request, arguments, created_images):
        """Treats gRPC inputs and assembles lua command. Specifically, checks if required field have been specified,
        if the values and types are correct and, for each input/input_type adds the argument to the lua command."""

        file_index_str = ""
        image_path = ""
        window_size = 0
        stride = 0

        for field, values in arguments.items():
            # var_type = values[0]
            # required = values[1] Not being used now but required for future automation steps
            default = values[2]

            # Tries to retrieve argument from gRPC request
            try:
                arg_value = eval("request.{}".format(field))
            except Exception as e:  # AttributeError if trying to access a field that hasn't been specified.
                log.error(e)
                return False

            if field == "input":
                log.debug("Received input image data.")
            else:
                log.debug("Received request.{} = {}".format(field, arg_value))

            # Deals with each field (or field type) separately.
            if field == "input":
                assert (request.input !=
                        ""), "Input image field should not be empty."
                try:
                    image_path, file_index_str = \
                        service.treat_image_input(arg_value, self.input_dir, "{}".format(field))
                    log.debug("Field: {}. Image path: {}".format(
                        field, image_path))
                    created_images.append(image_path)
                except Exception as e:
                    log.error(e)
                    raise
            elif field == "window_size":
                # If empty, fill with default, else check if valid
                if request.window_size == 0 or request.window_size == "":
                    window_size = default
                else:
                    try:
                        window_size = int(request.window_size)
                    except Exception as e:
                        log.error(e)
                        raise
            elif field == "stride":
                # If empty, fill with default, else check if valid
                if request.stride == 0 or request.stride == "":
                    stride = default
                else:
                    try:
                        stride = int(request.stride)
                    except Exception as e:
                        log.error(e)
                        raise
                if stride > request.window_size:
                    log.error('Stride must be smaller than window size')
            else:
                log.error("Error. Required request field not found.")
                return False

        return image_path, window_size, stride, file_index_str
Ejemplo n.º 4
0
    def treat_inputs(self, base_command, request, arguments, created_images):
        """Treats gRPC inputs and assembles lua command. Specifically, checks if required field have been specified,
        if the values and types are correct and, for each input/input_type adds the argument to the lua command."""

        model_path = self.model_dir
        # Base command is the prefix of the command (e.g.: 'th test.lua ')
        file_index_str = ""
        command = base_command
        for field, values in arguments.items():
            # var_type = values[0]
            # required = values[1] Not being used now but required for future automation steps
            default = values[2]

            # Tries to retrieve argument from gRPC request
            try:
                arg_value = eval("request.{}".format(field))
            except Exception as e:  # AttributeError if trying to access a field that hasn't been specified.
                log.error(e)
                return False

            print("Received request.{} = ".format(field))
            print(arg_value)

            # Deals with each field (or field type) separately. This is very specific to the lua command required.
            if field == "input":
                assert (request.input !=
                        ""), "Input image field should not be empty."
                try:
                    image_path, file_index_str = \
                        service.treat_image_input(arg_value, self.input_dir, "{}".format(field))
                    print("Image path: {}".format(image_path))
                    created_images.append(image_path)
                    command += "--{} {} ".format(field, image_path)
                except Exception as e:
                    log.error(e)
                    raise
            elif field == "model":
                if request.model == "proSR":
                    model_path += self.prosr_model
                elif request.model == "proSRGAN":
                    model_path += self.prosrgan_model
                else:
                    log.error(
                        "Input field model not recognized. Should be either \"proSR\" or \"proSRGAN\". Got: {}"
                        .format(request.model))
            elif field == "scale":
                # If empty, fill with default, else check if valid
                if request.scale == 0 or request.scale == "":
                    scale = default
                else:
                    try:
                        scale = int(request.scale)
                    except Exception as e:
                        log.error(e)
                        raise
                if scale not in self.scale_dict[request.model]:
                    log.error('Scale invalid. Should be one of {}.'.format(
                        self.scale_dict[request.model]))
                str_scale = str(scale)
                model_path += str_scale + self.model_suffix
                command += "--checkpoint {} --{} {} ".format(
                    model_path, field, str_scale)
            else:
                log.error("Command assembly error. Request field not found.")
                return False

        return command, file_index_str