简体   繁体   中英

Unable to display image using matplotlib.pyplot on ipython project

I am trying to run FienSoP/canny_edge_detector code on github with my own directory. But instead of showing the figure it shows figure size().

canny_Edge_Detector.ipynb

%matplotlib inline
from utils import utils
import canny_edge_detector as ced

imgs = utils.load_data('⁨/Users⁩/office⁩/Mask_RCNN⁩/office/images')
utils.visualize(imgs, 'gray')
<Figure size 1440x2880 with 0 Axes>

util.py

import numpy as np
import skimage
import matplotlib.pyplot as plt 
import matplotlib.image as mpimg
import os
import scipy.misc as sm
import glob

def rgb2gray(rgb):

    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

def load_data(dir_name):  
    imgs = []
    data_path = os.path.join(dir_name,'*g')
    files = glob.glob(data_path)
    for filename in files:
        img = mpimg.imread(filename)
        img = rgb2gray(img)
        imgs.append(img)
    return imgs


def visualize(imgs, format=None, gray=False):
    plt.figure(figsize=(20, 40))
    for i, img in enumerate(imgs):
        if img.shape[0] == 3:
            img = img.transpose(1,2,0)
        plt_idx = i+1
        plt.subplot(len(imgs), 1, plt_idx)
        plt.imshow(img, format)
    plt.show()

Since, I am a beginner described answers will be very helpful. Thank you in advance.

Need to use %matplotlib inline with iPython

%matplotlib inline 
import matplotlib.pyplot as plt
plt.plot([1,2,3])

See: Plotting with Matplotlib .

copying the string

'/Users/office/Mask_RCNN/office/images'

that you provide in your question, directly from the question, into the python console, gives the following:

'\xe2\x81\xa8/Users\xe2\x81\xa9/office\xe2\x81\xa9/Mask_RCNN\xe2\x81\xa9/office/images'

So, the source that you originally copied this string from adds some zero-width characters, that are not visible when you paste the string into a browser, but cause python to look for you data in a weird location. This is a related problem.

You should be fine if you manually retype the string (or copy+paste the top string in my answer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM