def handle_cli(self, args, func): parser = argparse.ArgumentParser() parser.add_argument("--input", required=True) parser.add_argument("-o", "--output", default="str", choices=["str", "json", "yaml"]) parsed_args = parser.parse_args(args) file_path = parsed_args.input check_file_format(file_path, self.accept_file_extensions) if not os.path.isabs(file_path): file_path = os.path.abspath(file_path) try: from fastai.vision import open_image, Image except ImportError: raise ImportError("fastai package is required to use") image_array = open_image( fn=file_path, convert_mode=self.convert_mode, div=self.div, after_open=self.after_open, cls=self.cls or Image, ) result = func(image_array) result = get_output_str(result, output_format=parsed_args.output) print(result)
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") if request.method != "POST": return Response(response="Only accept POST request", status=400) input_streams = [] for filename in self.input_names: file = request.files.get(filename) if file is not None: file_name = secure_filename(file.filename) check_file_format(file_name, self.accept_file_extensions) input_streams.append(BytesIO(file.read())) if len(input_streams) == 0: if request.data: input_streams = (request.data, ) else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request) input_data = [] for input_stream in input_streams: data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: data = self.after_open(data) data = pil2tensor(data, np.float32) if self.div: data = data.div_(255) if self.cls: data = self.cls(data) else: data = Image(data) input_data.append(data) result = func(*input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor import numpy as np except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") return if request.method != "POST": return Response(response="Only accept POST request", status=400) input_file = request.files.get(self.input_name) file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_stream = BytesIO(input_file.read()) input_data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: input_data = self.after_open(input_data) input_data = pil2tensor(input_data, np.float32) if self.div: input_data = input_data.div_(255) if self.cls: input_data = self.cls(input_data) else: input_data = Image(input_data) result = func(input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")