Why i am getting "name 'os' is not defined" error? - python

Why i am getting this error? it should be work.Probally i'm missing something from my sight.
Before that same thing happen the for Classes.I tried rewrite and still same.
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
img_array = cv2.imread("Training/0/Training_233976.jpg")
img_array.shape
plt.imshow(img_array)
Datadirectory = "Training/"
Classes = ["0","1","2","3","4","5","6"]
for category in Classes:
path = os.path.join(Datadirectory, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
plt.imshow(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
plt.show()
break
break

I am formalizing in an answer, all you need to do is add a line at the top
import os

import os
use this at beginning of your code or where you are importing other libraries and codes .

Related

How do you load an image on VSC for Python

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('25.png')
plt.imshow(image)
I've used this code here to try and produce the image but it keeps saying that the module does not exist.
First, you should import 'matplotlib.pyplot'
Second, make sure the photo is in the same directory as your code is.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('25.png')
imgplot = plt.imshow(image)

I am trying to access 'Energy Indicator.xls'

I tried the following:
import pandas as pd
import numpy as np
x=pd.ExcelFile('Energy Indicator.xls')
energy= x.parse(skiprows =17, skip_footer=38))
...
I got the following error message:
FileNotFoundError, no such file or directory.
I think it's best to declare your filepath specification with something like os.path or filedialog from tkinter.

Error opening '*.wav': File contains data in an unknown format

I'm trying to run the following code:
import os
import librosa
import IPython.display as ipd
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
import warnings
warnings.filterwarnings("ignore")
train_audio_path = 'train/audio/'
samples, sample_rate = librosa.load(train_audio_path+'yes/0a7c2a8d_nohash_0.wav', sr = 16000)
But get two errors:
Error opening 'train/audio/yes/0a7c2a8d_nohash_0.wav': File contains data in an unknown format
and
NoBackendError:
I've tried downloading ffmpeg and gstreamer to fix the second error but no luck. I'm not sure what to do about the first error as I have imported libraries that should be able to handle .wav files.
Thank you for your help in advance.

Adding noise to an image in increments

Hi I am trying to add noise to a QR image that I create, this is my code so far:
import numpy
import scipy
import scipy.misc
import sys
sys.path.append('M:/PythonMods')
import qrcode
if __name__ == "__main__":
myqr = qrcode.make("randomtexxxxxxxxxt")
#myqr.show()
myqr.save("M:/COMPUTINGSEMESTER2/myqr4.png")
filename = 'myqr4.png'
imagea = (scipy.misc.imread(filename)).astype(float)
poissonNoise = numpy.random.poisson(50,imagea.shape).astype(float)
noisyImage = imagea + poissonNoise
Please could someone advise me how I get it to show the noisy image? and how to save the image so I can test it?
Any help really appreciated.
edit
I tried adding this code to the program to get it to show the image:
from PIL import Image
myimage = Image.open(noisyImage)
myimage.load()
But then got this error:
Traceback (most recent call last):
File "M:\COMPUTINGSEMESTER2\untitled4.py", line 28, in <module>
myimage = Image.open(noisyImage)
File "Q:\PythonXY273_MaPS-T.v01\Python27\lib\site-packages\PIL\Image.py", line 1958, in open
prefix = fp.read(16)
AttributeError: 'numpy.ndarray' object has no attribute 'read'
Image.open needs an image file as parameter, use Image.fromarray:
im = Image.fromarray(noisyImage)
im.save("myFile.jpeg")
you may also use matplotlib module to show the image directly:
import matplotlib.pyplot as plt
plt.imshow(noisyImage) #Needs to be in row,col order
scipy.misc.imsave('NoisyImage.jpg', noisyImage)

Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'

import os
import sys
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
import matplotlib.pyplot as plt
import Image
from scipy import ndimage
from pymorph import regmax
from PIL import Image
path='all_images'
for file in os.listdir(path):
current = os.path.join(path, file)
extension = os.path.splitext(current)[-1]
fileType = extension.upper()
print(current)
if os.path.isfile(current):
img = mahotas.imread(current)
imgf = ndimage.gaussian_filter(img, 8)
pylab.gray()
imgf.save('dnaa.gif')
Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'. Can anyone help how to save file using pylab. I guss the last line of the code has some issue.
Use mahotas.imsave('dnaa.gif', imgf) instead. The NumPy array you get from gaussian_filter doesn't have save functionality built in.

Categories