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

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.

Related

Extract data from*.vtu file

I am trying to extract some data arrays from a *.vtu file
import numpy
import vtk.vtk
from vtk import vtkUnstructuredGridReader
from vtk.util import numpy_support as VN
reader = vtkUnstructuredGridReader()
reader.SetFileName("heterogeneousbox-00041.vtu")
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()
data = reader.GetOutput()
GasSaturation = data.GetPointData().GetScalars("Sn")
print(type(GasSaturation))
but I get this error: ModuleNotFoundError: No module named 'vtk'
PS: I am using pycharm and python3.8 as interpreter!
Thanks in advance

What is the json file I need to read?

enter image description hereI need to download satellite images using python. I have found a code in GitHub but I did not understand what at this line. Please help me what it exactly is.
Visit https://github.com/kscottz/PythonFromSpace/blob/master/TheBasics.ipynb
import sys
import os
import json
import scipy
import urllib
import datetime
import urllib3
import rasterio
import subprocess
import numpy as np
import pandas as pd
import seaborn as sns
from osgeo import gdal
from planet import api
from planet.api import filters
from traitlets import link
import rasterio.tools.mask as rio_mask
from shapely.geometry import mapping, shape
from IPython.display import display, Image, HTML
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
urllib3.disable_warnings()
from ipyleaflet import (
Map,
Marker,
TileLayer, ImageOverlay,
Polyline, Polygon, Rectangle, Circle, CircleMarker,
GeoJSON,
DrawControl
)
%matplotlib inline
# will pick up api_key via environment variable PL_API_KEY
# but can be specified using `api_key` named argument
api_keys = json.load(open("apikeys.json",'r'))
client = api.ClientV1(api_key=api_keys["PLANET_API_KEY"])
# Make a slippy map to get GeoJSON
api_keys = json.load(open("apikeys.json",'r'))
client = api.ClientV1(api_key=api_keys["PLANET_API_KEY"])
What is the meaning of these two lines. What file should I upload for apikeys.json
You should follow this link to get an API Key.
https://support.planet.com/hc/en-us/articles/212318178-What-is-my-API-key-
apikeys.json is a JSON file of following format/content in json:
{"PLANET_API_KEY":"<Some API Key here>"}
json.load(...) API loads this json file as a dictionary

CSV file not found despite specified path

I am trying a project as a beginner. It is driving me nuts because I keep getting minor errors that paralyze the whole execution. Here's an error that has been plaguing me.
### SOLUTION
## 1. Introduction of dataset
import pandas as pd
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
# This lets us see many columns in the output
pd.set_option('display.expand_frame_repr', False)
df = pd.read_csv('data.csv', index_col=0)
Error:
File "C:\ProgramData\Anaconda\Lib\site-packages\pandas\_libs\parsers.cp36-win_amd64.pyd", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
V\000~Ã\000\000ëtA¸P\000\000\000H»Ì\000H
builtins.FileNotFoundError: File b'data.csv' does not exist.
Why do I get this error even though the csv file exists?
You need to provide absolute path for the file.
For example: pd.read_csv("/path/to/the/file/data.csv", ...)
Or if you want to read the file from current directory:
import os
import sys
csv_path = os.path.dirname(os.path.abspath(sys.executable)) + '/data.csv'
df = pd.read_csv(csv_path, index_col=0)

Cifar - 10 / Unpickle

I am getting following error when i try to unpickle the cifar-10 dataset. I need to train a model but I can't even get the data for my operations. How can I fix this problem
dict=cPickle.load(fo)
UnpicklingError: invalid load key, '\x06'.
import tensorflow as tf
import os
import numpy as np
import dataset_class
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import glob
from PIL import Image
from scipy.spatial.distance import pdist
def cifar_10_reshape(batch_arg):
output=np.reshape(batch_arg,(10000,3,32,32)).transpose(0,2,3,1)
return output
def unpickle(file):
import _pickle as cPickle
fo=open(file,'rb')
dict=cPickle.load(fo)
fo.close()
return dict
#Loading cifar-10 data and reshaping it to be batch_sizex32x32x3
batch1=unpickle('cifar-10-batches-py/data_batch_1.bin')
batch2=unpickle('cifar-10-batches-py/data_batch_2.bin')
batch3=unpickle('cifar-10-batches-py/data_batch_3.bin')
batch4=unpickle('cifar-10-batches-py/data_batch_4.bin')
batch5=unpickle('cifar-10-batches-py/data_batch_5.bin')
batch1_data=cifar_10_reshape(batch1['data'])
batch2_data=cifar_10_reshape(batch2['data'])
batch3_data=cifar_10_reshape(batch3['data'])
batch4_data=cifar_10_reshape(batch4['data'])
batch5_data=cifar_10_reshape(batch5['data'])
batch1_labels=batch1['labels']
batch2_labels=batch2['labels']
batch3_labels=batch3['labels']
batch4_labels=batch4['labels']
batch5_labels=batch5['labels']
test_batch=unpickle('cifar-10-batches-py/test_batch')
test_images=cifar_10_reshape(test_batch['data'])
test_labels_data=test_batch['labels']
train_images=np.concatenate((batch1_data,batch2_data,batch3_data,batch4_data,batch5_data),axis=0)
train_labels_data=np.concatenate((batch1_labels,batch2_labels,batch3_labels,batch4_labels,batch5_labels),axis=0)
From what I have understood of the CIFAR-10 dataset, the version you are trying to unpickle is in a binary format, while you are not providing any information to the 'unpickler' about the encoding. You might have more luck trying the loading function provided on the CIFAR-10 website (https://www.cs.toronto.edu/~kriz/cifar.html) for python 3.x:
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict

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