I want to sort many face images to make good face images data. So, I want there's no blur, no multiple faces in one image in my images data.
I've tried the code, but my code just can check single image at once
image = face_recognition.load_image_file("./pictures/image.jpg")
face_locations = face_recognition.face_locations(image)
print(face_locations)
Is there any code to make my code can detect multiple images at once? I'd appreciate any answer. Thanks in advance!
There are no API calls in face_recognition, that allows loading several images in on time.
This page with the API documentation at readthedocs.
So I think you have to load the list of the images from a directory and then use it in a loop to get images objects.
This is This is great SO thread with answer how to load the list of files from directory using Python: Find all files in a directory with extension .txt in Python
I have no idea which framework or library you are using and you are not specific either.
As tags suggest I will do it in pythonic way.
Walk over the current directory and find and filter files by extension.
import os
def getfiles():
for root, dirs, files in os.walk("."):
for name in files:
yield os.path.join(root, name)
files = filter(lambda image: (image[-3:] == '.jpg' or image[-3:] == '.png'), getfiles())
# now you can loop your image detection api call.
for file in files
image = face_recognition.load_image_file(file)
face_locations = face_recognition.face_locations(image)
print(face_locations)
Related
I have Folder Named A, which includes some Sub-Folders starting name with Alphabet A.
In these Sub-Folders different images are placed (some of the image formats are .png, jpeg, .giff and .webp, having different names
like item1.png, item1.jpeg, item2.png, item3.png etc). From these Sub-Folders I want to get list of path of those images which endswith 1.
Along with that I want to only get 1 image file format like for example only for jpeg. In some Sub-Folders images name endswith 1.png, 1.jpeg, 1.giff and etc.
I only want one image from every Sub-Folder which endswith 1.(any image format). I am sharing the code which returns image path of items (ending with 1) for all images format.
CODE:
here is the code that can solve your problem.
import os
img_paths = []
for top,dirs, files in os.walk("your_path_goes_here"):
for pics in files:
if os.path.splitext(pics)[0] == '1':
img_paths.append(os.path.join(top,pics))
break
img_paths will have the list that you need
it will have the first image from the subfolder with the name of 1 which can be any format.
Incase if you want with specific formats,
import os
img_paths = []
for top,dirs, files in os.walk("your_path_goes_here"):
for pics in files:
if os.path.splitext(pics)[0] == '1' and os.path.splitext(pics)[1][1:] in ['png','jpg','tif']:
img_paths.append(os.path.join(top,pics))
break
Thanks, to S3DEV for making it more optimized
from pdf2image import convert_from_path
images = convert_from_path('path.pdf',poppler_path=r"E:/software/poppler-0.67.0/bin")
for i in range(len(images)):
images[i].save('image_name'+ str(i) +'.jpg', 'JPEG')
But now I want to convert more than 100 pdf files into images.
Is there any way?
Thanks in advance.
You can use glob to 'glob' the file names into a list: Python glob is here https://docs.python.org/3/library/glob.html - but it's a general expression for using wildcard expansion in the (*nix) filesystem [https://en.wikipedia.org/wiki/Glob_(programming)]. I assume it works under windows :)
Then you just loop over the files. Hey presto!
import glob
from pdf2image import convert_from_path
poppler_path = r"E:/software/poppler-0.67.0/bin"
pdf_filenames = glob.glob('/path/to/image_dir/*.pdf')
for pdf_filename in pdf_filenames:
images = convert_from_path(pdf_filename, poppler_path=poppler_path)
for i in range(len(images)):
images[i].save(f"{pdf_filename}{i}.jpg", 'JPEG')
!TIP: f"{pdf_filename}{i}.jpg" is a python f-string which gives a the reader a better idea of what the string will look like eventually. You might want to zero pad the integers there, because at some point you might want to 'glob' those or some such. There are lots of ways to achieve that - see How to pad zeroes to a string? for example.
You will possibly need to use the os module.
First step:
Use the os.listdir function like this
os.listdir(path to folder containing pdf files)
to get a list of paths within that folder.
To be more specific the os.isfile() to check if the current path is a file or a folder .
Perform the conversion if the path lead to a file like this.
images = convert_from_path('path.pdf',poppler_path=r"E:/software/poppler-0.67.0/bin")
for i in range(len(images)):
images[i].save('image_name'+ str(i) +'.jpg', 'JPEG')
Otherwise use recursion to traverse the folder even more.
Here's a link to a repo where I recursively resized images in a folder . It could be useful to digest this idea.
Link to a recursive resizing of images in a given path.
I am attempting to copy and apply a horizontal transformation to all images from one of my directories (directory_a), and then write them to another (directory_b) using cv2. All images are in png format. This is my first time using open cv/cv2.
When I use the code I currently have (see below), the transformation is applied but the images are saved to the same directory. Does anyone know how to properly do this?
from glob import glob
import cv2
#Flip Image horizontally and add Image to directory_b Folder
directory_a = '/content/drive/MyDrive/no_copies2/train/directory_a_images'
directory_b = '/content/drive/MyDrive/no_copies2/train/directory_b_images'
input_files = glob(os.path.join(directory_a, "*.png"))
for file in input_files:
img = cv2.imread(file)
horizontal_img = cv2.flip(img, 1)
cv2.imwrite(os.path.join(directory_b, file + '_flip_v' + '.png'), horizontal_img)
file variable is holding the path to the whole path, try:
cv2.imwrite(os.path.join(directory_b, os.path.basename(file).split(".")[0] + '_flip_v' + '.png'),
horizontal_img)
It might have to do with Google Colab. Make sure you have all directories created before running the code. I previously tried doing something similar and not setting up the directories properly in the drive came with some issues.
I have a folder with numerous images(about 300), I am gonna save the python file which will be splitting the images into red, green and blue channels and saving them as _red, _green, _blue, preceded by the original image name itself in a different folder. For example if the image is named "image 001", then the images obtained after the split are: "image 001_red", "image 001_green", "image 001_blue". Now, is there a way I can obtain the images one after the other using the OS library? (Appreciate any answer what-so-ever, because this is my first question on this site)
You are asking how to read an image list from a directory to python. Here is how.
from os import walk
# Get file list
def getImageList(path):
for (dirpath, dirnames, filenames) in walk(path):
return filenames
# Demo printing file names
filelist = getImageList("path/to/image/dir")
for fileName in fileList:
print(fileName)
getImageList(path) function returns all files (not directories) in a given path. Place all your images inside a directory, and use the function to get the file list.
Hope this helped.
I'm trying to read a bunch of pgm files for a facial recognition project.
These files lie in an overall folder called "negative" and within the negative folder are subfolders. This portion of my script is supposed to go into all of the directories, store the filenames in an array, and store the "image file" in another array using OpenCV.
os.chdir("../negative")
dirnames = os.listdir(".")
neg_names = []
for i in dirnames:
if os.path.isdir(i):
os.chdir(i)
neg_names.append(os.listdir("."))
os.chdir("..")
face = cv2.imread(i,-1)
faces_negatives.append(face)
print faces_negatives
For some reason when it prints the array I get NONE in every index (there are 40 of them). From my understanding I should be getting binary values from this. This code works file with jpg files.
Just in case anyone else runs into this issue, I found a solution:
I figured out the issue I was having had to do with the path that I was sending into the function "imread". The full path of the file needs to be passed into the function in order for it to read properly. The issue was resolved when I entered in the full path of the image