Hi: I would like load some 2400 images from a folder into python 3.6 for Neural Networks, the following code works, however, it loads images in its original size, which is (2443, 320, 400, 3), after converting into an array. How do I resize it to 64x96? so it is (2443, 64, 96, 3) and less of a load on memory. Separately, how do I do it using parallel processing, so it is efficient.
Thank you!
IMAGE_PATH = 'drive/xyz/data/'
file_paths = glob.glob(path.join(IMAGE_PATH, '*.gif'))
# Load the images
images = [misc.imread(path) for path in file_paths]
images = np.asarray(images)
Inspired by this link, I tried to do the following:
from PIL import Image
basewidth = 96
IMAGE_PATH = 'drive/xyz/data/'
file_paths = glob.glob(path.join(IMAGE_PATH, '*.gif'))
# Load the images img = [misc.imread(path) for path in file_paths]
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
images = np.asarray(img)
however, it resulted in an error, written below. Any suggestions, would be greatly appreciated. Thank you.
AttributeError Traceback (most recent call last)
<ipython-input-7-56ac1d841c56> in <module>()
9 img = [misc.imread(path) for path in file_paths]
10
---> 11 wpercent = (basewidth/float(img.size[0]))
12 hsize = int((float(img.size[1])*float(wpercent)))
13 img = img.resize((basewidth,hsize), Image.ANTIALIAS)
AttributeError: 'list' object has no attribute 'size'
First you may want to resize your images then you can use your same method to initialize the arrays given the output image folder.
Resizing the image
This uses PIL package for resizing images but any library should do the same as long as it provides a resize method.
You can read further down discussions from here How do I resize an image using PIL and maintain its aspect ratio?
import os, sys
import Image
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".gif"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "GIF")
except IOError:
print "cannot create thumbnail for '%s'" % infile
Parallelism Example
Regarding parallelism you can use this example as a base and move on from there.
This is from the python docs https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
Related
Basically, I copied and paste a script, and merged it with other script, and now the script doesn't work, it says an error "NoneType object has no attribute save"Screenshot
And here's the script:
` from PIL import Image
import PIL
import os
from glob import glob
imgs = [y for x in os.walk(".") for y in glob(os.path.join(x[0], '*.png'))]
size = 32, 32
lastdir = None
for file in imgs:
img = Image.open(file)
img = img.thumbnail(size, resample=PIL.Image.NEAREST)
file = file.replace('img', 'icon', 1)
dir = os.path.dirname(file)
try:
os.makedirs(dir)
except:
pass
if dir!=lastdir:
print(dir)
lastdir = dir
img.save(file + ".png", "PNG")`
Resize various images on various directories and save them, but the images are not saving.
The thumbnail() method modifies its object rather than returning a new one. So, this line:
img = img.thumbnail(size, resample=PIL.Image.NEAREST)
should be:
img.thumbnail(size, resample=PIL.Image.NEAREST)
I am trying to do image processing on my dataset. The dataset is divided into 346 folders according to the following manner
What I want to do is
loop over the 346.
Enter each folder and process the images within
Process the image in regards to changing it to gray scale, resize and normalize (these three steps should be applied to my whole dataset.
I want to keep the same folders/files names and dont change it when I run my data processing.
The folder/ files name are as follows
video_0001/ 00000.png, 00001.png, .....
video_0002/ 00000.png, 00001.png,
The number of files vary according to each folder and the last video_0346
P.S when I try to normalize the images I get black images when dividing by 255
I am still new to python. Here's what I was able to accomplish
I appreciate your help
Img_height = 512
Img_width = 512
srcdir = "C:\\Users\\Desktop\\_dataset"
for subdir, dirs, files in os.walk(srcdir):
for i in range(346):
for file in os.listdir(subdir):
print(file )
img = cv2.imread(os.path.join(srcdir,file))
print("image", img)
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
Image_sample_resized = resize(gray_img, (height, width))
plt.imshow( Image_sample_resized, cmap = "gray")
i = i+1
I was still trying to understand how you wanted to save you formated images but the methods below just save them to a new dir that you specify so you can batch them while training if you want.
import os
import cv2
import matplotlib.image as mpimg
def resize_image(image, size=(512, 512)):
"""
Resize an image to a fixed size
"""
return cv2.resize(image, size)
def convert_to_grayscale(image):
"""
Convert an image to grayscale
"""
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
def normalize_image(image):
"""
Normalize an image
"""
return cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
def save_image(image, directory, filename):
"""
Save an image to a new directory
"""
cv2.imwrite(os.path.join(directory, filename), image)
def main():
"""
Main function
"""
# Get the directory to process
directory = input("Enter the directory to process: ")
# Get the directory to save the images
save_directory = input("Enter the directory to save the images: ")
# Size to resize the images
img_width, img_height = 512, 512
# Iterate through the directory
for root, _, files in os.walk(directory):
for file in files:
# Get the filepath
filepath = os.path.join(root, file)
# Get the filename
filename = os.path.basename(filepath)
# Get the extension
extension = os.path.splitext(filepath)[1]
# Check if the file is an image
if extension in [".jpg", ".jpeg", ".png"]:
# Load the image
image = mpimg.imread(filepath)
# Resize the image
image = resize_image(image, (img_width, img_height))
# Convert the image to grayscale
image = convert_to_grayscale(image)
# Normalize the image
image = normalize_image(image)
# Save the image
save_image(image, save_directory, filename)
if __name__ == "__main__":
main()
I'm learning opencv via the following link link link2. And I got error related to image training process for face recognition. Please correct me or help with the problem I'm having. thank you
System Information :
OpenCV 4.5.2.52
Python 3.9.5
Detailed Description
I need to train an images by using the code below and the results was :
- OpenCV/faces-train.py", line 50, in <module>
recognizer.train(x_train, np.array(y_labels))
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'train'
> Overload resolution failed:
> - src is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'src'
Code
import cv2
import os
import numpy as np
from PIL import Image
import pickle
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")
face_cascade = cv2.CascadeClassifier('cascade/data/haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
label_ids = {}
y_labels = []
x_train = []
for root, dirs, files, in os.walk(image_dir):
for file in files:
if file.endswith("png") or file.endswith("jpg") or file.endswith("jpeg") :
path = os.path.join(root, file)
label = os.path.basename(root).replace(" ", "-").lower()
# print(label, path)
if not label in label_ids:
label_ids[label] = current_id
current_id += 1
id_ = label_ids[label]
# print(label_ids)
y_labels.append(label)
x_train.append(path)
pil_image = Image.open(path).convert("L")
image_array = np.array(pil_image, "uint8")
# print(image_array)
faces = face_cascade.detectMultiScale(image_array, scaleFactor=1.5, minNeighbors=5)
for(x,y,w,h) in faces:
roi = image_array[y:y+h, x:x+w]
x_train.append(roi)
y_labels.append(id_)
# print(y_labels)
# print(x_train)
with open("labels.pickle", 'wb') as f:
pickle.dump(label_ids, f)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yml")
remove the following lines:
y_labels.append(label)
x_train.append(path)
you don't really need to append the labels or the paths since you are basically training the face recognizer to recognise/categorise faces. Likewise, you wouldnt need the path to your images as it is also not necessary. Guessing this is what caused the error!
Do let me know if the fix works! Cheers :)
I am working on a robotics project in which a robot creates images in .pgm format (not camera images) continuously which I'm converting to .jpeg and saving to a directory. However, this directory keeps updating as more images keep getting added while I want to convert these images continuously in a video. I can already create a video from the images saved in one instance but what I want is to update the video continuously as a single file (streaming onto a URL) while images keep getting saved. Here is my code but it doesn't work in parallel for images as well as for videos. Anyone can help?
from PIL import Image
import os, glob
import cv2
import numpy as np
from os.path import isfile, join
# remove .yaml files
directory = "/home/user/catkin_ws/src/ros_map/map_pgms"
files_in_directory = os.listdir(directory)
filtered_files = [file for file in files_in_directory if file.endswith(".yaml")]
for file in filtered_files:
path_to_file = os.path.join(directory, file)
os.remove(path_to_file)
def batch_image(in_dir, out_dir):
if not os.path.exists(out_dir):
print(out_dir, 'is not existed.')
os.mkdir(out_dir)
if not os.path.exists(in_dir):
print(in_dir, 'is not existed.')
return -1
count = 0
for files in glob.glob(in_dir + '/*'):
filepath, filename = os.path.split(files)
out_file = filename[0:9] + '.jpg'
# print(filepath,',',filename, ',', out_file)
im = Image.open(files)
new_path = os.path.join(out_dir, out_file)
print(count, ',', new_path)
count = count + 1
im.save(os.path.join(out_dir, out_file))
if __name__ == '__main__':
batch_image('/home/user/catkin_ws/src/ros_map/map_pgms', './batch')
# convert the .jpgs to video
img_array = []
for filename in glob.glob('./batch/*.jpg'):
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()
I have the following code that I thought would resize the images in the specified path But when I run it, nothing works and yet python doesn't throw any error so I don't know what to do. Please advise. Thanks
import cv2
import numpy as np
import os
from PIL import Image
def image_resize(folder):
images = []
num_images = 0
location = folder+"_"
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder, filename))
if img is not None:
new_img = np.array(Image.fromarray(img).resize((200, 200), Image.ANTIALIAS)) # Resize the images to 50 X 50
images.append(new_img)
num_images += 1
cv2.imwrite("{}/{}".format(location, filename), new_img)
return None
image_resize("2S1")
image_resize("SLICY")
image_resize("BRDM-2")
image_resize("BTR-60")
image_resize("D7")
image_resize("T62")
image_resize("ZIL131")
image_resize("ZSU-23_4")
all image in folder: G:\sar
help me
It does not do anything because your for loop is not finding any files in the folder. You are never passing the "G:\sar" part of the file path.