flask中的一些问题比如不能调试,keras读取其传入的图片出异常等
当flask在pycharm中不能断点调试是因为没开那个开关
当keras的代码无法读取上传的图片类型是
img = keras.preprocessing.image.load_img(f,target_size=(imageSize, imageSize), interpolation='bicubic')
上面的代码在有的机器上是好用的,有的机器上不好用,是因为keras.preprocessing.image.load_img的不同版本有改动导致的。
f的类型是 <FileStorage: 'small.jpg' ('application/octet-stream')>
因为keras Preprocessing版本高了,
pip install Keras-Preprocessing==1.1.0
实际原因是keras高版本的代码写得有问题
下面代码是没有问题的
try: from PIL import ImageEnhance from PIL import Image as pil_image except ImportError: pil_image = None ImageEnhance = None if pil_image is not None: _PIL_INTERPOLATION_METHODS = { 'nearest': pil_image.NEAREST, 'bilinear': pil_image.BILINEAR, 'bicubic': pil_image.BICUBIC, } # These methods were only introduced in version 3.4.0 (2016). if hasattr(pil_image, 'HAMMING'): _PIL_INTERPOLATION_METHODS['hamming'] = pil_image.HAMMING if hasattr(pil_image, 'BOX'): _PIL_INTERPOLATION_METHODS['box'] = pil_image.BOX # This method is new in version 1.1.3 (2013). if hasattr(pil_image, 'LANCZOS'): _PIL_INTERPOLATION_METHODS['lanczos'] = pil_image.LANCZOS def load_img(path, grayscale=False, color_mode='rgb', target_size=None, interpolation='nearest'): """Loads an image into PIL format. # Arguments path: Path to image file. grayscale: DEPRECATED use `color_mode="grayscale"`. color_mode: One of "grayscale", "rgb", "rgba". Default: "rgb". The desired image format. target_size: Either `None` (default to original size) or tuple of ints `(img_height, img_width)`. interpolation: Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. # Returns A PIL Image instance. # Raises ImportError: if PIL is not available. ValueError: if interpolation method is not supported. """ if grayscale is True: color_mode = 'grayscale' if pil_image is None: raise ImportError('Could not import PIL.Image. ' 'The use of `load_img` requires PIL.') img = pil_image.open(path) if color_mode == 'grayscale': if img.mode != 'L': img = img.convert('L') elif color_mode == 'rgba': if img.mode != 'RGBA': img = img.convert('RGBA') elif color_mode == 'rgb': if img.mode != 'RGB': img = img.convert('RGB') else: raise ValueError('color_mode must be "grayscale", "rgb", or "rgba"') if target_size is not None: width_height_tuple = (target_size[1], target_size[0]) if img.size != width_height_tuple: if interpolation not in _PIL_INTERPOLATION_METHODS: raise ValueError( 'Invalid interpolation method {} specified. Supported ' 'methods are {}'.format( interpolation, ", ".join(_PIL_INTERPOLATION_METHODS.keys()))) resample = _PIL_INTERPOLATION_METHODS[interpolation] img = img.resize(width_height_tuple, resample) return img
可以把这部分代码单独扣出来用。