Image count is zero - python

I am trying to create image classification model using tensorflow lite for android app here: https://www.tensorflow.org/tutorials/images/classification However,
I am using my local directory.
Here is my code:
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import pathlib
data_dir = pathlib.Path('C:/Users/aalroumi/Documents/MLKIT/V')
image_count = len(list(data_dir.glob('*/*.JPEG')))
print(image_count)
and here is an image of the output:
![][1]
and this is the image of my local directory:
So why am I getting image size is zero?
Am I missing something? I did everything right.
Because I am trying to files.download('model.tflite')
Image size is zero error:
Thank you

It seems you have images directly in data_dir so you should use *.JPEG to get them in glob()
But from_folder(data_dir) expects images in subfolders which names will be used as labels for images.
ie.
data_dir/cat/small_cat.JPEG
data_dir/cat/big_cat.JPEG
# ...
data_dir/dog/white_dog.JPEG
data_dir/dog/black_dog.JPEG
but you don't have subfolders so it can't find images in subfolders.
It doesn't count images directly in data_dir

Related

python about train a deep neural network on the MRI slices dataset

I want to Train a deep neural network on the MRI slices dataset. Here is my code
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
file_dir = 'C:\\Users\\adam\\Downloads\\MRI_Images\\'
import glob
import cv2
images = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\.png")]
(X_train_full, y_train_full), (X_test, y_test) = images
And python shows that not enough values to unpack. I don't know why. Is there problem when I put all images in one file to python?
I don't know the structure of your dataset directory, but I know that using glob.glob() will return all the images inside the 'C:\\Users\\adam\\Downloads\\MRI_Images\\' folder (not include subfolder).
That is, what you get inside image is a list of read-in images (numpy array format), like:
[image_0, image_1, ...]
A list can not be unpack into two tuples. And this is why the error comes out.
Try reading your train and test images seperately might help:
images_trainx = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\trainx\\*.png")]
images_trainy = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\trainy\\*.png")]
images_testx = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\testx\\*.png")]
images_testy = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\testy\\*.png")]
This approach is clunky but hard to go wrong.

How to reshape images in folder for DCGAN code?

These are the 9600 images in my folder + their properties: enter image description here
I tried resizing them with this code below, but then this gave me an image shape of (9600, 224, 672). I mainly used this code because it helped me get my images into the correct array/ I had used this code earlier for a kmeans thing.
I need a smaller image shape for the DCGAN code (something closer to 9600,64,128)-- see below for why.
import numpy as np
from numpy import save
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import cv2
import os, glob, shutil
input_dir = '/Users/User/Documents/MATLAB/attempt6images/'
glob_dir = input_dir + '/*.jpg'
img = [cv2.resize(cv2.imread(file), (224, 224)) for file in glob.glob(glob_dir)]
#save('SV_images.npy', images)
paths = [file for file in glob.glob(glob_dir)]
img = np.array(np.float32(img).reshape(len(img), -1)/255)
save('SV_images.npy', img)
I'm trying to run them through a DCGAN code which was originally created for an image with shape (9600, 64, 128). When I use the image shape 224,672, my kernel dies. So I'm assuming my image size might be too big in comparison to the original images. By the way I changed the 25624 part to 256721 to match my image shape + changed all the other numbers besides the 256 column). Below is a part of that original code for the 64x128. Any idea how to reshape my image size so that my kernel doesnt die?
enter image description here

OpenCV displays 'None' for a large number of images

I am trying to run a pre-trained pose detection model on a new dataset.
When I pass a single image to the model, I get the correct results.
Although, when I apply a for loop for multiple images present in a folder(as shown below), openCV even fails to load half of the images.
Keep in mind I am running this code on Intel i3 dual core laptop.
import os
import cv2
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(i)
print(image,i)
Output:
None frame_000000.jpg
None frame_000001.jpg
None frame_000002.jpg
None frame_000003.jpg
os.listdir() does not return absolute file paths, but only the file names.
import os
import cv2
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(os.path.join(DIR, i))
print(image,i)
The path you're trying to use to read image is wrong. You have to use:
import cv2
import os
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(os.path.join(DIR,i))
print(image,i)

How to load image dataset for SVM image classification task

I'm trying to make a linear SVM classifier (AD vs NC) for the classification of Alzheimer's Disease by using MRI images. How can I load the image dataset correctly?
I found an example of SVM image classification and I tried to run through the trial, but there was an error when loading the dataset.
The folder name is "images"
there are five subfolders in "images". They are named as doller_bill, sunflower, pizza, dog, and ball. Each subfolder contains 50-60 photos as jpg format. The followings are the sample codes I downloaded.
download from github
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np %matplotlib notebook
from sklearn import svm, metrics, datasets
from sklearn.utils import Bunch
from sklearn.model_selection import GridSearchCV, train_test_split
from skimage.io import imread
from skimage.transform import resize
def load_image_files(container_path, dimension=(64, 64)):
image_dir = Path(container_path)
folders = [directory for directory in image_dir.iterdir() if
directory.is_dir()]
categories = [fo.name for fo in folders]
descr = "A image classification dataset"
images = []
flat_data = []
target = []
for i, direc in enumerate(folders):
for file in direc.iterdir():
img = skimage.io.imread(file)
img_resized = resize(img, dimension, anti_aliasing=True,
mode='reflect')
flat_data.append(img_resized.flatten())
images.append(img_resized)
target.append(i)
flat_data = np.array(flat_data)
target = np.array(target)
images = np.array(images)
return Bunch(data=flat_data,
target=target,
target_names=categories,
images=images,
DESCR=descr)
image_dataset = load_image_files("images/")
However, when I run through the codes, it appeared an error as follows
NameError: name 'skimage' is not defined
So, would you please help me to figure out how to load the image dataset.
For instance, I have a folder named "images"
the subfolders are named as "MRI images_NC", "MRI images_AD",
Accordingly, each folder contains 1500 photos approximately.
Thanks again.
name 'skimage' is not defined
means that during the import
from skimage.io import imread `enter code here`
the skimage package can not be found
Please run a
pip install scikit-image

How to convert multiple mha images to nii

I would like to convert about 300 .mha images into .nii format. Which are under subfolders . I have successfully converted one image but in order to go each folder and convert, its a tidious task. Please suggest me code that is functioning with sitk library
import SimpleITK as sitk
import matplotlib.pyplot as plt
import numpy as np
import os
OUTPUT_DIR = '/home/user/Downloads/'
Image = sitk.ReadImage('Input dir')
print(Image.GetPixelIDTypeAsString())
sitk.WriteImage(Image, os.path.join(OUTPUT_DIR, 'Flair.nii'))
Use python's os.walk method to get all the files in a directory tree. Then select out only the ones with the '.mha' suffix.
Here's an example of how it works:
https://www.tutorialspoint.com/python/os_walk.htm
Or you can try and decipher the os library documentation:
https://docs.python.org/3/library/os.html

Categories