Assigning a filepath to a variable in Python 3 - python-3.x

I am trying to convert few camera-clicked images of handwritten Gujarati characters to the form of MNIST dataset as I intend to pass the Gujarati handwritten characters images to the MNIST deep learning model. And as part of that, I'm trying to assign a file path to a variable named "datadir". But when executing the below code in Ubuntu 16.04, the terminal throws the error which looks like this:
File "gujaratinn.py", line 7
datadir = /home/cryptoaniket256/Desktop/opencv-3.4.1/project/Resize
^
SyntaxError: invalid syntax
Note that the name of the file is gujaratinn.py and all the camera-clicked images are stored in the Resize folder.
import numpy as np
import matplotlib.pyplot as py
import os
import cv2
from pathlib import Path
datadir = Path("/home/cryptoaniket256/Desktop/opencv-
3.4.1/project/Resize")
fileToOpen = datadir/"practice.txt"
f = open(fileToOpen)
print(f.read())

Are you affecting datadir with a path you wrote on 2 rows in your code ?
Try to put line 7 and 8 on the same row or change the quotes like that:
import numpy as np
import matplotlib.pyplot as py
import os
import cv2
from pathlib import Path
datadir = Path("""/home/cryptoaniket256/Desktop/opencv-3.4.1/project/Resize""")
fileToOpen = datadir/"practice.txt"
f = open(fileToOpen)
print(f.read())

Related

How to extract only CR No only from image

Sample image
I need to extract CR No.from the sample image given above. Using Easyocr, I got the output in complex nested list form. How to update the code to filter out all the detected text/numbers and get only CR No. I am running out of ideas, and help will be appreciated. What I have tried so far-
#Import libraries
import os
import easyocr
import cv2
from matplotlib import pyplot as plt
import numpy as np
IMAGE_PATH = 'H://CDAC//Spyder_projects//CR_No//input_image//input7.jpg'
reader = easyocr.Reader(['en'])
result3 = reader.readtext(IMAGE_PATH)
result3
my_list2 = []
length = len(result3)
for i in range(length):
if (result3[i][1]) == 'CR No':
print(result3[i])
print(result3[i+1])
my_list2.append(result3[i+1]+result3[i])
print(my_list2)
print('The CR No is:', my_list2[0][1])
The expected output should be- 211022203481161

OSError: Unable to open file (file signature not found)

I am currently doing an assignment on deep learning by downloading the assignment files from github.
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
%matplotlib inline
You are given a dataset ("data.h5") containing: - a training set of m_train images labeled as cat (y=1) or non-cat (y=0) - a test set of m_test images labeled as cat or non-cat - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
I ran the setup.sh file too but the error doesn't seem to go away.
lr_utils.py file:
import numpy as np
import h5py
def load_dataset():
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
Kindly help!
I solved the issue by downloading uncorrupted .h5 files and putting them in the folder datasets/ in the same directory.
The files you downloaded are corrupted. You can visit https://github.com/abdur75648/Deep-Learning-Specialization-Coursera to download the uncorrupted files.
you can download uncorrupted files from here :
https://www.kaggle.com/datasets/muhammeddalkran/catvnoncat
and replace it in the directory of the corrupted files

TypeError: join() argument must be str or bytes, not 'TextIOWrapper

I have features and a target variable which I am wanting to generate a Decision Tree. However, the code is throwing an error. Since the 'out file' did not generate an error, I figured there wouldn't be an error for the 'Source.from_file' either, but there is one.
import os
from graphviz import Source
from sklearn.tree import export_graphviz
f = open("C:/Users/julia/Desktop/iris_tree.dot", 'w')
export_graphviz(
tree_clf,
out_file=f,
feature_names=sample2[0:2],
class_names=sample2[5],
rounded=True,
filled=True
)
Source.from_file(f)
As noted in the docs, from_file accepts a string path, not a file object:
filename – Filename for loading/saving the source.
Just pass the path in:
import os
from graphviz import Source
from sklearn.tree import export_graphviz
path = "C:/Users/julia/Desktop/iris_tree.dot"
f = open(path, 'w')
export_graphviz(
tree_clf,
out_file=f,
feature_names=sample2[0:2],
class_names=sample2[5],
rounded=True,
filled=True
)
Source.from_file(path)

How can i feature extraction (.wav) form folders and sub-folders for using as input of the neural network?

This is my code, please help me to correct:
from pathlib import Path
from python_speech_features import fbank
import scipy.io.wavfile as wavfile
path = Path('/home/narges/dataset/seri1.16khz.128kbps/Voice Recorder/N00xxxx/').glob('*/*.wav')
wavs = [str(wavf) for wavf in path if wavf.is_file()]
wavs.sort()
print(wavs)
number_of_files=len(wavs)
wav_data=[]
for i in range(number_of_files):
samplerate, data=wavfile.read(wavs[i])
wav_data.append(data)
print(wav_data)
fbank_feat=fbank(samplerate, data)
print(fbank_feat)
my error is: 'int' object is not subscriptable. How can i correct this?
Finally i used this code and it's correct:
from pathlib import Path
from python_speech_features import fbank
import scipy.io.wavfile as wavfile
import numpy as np
path = Path('/home/narges/dataset/seri1.16khz.128kbps/Voice Recorder/N00xxxx/').glob('*/*.wav')
wavs = [str(wavf) for wavf in path if wavf.is_file()]
wavs.sort()
print(wavs)
number_of_files=len(wavs)
for i in range(number_of_files):
(rate, sig) = wavfile.read(wavs[i])
fbank_feat=fbank(sig, rate, winlen=0.06, winstep=0.01, nfilt=26, nfft=512, lowfreq=0, highfreq=rate/2, preemph=0.97, winfunc=np.hamming)
print(fbank_feat)

Reading images from directory in python

I am facing the problem in reading images from multiple directory in python. Like there is single image in png format and it is located in multiple folders. I want to keep a for loop on that and then access the image like from every folder. So what could be the way to read those images from the particular folder?
import os
from os import listdir
from os.path import isfile, join
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import glob
%matplotlib inline
mypath='E:/Datasets/CBIS-DDSM PNG/Converted_Test'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
Maybe you should try another method to reach to the directories for reading images.
in imread() you can pass the directory .so you can use str() and + to combine dynamic directories and fixed directory.
Here is the example maybe help you :
path = '/home/pictures/1'
for i in range(2) :
image = cv2.imread(str(path)+'1'+'/222.jpg')
plt.imshow(image)
plt.show()
In this example I had 2 folder 11 and 12 so with a for loop I changed folders that I need.

Resources