Ниже код показывает ошибку. - PullRequest
0 голосов
/ 05 февраля 2020

Ниже мой код. Эта реализация показывает ошибки, подробно описанные в конце. Пожалуйста, разрешите проблемы. Этот код является частью системы подписи изображений.

 from __future__ import print_function
        from future import standard_library
        standard_library.install_aliases()
        from builtins import range
        import urllib.request, urllib.error, urllib.parse, os, tempfile

        import numpy as Np
        from scipy.misc.pilutil import imread, imresize

    """
    Utility functions used for viewing and processing images.
    """

        def blur_image(X):
        """
        A very gentle image blurring operation, to be used as a regularizer for
        image generation.

        Inputs:
        - X: Image data of shape (N, 3, H, W)

        Returns:
        - X_blur: Blurred version of X, of shape (N, 3, H, W)
        """
        from cs231n.fast_layers import conv_forward_fast
        w_blur = np.zeros((3, 3, 3, 3))
        b_blur = np.zeros(3)
        blur_param = {'stride': 1, 'pad': 1}
        for i in range(3):
            w_blur[i, i] = np.asarray([[1, 2, 1], [2, 188, 2], [1, 2, 1]],
                                      dtype=np.float32)
        w_blur /= 200.0
        return conv_forward_fast(X, w_blur, b_blur, blur_param)[0]


        SQUEEZENET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
        SQUEEZENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)

        def preprocess_image(img):
            """Preprocess an image for squeezenet.

            Subtracts the pixel mean and divides by the standard deviation.
        """
            return (img.astype(np.float32)/255.0 - SQUEEZENET_MEAN) / SQUEEZENET_STD


       def deprocess_image(img, rescale=False):
            """Undo preprocessing on an image and convert back to uint8."""
            img = (img * SQUEEZENET_STD + SQUEEZENET_MEAN)
            if rescale:
                vmin, vmax = img.min(), img.max()
                img = (img - vmin) / (vmax - vmin)
            return np.clip(255 * img, 0.0, 255.0).astype(np.uint8)


       def image_from_url(url):
        """
        Read an image from a URL. Returns a numpy array with the pixel data.
        We write the image to a temporary file then read it back. Kinda gross.
        """
        try:
            f = urllib.request.urlopen(url)
            _, fname = tempfile.mkstemp()
            with open(fname, 'wb') as ff:
                ff.write(f.read())
            img = imread(fname)
            os.remove(fname)
            return img
        except urllib.error.URLError as e:
            print('URL Error: ', e.reason, url)
        except urllib.error.HTTPError as e:
            print('HTTP Error: ', e.code, url)


       def load_image(filename, size=None):
        """Load and resize an image from disk.

        Inputs:
        - filename: path to file
        - size: size of shortest dimension after rescaling
        """
        img = imread(filename)
        if size is not None:
            orig_shape = np.array(img.shape[:2])
            min_idx = np.argmin(orig_shape)
            scale_factor = float(size) / orig_shape[min_idx]
            new_shape = (orig_shape * scale_factor).astype(int)
            img = imresize(img, scale_factor)
        return img

Ниже приведена ошибка, которая отображается при выполнении кода выше:

    PermissionError                           Traceback (most recent call last)
    <ipython-input-4-ee88f13d9fc2> in <module>
          4 captions, features, urls = sample_coco_minibatch(data, batch_size=batch_size)
          5 for i, (caption, url) in enumerate(zip(captions, urls)):
    ----> 6     plt.imshow(image_from_url(url))
          7     plt.axis('off')
          8     caption_str = decode_captions(caption, data['idx_to_word'])

    ~\Desktop\Image-Captioning-master\image_utils.py in image_from_url(url)
         65             ff.write(f.read())
         66         img = imread(fname)
    ---> 67         os.remove(fname)
         68         return img
         69     except urllib.error.URLError as e:

    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\SASIKI~1\\AppData\\Local\\Temp\\tmpwyerl_qa'
...