import cv2
import numpy as np
import face_recognition
import os
path = os.getcwd() +"\\" + "known_images"
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])
print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
After writing this code to encode the images of a directory(3 images in total) one by one, using face_recognition library , it prints out this error code
File"C:\Users\user\PycharmProjects\pythonProject\First_semester\Face_recognition\hello.py", line 26, in findEncodings encode = face_recognition.face_encodings(img)[0] IndexError: list index out of range
I noticed that you are loading the images with cv2, but given this snippet (taken from face_recognition repo):
import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")
biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
I would say that you need to load the images with face_recognition.load_image_file.
The problem is that one of the images at dataset is not ok to be encoded. So, try this code bellow. It remove the image with problems and continue encoding the rest (use folder "dataset" with subfolders with the name of the people that you collect face image):
import cv2
import numpy as np
import face_recognition
import os
images = []
classNames = []
listNames = []
listFiles = []
myLists = list(paths.list_images('dataset'))
for (i, myList) in enumerate(myLists):
name = myList.split(os.path.sep)[-2]
print("[INFO] processing image {}/{} - folder {}".format(str(i + 1).zfill(3), len(myLists), name))
curImg = cv2.imread(myList)
images.append(curImg)
listNames.append(name)
listFiles.append(myList)
def findEncodings(images):
encodeList = []
for (i, img) in enumerate(images):
print("[INFO] analising image {}/{}".format(i + 1, len(images)))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
try:
encode2 = face_recognition.face_encodings(img)[0]
encodeList.append(encode2)
classNames.append(listNames[i])
except Exception as e:
print('removing ' + listFiles[i])
os.remove(listFiles[i]) # remove image if not ok
with open('encodeList.txt', 'w') as filehandle:
for listitem in encodeList:
filehandle.write('%s\n' % listitem)
return encodeList
encodeListKnown = findEncodings(images)
Related
import cv2
import numpy as np
import face_recognition
import os
path = 'Images'
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])
print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
print(len(encodeListKnown))
i really dont see my error here right now...
The console prints me ->
C:\Users\davut\PycharmProjects\QRCodeOpenCV\venv\Scripts\python.exe C:/Users/davut/PycharmProjects/QRCodeOpenCV/AttendanceProject.py
['Armagan.jpeg', 'Bill-Gates.jpg', 'Elon-Musk.jpg', 'Elon-Test.jpg', 'Gizem (1).jpeg', 'Muenster.jpg', 'Oezi.jpeg', 'Sude.jpeg']
['Armagan', 'Bill-Gates', 'Elon-Musk', 'Elon-Test', 'Gizem (1)', 'Muenster', 'Oezi', 'Sude']
Traceback (most recent call last):
File "C:/Users/davut/PycharmProjects/QRCodeOpenCV/AttendanceProject.py", line 25, in
encodeListKnown = findEncodings(images)
File "C:/Users/davut/PycharmProjects/QRCodeOpenCV/AttendanceProject.py", line 21, in findEncodings
encode = face_recognition.face_encodings(img)[0]
IndexError: list index out of range
alright the solution is, dont use a test photo without an face when using face_recognition ...
I am trying to convert high resolution images to something more manageable for machine learning. Currently I have the code to resize the images to what ever height and width I want however I have to do one image at a time which isn't bad when I'm only doing a 12-24 images but soon I want to scale up to do a few hundred images.
I am trying to read in a directory rather than individual images and save the new images in a new directory. Initial images will vary from .jpg, .png, .tif, etc. but I would like to make all the output images as .png like I have in my code.
import os
from PIL import Image
filename = "filename.jpg"
size = 250, 250
file_parts = os.path.splitext(filename)
outfile = file_parts[0] + '_250x250' + file_parts[1]
try:
img = Image.open(filename)
img = img.resize(size, Image.ANTIALIAS)
img.save(outfile, 'PNG')
except IOError as e:
print("An exception occured '%s'" %e)
Any help with this problem is appreciated.
Assuming the solution you are looking for is to handle multiple images at the same time - here is a solution. See here for more info.
from multiprocessing import Pool
def handle_image(image_file):
print(image_file)
#TODO implement the image manipulation here
if __name__ == '__main__':
p = Pool(5) # 5 as an example
# assuming you know how to prepare image file list
print(p.map(handle_image, ['a.jpg', 'b.jpg', 'c.png']))
You can use this:
#!/usr/bin/python
from PIL import Image
import os, sys
path = "\\path\\to\\files\\"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((200,100), Image.ANTIALIAS)
imResize.save(f+'.png', 'png', quality=80)
resize()
You can loop over the contents of a directory with
import os
for root, subdirs, files in os.walk(MY_DIRECTORY):
for f in files:
if f.endswith('png'):
#do something
You can run through all the images inside the directory using glob. And then resize the images with opencv as follows or as you have done with PIL.
import glob
import cv2
import numpy as np
IMG_DIR='home/xx/imgs'
def read_images(directory):
for img in glob.glob(directory+"/*.png"):
image = cv2.imread(img)
resized_img = cv2.resize(image/255.0 , (250 , 250))
yield resized_img
resized_imgs = np.array(list(read_images(IMG_DIR)))
I used:
from PIL import Image
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
dirs = os.listdir( path )
final_size = 244
print(dirs)
def resize_aspect_fit():
for item in dirs:
if ".PNG" in item:
print(item)
im = Image.open(path+"\\"+item)
f, e = os.path.splitext(path+"\\"+item)
size = im.size
print(size)
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x*ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (final_size, final_size))
new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
print(f)
new_im.save(f + 'resized.jpg', 'JPEG', quality=400)# png
resize_aspect_fit()
You can use this code to resize multiple images and save them after conversion in the same folder for let's say dimensions of (200,200):
import os
from PIL import Image
f = r' ' #Enter the location of your Image Folder
new_d = 200
for file in os.listdir(f):
f_img = f+'/'+file
try:
img = Image.open(f_img)
img = img.resize((new_d, new_d))
img.save(f_img)
except IOError:
pass
you can try to use the PIL library to resize images in python
import PIL
import os
import os.path
from PIL import Image
path = r'your images path here'
for file in os.listdir(path):
f_img = path+"/"+file
img = Image.open(f_img)
img = img.resize((100, 100)) #(width, height)
img.save(f_img)
from PIL import Image
import os
images_dir_path=' '
def image_rescaling(path):
for img in os.listdir(path):
img_dir=os.path.join(path,img)
img = Image.open(img_dir)
img = img.resize((224, 224))
img.save(img_dir)
image_rescaling(images_dir_path)
import glob
import cv2
import os
import numpy as np
from PIL import Image
images=[]
images=np.array(images)
path='C:\Users\Quantum\Desktop\test'
count=0
images = [cv2.imread(file,0) for file in glob.glob("E:\homework\Computer vision\Faces\*.jpg")]
for i in range(len(images)):
# im = Image.fromarray(images[i])
# cv2.imwrite(str(path) + '.jpg', images[count])
cv2.imwrite(os.path.join(path, 'pic.jpg'), images[count])
count+=1
Trying to select all the images from a folder and the images are getting selected and are converted to grayscale although I dont know how to write those images to a specific folder.Kindly help
#multiple image conversions
import cv2
import os,glob
from os import listdir,makedirs
from os.path import isfile,join
path = '/root/Desktop/Anil' # Source Folder
dstpath = '/root/Desktop/Anil2' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in same folder")
# Folder won't used
files = list(filter(lambda f: isfile(join(path,f)), listdir(path)))
for image in files:
try:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(dstpath,image)
cv2.imwrite(dstPath,gray)
except:
print ("{} is not converted".format(image))
for fil in glob.glob("*.jpg"):
try:
image = cv2.imread(fil)
gray_image = cv2.cvtColor(os.path.join(path,image), cv2.COLOR_BGR2GRAY) # convert to greyscale
cv2.imwrite(os.path.join(dstpath,fil),gray_image)
except:
print('{} is not converted')
import cv2
import glob, os, errno
# Replace mydir with the directory you want
mydir = r'C:\Users\Quantum\Desktop\testoutput'
#check if directory exist, if not create it
try:
os.makedirs(mydir)
except OSError as e:
if e.errno == errno.EEXIST:
raise
for fil in glob.glob("*.jpg"):
image = cv2.imread(fil)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
cv2.imwrite(os.path.join(mydir,fil),gray_image) # write to location with same name
import os,cv2
path = r'C:\Users\me\Desktop\folder' # Source Folder
dstpath = r'C:\Users\me\Desktop\desfolder' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in asme folder")
# Folder won't used
files = os.listdir(path)
for image in files:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(dstpath,image),gray)
import cv2
from os import listdir,makedirs
from os.path import isfile,join
path = r'C:\Users\fakabbir.amin\Desktop\pdfop' # Source Folder
dstpath = r'C:\Users\fakabbir.amin\Desktop\testfolder' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in asme folder")
# Folder won't used
files = [f for f in listdir(path) if isfile(join(path,f))]
for image in files:
try:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(dstpath,image)
cv2.imwrite(dstPath,gray)
except:
print ("{} is not converted".format(image))
This code snippet will take all the images from path and write into another folder mentioned in dstpath.
I'm trying to make a script that resize multiple or a single image based on a data pulled from XML.
My question is if i have multiple images how can I print out a qusetion like "There are more than 1 image do you wish to resize image 2 also?... than maybe " Would you liek to resize image 3 also ?"
My script so far is as follow,the only problem is taht it resizez all the images at start :
import os, glob
import sys
import xml.etree.cElementTree as ET
import re
from PIL import Image
pathNow ='C:\\'
items = []
textPath = []
imgPath = []
attribValue = []
#append system argument to list for later use
for item in sys.argv:
items.append(item)
#change path directory
newPath = pathNow + items[1]
os.chdir(newPath)
#end
#get first agrument for doc ref
for item in items:
docxml = items[2]
#search for file
for file in glob.glob(docxml + ".xml"):
tree = ET.parse(file)
rootFile = tree.getroot()
for rootChild in rootFile.iter('TextElement'):
if "svg" or "pdf" in rootChild.text:
try:
textPath = re.search('svg="(.+?)"', str(rootChild.text)).group(1)
attribValue.append(rootChild.get('elementId'))
imgPath.append(textPath)
except:
continue
for image in imgPath:
new_img_path = image[:-4] + '.png'
new_image = Image.open(new_img_path)
new_size=int(sys.argv[3]), int(sys.argv[4])
try:
new_image.thumbnail(new_size, Image.ANTIALIAS)
new_image.save(new_img_path, 'png')
except IOError:
print("Cannot resize picture '%s'" % new_img_path)
finally:
new_image.close()
print("Done resizeing image: %s " % new_img_path)
Thank you in advance.
Zapo
Change your final loop to:
for idx, image in enumerate(imgPath):
#img resizing goes here
count_remaining = len(imgPath) - (idx+1)
if count_remaining > 0:
print("There are {} images left to resize.".format(count_remaining))
response = input("Resize image #{}? (Y/N)".format(idx+2))
#use `raw_input` in place of `input` for Python 2.7 and below
if response.lower() != "y":
break
Loading and saving images in OpenCV is quite limited, so... what is the preferred ways to load all images from a given folder? Should I search for files in that folder with .png or .jpg extensions, store the names and use imread with every file? Or is there a better way?
Why not just try loading all the files in the folder? If OpenCV can't open it, oh well. Move on to the next. cv2.imread() returns None if the image can't be opened. Kind of weird that it doesn't raise an exception.
import cv2
import os
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
return images
I used skimage. You can create a collection and access elements the standard way, i.e. col[index]. This will give you the RGB values.
from skimage.io import imread_collection
#your path
col_dir = 'cats/*.jpg'
#creating a collection with the available images
col = imread_collection(col_dir)
import glob
cv_img = []
for img in glob.glob("Path/to/dir/*.jpg"):
n= cv2.imread(img)
cv_img.append(n)`
If all images are of the same format:
import cv2
import glob
images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')]
For reading images of different formats:
import cv2
import glob
imdir = 'path/to/files/'
ext = ['png', 'jpg', 'gif'] # Add image formats here
files = []
[files.extend(glob.glob(imdir + '*.' + e)) for e in ext]
images = [cv2.imread(file) for file in files]
you can use glob function to do this. see the example
import cv2
import glob
for img in glob.glob("path/to/folder/*.png"):
cv_img = cv2.imread(img)
You can also use matplotlib for this, try this out:
import matplotlib.image as mpimg
def load_images(folder):
images = []
for filename in os.listdir(folder):
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
return images
import os
import cv2
rootdir = "directory path"
for subdir, dirs, files in os.walk(rootdir):
for file in files:
frame = cv2.imread(os.path.join(subdir, file))
To add onto the answer from Rishabh and make it able to handle files that are not images that are found in the folder.
import matplotlib.image as mpimg
images = []
folder = './your/folder/'
for filename in os.listdir(folder):
try:
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
except:
print('Cant import ' + filename)
images = np.asarray(images)
Here is a simple script that feature opencv, scikit image, and glob
#!C:\Users\test\anaconda3\envs\data_aquisition\python.exe
import glob
import argparse
from timeit import default_timer as timer
import skimage
from skimage.io import imread_collection
import cv2
def get_args():
parser = argparse.ArgumentParser(
description='script that test the fastest image loading methods')
parser.add_argument('src_path', help = "diractorry that contains the ims")
parser.add_argument('extension', help = "extension of the images",choices=['jpg','png','webp'])
return parser.parse_args()
def load_imgs_scikit_image_collection(path:str):
#creating a collection with the available images
col = imread_collection(path)
print('loaded: ',len(col),' imgs')
return col
def load_imgs_scikit_image_glob(path):
imgs = []
for img in glob.glob(path):
imgs.append(skimage.io.imread(img))
return imgs
def load_image_opencv(path:str):
imgs = []
for f in glob.glob(path):
imgs.extend(cv2.imread(f))
return imgs
def load_image_opencv_glob(path:str):
filenames = glob.glob(path)
filenames.sort()
images = [cv2.imread(img) for img in filenames]
return images
def laod_images_opencv_extisions(path):
ext = [".jpg",".gif",".png",".tga",".webp"] # Add image formats here
files = []
images = []
[files.extend(glob.glob(path + '/*' + e)) for e in ext]
images.extend([cv2.imread(file) for file in files])
return images
def laod_images_ski_extisions(path):
ext = [".jpg",".gif",".png",".tga",".webp"] # Add image formats here
files = []
images = []
[files.extend(glob.glob(path + '/*' + e)) for e in ext]
images.extend([skimage.io.imread(file) for file in files])
return images
def show_image(img):
window_name = 'image'
cv2.imshow(window_name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
args = get_args()
dir = args.src_path+'/*.'+args.extension
start = timer()
imgs=load_imgs_scikit_image_collection(dir)
end = timer()
print('scikit_image image collection',end - start) #time 0.08381089999999991
show_image(imgs[2])
start = timer()
load_imgs_scikit_image_glob(dir)
end = timer()
print('scikit_image and glob',end - start) #time 16.627431599999998
# dir = args.src_path+'\\.*'+args.extension
start = timer()
imgs_opencv = load_image_opencv_glob(dir) #time 10.9856656
end = timer()
print('opencv glob',end - start)
show_image(imgs_opencv[2])
start = timer()
valid_imgs_opencv = laod_images_opencv_extisions(args.src_path) #time 11.318516700000004
end = timer()
print('opencv glob extensions',end - start)
show_image(valid_imgs_opencv[2])
start = timer()
valid_imgs_opencv = laod_images_ski_extisions(args.src_path) #time 15.939870800000001
end = timer()
print('scikit_image glob extensions',end - start)
show_image(valid_imgs_opencv[2])
main()
Command to run script: python best_image_loader.py D:\data\dataset\radar_dome\manual png
png is used to load only png files.
Output
loaded: 876 imgs
scikit_image image collection 0.08248239999999996
scikit_image and glob 14.939381200000001
opencv glob 10.9708085
opencv glob extensions 10.974014100000005
scikit_image glob extensions 14.877048600000002
your_path = 'your_path'
ext = ['*.jpg', '*.png', '*.gif'] # Add image formats here
images = []
not_copy = 0
for item in [your_path + '/' + e for e in ext]:
images += glob(item)