I have an image dataset in the .mat format, what I want is to load this dataset and visualize it's images to interact with them such as resize them and save them in folder in the format that enable me to show them such as .jpg, .png, etc. How can I do that?
What I did is save the dataset in the scipy.io path in the python site-packages and write the following code:
import scipy.io as sio
dbpath = sio.loadmat('COFW_train_color.mat')
listing = os.listdir(dbpath)
num_samples = size(dbpath)
for file in listing:
im = (dbpath + '\\' + file)
imag = cv2.imread(im)
cv2.imshow(imag)
But this did not give me what i need and also return me the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'COFW_train_color.mat'
I also tried to use the full path to the dataset as folloe:
dbpath = "C:\\Users\\SONY\\AppData\\Local\\Programs\\Python\\Python35\\Lib\\site-packages\\scipy\\io\\COFW_train_color.mat"
but I received another error message:
NotImplementedError: Please use HDF reader for matlab v7.3 files
How can I reach and interact with this type of dataset and visualize it's images? can anyone please help me and I will be thankful.
pip install mat73
import mat73
data_dict = mat73.loadmat('COFW_train_color.mat')
Related
I am doing an ML project in google colab. I need to pre-process the whole image in the train set and replace those images with the newly preprocessed ones provided that train set images are already uploaded in "content/train/images/". I created an image_preprocessing function where the input is the image and returns preprocessed image. Now I need to save this image by replacing previous one.
This is my code :
import cv2
import glob
import os
path = "/content/train/images/*.jpg"
for file in glob.glob(path):
img = cv2.imread(file)
file_name = os.path.basename(file)
img_preprocessed = image_preprocessing(img)
with open(file, 'w') as f:
f.write(img_preprocessed)
print(file_name + " preprocessed and saved\n")
I am a newbie in python. Please help. Thanks in advance.
What you have in your example is not too far but you are trying to save an image using a syntax made to write a text file (with open(file, 'w) as f).
As you are using openCV, you can directly save with cv2.imwrite(file, img_preprocessed). All put together:
import cv2
import glob
import os
path = "/content/train/images/*.jpg"
for file in glob.glob(path):
img = cv2.imread(file)
file_name = os.path.basename(file)
img_preprocessed = image_preprocessing(img)
# Save the img_preprocessed as a picture with a path matching 'file'
cv2.imwrite(file, img_preprocessed)
print(file_name + " preprocessed and saved")
NOTE: This example will overwrite your original images as requested in the question. However, it may be an issue for repeatability, it may be better to save them in a preprocessed_images folder so you retain the source. But it may not be required, it is up to your usage.
train_data = object_detector.DataLoader.from_pascal_voc(
'images_jpg_splitted/train/img',
'images_jpg_splitted/train/xml',
['bat']
)
val_data = object_detector.DataLoader.from_pascal_voc(
'images_jpg_splitted/test/img',
'images_jpg_splitted/test/xml',
['bat']
)
I am trying to detect bat from images. I have labeled the data using labelImg.
While trying to load the data from tflite_model_maker, object_detector.DataLoader.from_pascal_voc returns empty data. I have tried not splitting the image and XML file and it still did not work.
The error was in the image file. The file supported was only jpeg but although the extension was a jpeg, it was not recognizing the images as jpeg maybe because it was a png file and the extension was renamed. So, I used PIL to convert them to jpeg.
import PIL.Image
import glob
import os
if not "converted" in os.listdir():
os.mkdir("converted")
lst_imgs = [i for i in glob.glob("*.jpeg")]
print(lst_imgs)
for i in lst_imgs:
img = PIL.Image.open(i)
img = img.convert("RGB")
img.save("converted\\"+i, "JPEG")
print("Done.")
os.startfile("converted")
I ran into this issue because I was specifying the path to my annotations using ~. Starting my path with /home/myuser fixed this for me.
could you please write the code in below style?
dataloader = object_detector.DataLoader.from_pascal_voc(image_dir, annotations_dir, label_map={1: "person", 2: "notperson"})
It may be a syntax issue.
I'm in a beginner neural networks class and am really struggling.
I have a dataset of images that isn't big enough to train my network with, so I'm trying to augment them (rotate/noise addition etc.) and add the augmented images onto the original set. I'm following the code found on Medium: https://medium.com/#thimblot/data-augmentation-boost-your-image-dataset-with-few-lines-of-python-155c2dc1baec
However, I'm encountering ValueError: Could not find a format to read the specified file in mode 'i'
Not sure what this error means or how to go about solving it. Any help would be greatly appreciated.
import random
from scipy import ndarray
import skimage as sk
from skimage import transform
from skimage import util
path1 = "/Users/.../"
path2 = "/Users/.../"
listing = os.listdir(path1)
num_files_desired = 1000
image = [os.path.join(path2, f) for f in os.listdir(path2) if os.path.isfile(os.path.join(path2, f))]
num_generated_files = 0
while num_generated_files <= num_files_desired:
image_path = random.choice(image)
image_to_transform = sk.io.imread(image_path)
137 if format is None:
138 raise ValueError(
--> 139 "Could not find a format to read the specified file " "in mode %r" % mode
140 )
141
ValueError: Could not find a format to read the specified file in mode 'i'
I can see few possiblities. Before passing to them. I'd like to express what is your error. It's basically an indicator that your images cannot be read by sk.io.imread(). Let me pass to the possible things to do:
Your [os.path.join(path2, f) for f in os.listdir(path2) if os.path.isfile(os.path.join(path2, f))] part may not give the image path correctly. You have to correct it manually. If so, you can manually give the exact folder without doing such kind of a loop. Just simply use os.listdir() and read the files manually.
You can also use glob to read the files that having same extension like .jpg or stuff.
Your files may be corrupted. You can simply eliminate them by using PIL and read the images with PIL like image = Image.open() first and use image.verify() method.
Try to read about sk.io.imread(filename, plugin='' the plugin part may resolve your issue.
Hope it helps.
I'm currently having some issues removing a file in python. I am creating a temporary file for pdf to image conversion. It is housed in a folder that holds a .ppm file and converts it to a .jpg file. It then deletes the temporary .ppm file. Here is the code:
import pdf2image
from PIL import Image
import os
images = pdf2image.convert_from_path('Path to pdf.pdf', output_folder='./folder name')
file = ''
for files in os.listdir('./folder name'):
if files.endswith(".ppm"):
file = files
path = os.path.join('folder name',file)
im = Image.open(path)
im.save("Path to image.jpg")
im.close()
os.remove(path)
The issue is at the end in the os.remove(path). I get the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'path to ppm file'
I would appreciate any help and thanks in advance.
Not really the answer to your question, but you can just output in the correct format at the start, and avoid the issue in the first place:
pdf2image.convert_from_path('Path to pdf.pdf', output_folder='./folder name', fmt='jpg')
To actually answer your question, I'm not sure why you're having the issue, because really the close() should prevent this problem. Perhaps check out this answer and try using a with statement? Or maybe the permissions release is just delayed, I'm curious what throwing that remove in a loop for as long as it throws an exception would do.
Edit: To set the name, you'll want to do something like:
images = pdf2image.convert_from_path('Path to pdf.pdf', output_folder='./folder name', fmt='jpg')
for image in images:
# Save the image
The pdf2image documentation looks like it recommends using a temporary folder, like in this example, and then you can just .save(...) the PIL image:
import tempfile
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path('/home/kankroc/example.pdf', output_folder=path)
# Do something here
Edit: I realized that the reason it was in use is probably because you need to close() all the images in images. You should read up on the pdf2image documentation and about the PIL images that it spits out for more details.
I'm newbie in python and in geoprocessing. I'm writing some program to calculate ndwi. To make this, I try to open geotiff dataset with gdal, but dataset can't be opened. I tried to open different tiff files (Landsat8 multiple data, Landsat7 composite, etc), but dataset is always None.
What reason to this could be? Or how can i find it out?
Here's a part of code:
import sys, os, struct
import gdal, gdalconst
from gdalconst import *
import numpy as np
from numpy import *
class GDALCalcNDWI ():
def calcNDWI(self, outFilePath):
gdal.AllRegister()
# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()
filePath = "C:\\Users\\Daria\\Desktop.TIF\\170028-2007-05-21.tif"
# Open
dataset = gdal.Open(filePath, gdal.GA_ReadOnly)
# Check
if dataset is None:
print ("can't open tiff file")
sys.exit(-1)
Thanks
Whenever you have a well-known file reader that is returning None, make sure the path to your file is correct. I doubt you have a directory called Desktop.TIF, I'm assuming you just made a typo in your source code. You probably want C:\\Users\\Dara\\Desktop\\TIF\\170028-2007-05-21.tif as the path (note that Desktop.TIF ==> Desktop\\TIF).
The safest thing to do is right click on the file, go to properties, and copy/paste that path into your python source code.