I am new in python and following this article https://www.mygreatlearning.com/blog/face-recognition/#whatsopencv to extract features from face. After trying it, I realise that I do looping inside directory Images for each image and then save it inside face_enc:
datas = {"encodings": knownEncodings, "names": knownNames}
f = open("face_enc", "wb")
f.write(pickle.dumps(datas))
f.close()
So, the thing that makes me confused is, let's say that I have 50 images inside Images directory then I add another 100 images (just for example), so I will do looping from the start (1-150 images) and then save it in face_enc. Is there a way to update data inside face_enc without saving it from the start to saving time?
Related
So I have this bit of code, which clips out a shapefile of a tree out of a Lidar Pointcloud. When doing this for a single shapefile it works well.
What I want to do: I have 180 individual tree shapefiles and want to clip every file out of the same pointcloud and save it as a individual .las file.
So in the end I should have 180 .las files. E.g. Input_shp: Tree11.shp -> Output_las: Tree11.las
I am sure that there is a way to do all of this at once. I just dont know how to select all shapefiles and save the output to 180 individual .las files.
Im really new to Python and any help would be appreciated.
I already tried to get this with placeholders (.format()) but couldnt really get anywhere.
from WBT.whitebox_tools import WhiteboxTools
wbt = WhiteboxTools()
wbt.work_dir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.clip_lidar_to_polygon(i="Pointcloud_to_clip.las", polygons="tree_11.shp", output="Tree11.las")
I don't have the plugin you are using, but you may be looking for this code snippet:
from WBT.whitebox_tools import WhiteboxTools
wbt = WhiteboxTools()
workDir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.work_dir = workDir
# If you want to select all the files in your work dir you can use the following.
# though you may need to make it absolute, depending on where you run this:
filesInFolder = os.listDir(workDir)
numberOfShapeFiles = len([_ for _ in filesInFolder if _.endswith('.shp')])
# assume shape files start at 0 and end at n-1
# loop over all your shape files.
for fileNumber in range(numberOfShapeFiles):
wbt.clip_lidar_to_polygon(
i="Pointcloud_to_clip.las",
polygons=f"tree_{fileNumber}.shp",
output=f"Tree{fileNumber}.las"
)
This makes use of python format string templates.
Along with the os.listdir function.
I try to create my own image datasets for machine learning.
The workflow I thought is the following :
①Load all image files as an array in the folder.
②Label the loaded images
③Split loaded image files to image_data and label_data.
④Finally, split image_data to image_train_data and image_test_data and split label_data to label_train_data and label_test_data.
However, it doesn't go well in the first step(①).
How can I load all image data efficiently?
And if you implement an image data set for machine learning according to this workflow, how you handle it?
I wrote following code.
cat_im = cv2.imread("C:\\Users\\path\\cat1.jpg")
But, Am I forced writing \cat1.jpg , \cat2.jpg ,\cat3.jpg.....?
## you can find all images like extenstion
import os,cv2
import glob
all_images_path= glob.glob('some_folder\images\*png') ## it gives path of images as list
## then you can loop over all files
loaded_images = []
for image_path in all_images_path:
image = cv2.imread(image_path)
loaded_images.append(image)
## lets assume your labels are just name of files and its like cat1.png,cat2.png etc
labels = []
for image_path in all_images_path:
labels.append(os.basename(image_path))
I'm so much newbie in openCV/Python tasks. I use Python 3.7 and openCV 4 running by a JNotebook. The question: I wanna save just 1,000 images from a dataset with 10,000 pictures, extracting them from it and write only those 1,000.jpeg in a new folder, is it possible using openCV package in Python? I've already had a list of names (1,000 images).
If you need just to copy files, you even don't need OpenCV tools:
out_folder_path = '...'
in_folder_path = '...'
images_to_save_names = [...]
for image_name in images_to_save_names:
cur_image_path = os.path.join(in_folder_path, image_name)
cur_image_out_path = os.path.join(out_folder_path, image_name)
shutil.copyfile(cur_image_path, cur_image_out_path)
If you have image names and their binary data from some specific DS file(.csv, .hdf, e.t.c.), you can use cv2.imwrite(path, image) instead of copying.
Assuming you have OpenCV correctly installed on your machine, you can first read the images with img = cv.imread(filename) and then write them with cv.imwrite(filename, img).
I am working on DICOM images, I have 5 scans(folders) each scan contain multiple images, after working some preprocessing on the images, I want to save the processed images in a single file using "np.save", I have the code below that save each folder in a separate file:
data_path = 'E:/jupyter/test/LIDC-IDRI/'
patients_data = os.listdir(data_path)
for pd in range(len(patients_data)):
full_path = load_scan(data_path + patients_data[pd])
after_pixel_hu = get_pixels_hu(full_path)
after_resample, spacing = resample(after_pixel_hu, full_path, [1,1,1])
np.save(output_path + "images_of_%s_patient.npy" % (patients_data[pd]), after_resample)
load_scan is a function for loading(reading) DICOM files, what I want to do with this code is to save all processed images in a single file, not in five files, can anyone tell me how to do that, please?
The first thing to notice is that you are using %s with patients_data[pd]. I assume patients_data is a list of the names of the patients, which means you are constructing a different output path for each patient - you are asking numpy to save each of your processed images to a new location.
Secondly, .npy is probably not the file type you want to use for your purposes, as it does not handle appending data. You probably want to pick a different file type, and then np.save() to the same file path each time.
Edit: Regarding file type, a pdf may be your best option, where you can make each of your images a separate page.
Can anyone tell me how to store images in python dictionary (dict), and how to retrieve images from the dictionary based on the key value ?
It is better to store images in files and then reference them with a filename:
pictures = {'mary': '001.jpg', 'bob', '002.jpg'}
filename = pictures['mary']
with open(filename. 'rb') as f:
image = f.read()
That said, if you want to store images directly in a dictionary, just add them:
pictures = {}
with open('001.jpg', 'rb') as f:
image = f.read()
pictures['mary'] = image
Images aren't special, they are just data.
Personally, if I had to go down that road, I would load the image into a variable and then add it to dictionary as a value {key:value} as you would with any other variable. If you are using Pygame you don't need to load it as a file and can already load the image using pygame.image.load(file) .
Just a note : Loading image files, especially JPEGs in binary as suggested is tricky. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files.
As with images in dictionaries, you could even nest a list of images in the dictionary (along with a key), Having a list is one way to easily animate by looping the list within each key. {'key1' : ['value1', 'value2','value2']}
Remember however that once you pack a variable inside a dictionary, the value of the variable would not change and remain constant inside the dictionary, unless you specifically update the dictionary. Just updating the value outside of the dictionary, would not affect it's original value that it was given when it was placed inside the dictionary.
Example: (using pygame, load images into a list, and nest it into a dictionary for recall)
def load_image(file):
"""loads and prepares image from data directory"""
file = os.path.join(main_dir, 'data', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))
return surface.convert()
def load_images(*files):
""" function to load a list of images through an *arg and return a list
of images"""
images = []
for file in files:
images.append(load_image(file))
return images
bomb_images = load_images('bomb.gif', 'bomb2.gif','bomb3.gif')
explosion_images = load_images('ex1.gif', 'ex2.gif', 'ex3.gif')
# now all the images are inside a list and you can pack them in a dictionary
image_dict = {'bomb' : bomb_images, 'explode' :explosion_images}
# to call the image would be the same as you call any value from the dict
bombs = image_dict['bomb']
image_ready_for_blit = bombs[0]
# changing the slice position allows animation loops [0]