Resizing images efficiently - python

My dataset contains 102235 images which I stored in a list and saved as a pickle file. I would like to train a neural network with these images.
Therefore, I would like to resize the images.
I used the following code but my Jupyter Notebook died.
import pickle
images = pickle.load(open(r"path\to\pickle\data\*.pkl", "rb"))
import cv2
def resize_images(img, new_width, new_height):
size = (new_width, new_height)
resized_img = cv2.resize(img, size)
return resized_img
import pandas as pd
images = images.tolist()
resized_img_train = [resize_images(img, new_width=224, new_height=224) for img in images]
Is there a better way to do this?
Edit: I do not want to use TensorFlow or Keras since my computer seems to have problems with these two packages.

Related

How possibly can I use the frames generated from my webcam to be the input of my Keras model?

I want to change the img_path to the frames generated from opencv:
img = image.load_img(img_path, target_size=(224, 224))
How can I rewrite it?
I am assuming that the image.load_img() function that you are using is the one from keras_utils package.
As it is noted in the documentation, load_img() accepts path to the image as the first parameter and returns:
Returns:
A PIL Image instance.
It is not mentioned in the question, but if you read the frames from the camera using opencv they should already be numpy arrays which you could to pass to your model. Of course you should resize them to (224,224) before (how to resize the image using opencv).
However, if you want to have the PIL images (to have the same type as the one returned by load_img()), you need to convert your opencv frames (numpy array) to PIL image. Follow this question and answer by #ZdaR to do this conversion:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread("path/to/img.png")
# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
# For reversing the operation:
im_np = np.asarray(im_pil)

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

Python- Convert .png images in a folder into a video

I wanted to build a screen recorder with python. So, I used the Screenshot() function of the pyautogui library. When I run the program, I get individual PNG images in a folder like this-----
C:\rec\1.png
C:\rec\2.png
C:\rec\3.png
C:\rec\4.png
C:\rec\5.png
And so on. Now I want to convert these PNG images into a video of MP4 or AVI format. How should I proceed?
I found this.
they use the opencv library:
pip install opencv-python
then:
import cv2
import numpy as np
import glob
img_array = []
for filename in glob.glob('C:/rec/*.png'): # (The only change I have made is here to the filepath.)
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()

Extracting images from matlab file

I'm trying to extract the images (and its label and such) from an RGB-D dataset called NYUV2 dataset. (I downloaded the labelled dataset)
It's a matlab file so I tried using hdf5 to read it but I don't know how to proceed from here. How do I save the images and its corresponding labels and depths into a different folder??
Here's the script that I used and its corresponding output.
import numpy as np
import h5py
f = h5py.File('nyu_depth_v2_labeled.mat','r')
k = list(f.keys())
print(k)
Output is
['#refs#', '#subsystem#', 'accelData', 'depths', 'images', 'instances', 'labels', 'names', 'namesToIds', 'rawDepthFilenames', 'rawDepths', 'rawRgbFilenames', 'sceneTypes', 'scenes']
I hope this helps.
I suppose you are using the PIL package The function fromarray expects the "mode of the image" see https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes
I suppose your image is in RGB. I believe the image souhld be under group 'images' and dataset image_name
Therefore
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File('nyu_depth_v2_labeled.mat','r')
array = np.array(list(hdf.get("images/image_name")))
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.show()
You can also look at another answer I gave to know how to save images
Images saved as HDF5 arent colored
To view the content of the h5 file, download HDFview, it will help navigate through it.

Read mnist images into Tensorflow

I was looking at this Tensorflow tutorial.
In the tutorial the images are magically read like this:
mnist = learn.datasets.load_dataset("mnist")
train_data = mnist.train.images
My images are placed in two directories:
../input/test/
../input/train/
They all have a *.jpg ending.
So how can read them into my program?
I don't think I can use learn.datasets.load_dataset because this seems to take in a specialized dataset structure, while I only have folders with images.
mnist.train.images is essentially a numpy array of shape [55000, 784]. Where, 55000 is the number of images and 784 is the number of pixels in each image (each image is 28x28)
You need to create a similar numpy array from your data in case you want to run this exact code. So, you'll need to iterate over all your images, read image as a numpy array, flatten it and create a matrix of size [num_examples, image_size]
The following code snippet should do it:
import os
import cv2
import numpy as np
def load_data(img_dir):
return np.array([cv2.imread(os.path.join(img_dir, img)).flatten() for img in os.listdir(img_dir) if img.endswith(".jpg")])
A more comprehensive code to enable debugging:
import os
list_of_imgs = []
img_dir = "../input/train/"
for img in os.listdir("."):
img = os.path.join(img_dir, img)
if not img.endswith(".jpg"):
continue
a = cv2.imread(img)
if a is None:
print "Unable to read image", img
continue
list_of_imgs.append(a.flatten())
train_data = np.array(list_of_imgs)
Note:
If your images are not 28x28x1 (B/W images), you will need to change the neural network architecture (defined in cnn_model_fn). The architecture in the tutorial is a toy architecture which only works for simple images like MNIST. Alexnet may be a good place to start for RGB images.
You can check the answers given in How do I convert a directory of jpeg images to TFRecords file in tensorflow?. Easiest way is to use the utility provided by tensor flow :build_image_data.py, which does exactly the thing you want to do.

Categories