I'm trying to reshape an image after reshaping it, I'm facing problems when it comes to the saving method. Here's the code I'm trying to run:
import nibabel as nib
import numpy as np
from nibabel.testing import data_path
import os
example_filename = os.path.join("D:/Volumes convertidos LIDC",
'teste001converted.nii.gz')
img = nib.load('teste001converted.nii.gz')
print (img.shape)
newimg = img.get_fdata().reshape(332,360*360)
print (newimg.shape)
final_img = nib.Nifti1Image(newimg, img.affine)
nib.save(final_img, os.path.join("D:/Volumes convertidos LIDC",
'test2d.nii.gz'))
And I'm getting an error:
(most recent call last):
File "d:\Volumes convertidos LIDC\reshape.py", line 17, in
final_img = nib.Nifti1Image(newimg, img.affine)
File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1756, in init
super(Nifti1Pair, self).init(dataobj,
File "C:\Python39\lib\site-packages\nibabel\analyze.py", line 918, in init
super(AnalyzeImage, self).init(
File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 469, in init
self.update_header()
File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 2032, in update_header
super(Nifti1Image, self).update_header()
File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1795, in update_header
super(Nifti1Pair, self).update_header()
File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 496, in update_header
hdr.set_data_shape(shape)
File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 880, in set_data_shape
super(Nifti1Header, self).set_data_shape(shape)
File "C:\Python39\lib\site-packages\nibabel\analyze.py", line 633, in set_data_shape
raise HeaderDataError(f'shape {shape} does not fit in dim datatype')
nibabel.spatialimages.HeaderDataError: shape (332, 129600) does not fit in dim datatype
Is there any way to solve it?
You are trying to save a numpy array, whereas the nib.save expects a SpatialImage object.
You should convert the numpy array to a SpatialImage:
final_img = nib.Nifti1Image(newimg, img.affine)
After which you can save the image:
nib.save(final_img, os.path.join("D:/Volumes convertidos LIDC", 'test4d.nii.gz'))
See the documentation and this answer for more explanation.
Edit: This will not work if newimg is a 2D image.
Related
I am implementing the HOG(Histogram of Oriented Gradient) with below code.
import io
from skimage.io import imread, imshow
from skimage.feature import hog
from skimage import exposure
from skimage import io
import matplotlib
img = imread('cr7.jpeg')
io.imshow(img)
MC = True #Fpr color images
#MC = false #for grayscale images
hogfv, hog_image = hog(img, orientations=9,
pixels_per_cell=(32,32),
cells_per_block=(4,4),
visualize = True ,
channel_axis=MC)
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0,5))
imshow(hog_image_rescaled)
I don't know why i am getting error of dimension.
Traceback (most recent call last):
File "main.py", line 22, in <module>
channel_axis=MC)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 427, in fixed_func
out = func(*new_args, **kwargs)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 348, in fixed_func
return func(*args, **kwargs)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/feature/_hog.py", line 286, in hog
dtype=float_dtype
ValueError: negative dimensions are not allowed
(base) (env) c100-110#C100-110s-iMac-2 HOG % python main.py
Traceback (most recent call last):
File "main.py", line 18, in <module>
channel_axis=MC)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 427, in fixed_func
out = func(*new_args, **kwargs)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 348, in fixed_func
return func(*args, **kwargs)
File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/feature/_hog.py", line 286, in hog
dtype=float_dtype
ValueError: negative dimensions are not allowed
Can anyone help me in finding solution to this error.
The error log says there is a problem in "line 22"
Traceback (most recent call last):
File "main.py", line 22, in <module>
channel_axis=MC)
...
ValueError: negative dimensions are not allowed
channel_axis, it's the "channel axis"! So I guess it expects an integer, rather than a bool value.
It is confirmed in the source code:
channel_axis : int or None, optional
If None, the image is assumed to be a grayscale (single channel) image.
Otherwise, this parameter indicates which axis of the array corresponds
to channels.
I think you were trying to use multichannel, which is deprecated:
multichannel : boolean, optional
If True, the last image dimension is considered as a color channel,
otherwise as spatial. This argument is deprecated: specify channel_axis instead.
By adding following, it is working for my case.
channel_axis=-1
Im trying this code https://github.com/arsfutura/face-recognition , but While running sh tasks/train.sh images/ Im getting valueerror as :-
images/rah/ra.jpg
/home/pi/.local/lib/python3.7/site-packages/facenet_pytorch/models/utils/detect_face.py:146: UserWarning: This overload of nonzero is deprecated:
nonzero()
Consider using one of the following signatures instead:
nonzero(*, bool as_tuple) (Triggered internally at ../torch/csrc/utils/python_arg_parser.cpp:882.)
bb = mask.nonzero().float().flip(1)
Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/pi/face-recognition/training/train.py", line 99, in
main()
File "/home/pi/face-recognition/training/train.py", line 84, in main
embeddings, labels, class_to_idx = load_data(args, features_extractor)
File "/home/pi/face-recognition/training/train.py", line 61, in load_data
embeddings, labels = dataset_to_embeddings(dataset, features_extractor)
File "/home/pi/face-recognition/training/train.py", line 41, in dataset_to_embeddings
_, embedding = features_extractor(transform(Image.open(img_path).convert('RGB')))
File "/home/pi/face-recognition/face_recognition/face_features_extractor.py", line 26, in call
return self.extract_features(img)
File "/home/pi/face-recognition/face_recognition/face_features_extractor.py", line 15, in extract_features
bbs, _ = self.aligner.detect(img)
File "/home/pi/.local/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", line 308, in detect
self.device
File "/home/pi/.local/lib/python3.7/site-packages/facenet_pytorch/models/utils/detect_face.py", line 66, in detect_face
tmp[(dy[k] - 1):edy[k], (dx[k] - 1):edx[k], :] = img[(y[k] - 1):ey[k], (x[k] - 1):ex[k], :]
ValueError: could not broadcast input array from shape (0,1364,3) into shape (0,0,3)
I even tried hardcoding tmp = np.zeros((0,1364, 3)) at line 65 in detect_face.py just to test, but no luck.
Why don't you use facenet within deepface? You just pass the exact image paths as pair and it builds a face recognition pipeline. I mean that verify function handles face detection and alignment in the background.
#!pip install deepface
from deepface import DeepFace
obj = DeepFace.verify("img1.jpg", "img2.jpg", model_name = 'Facenet')
print(obj["verified"])
Or you can find an identity in a data base similarly. Here, you are expected to store facial images with .jpg or .png extention in a folder and pass it to database path.
df = DeepFace.find("img1.jpg", db_path="C:/my_db", model_name = 'Facenet')
print(df.head())
I am doing a small script to paste an image into a black background to have same size images for a dataset. Here is the script:
for file in glob.glob(imagetteDir):
r = str(file)
image = Image.open(file)
img = np.zeros([100,100,3],dtype=np.uint8)
imageio.imwrite('black.tif', img)
black = Image.open('black.tif')
position = ((black.width - image.width), (black.height - image.height))
print(position)
Image.Image.paste(black,image,position)
dirr , name = r.split("imagette")
path1 = name.raplace("Hexagone_resized","Hexagone")
print(path1)
black.save(imagetteresizeDir+path1)
So Image.Image.paste() make the following error that i didnt find how to resolve:
Traceback (most recent call last):
File "C:\Users\user\Documents\Project\segmentation\Imagette creation\resizing.py", line 29, in <module>
Image.Image.paste(black,image)
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\Image.py", line 1455, in paste
im.load()
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
Have you any idea from where it might come?
Thanks in advance for your help
I am trying to extract text from a gif image using the below code, it has worked for png format not working for gif.
import pytesseract
import io
import requests
from PIL import Image
url = requests.get('http://article.sapub.org/email/10.5923.j.aac.20190902.01.gif')
img = Image.open(io.BytesIO(url.content))
text = pytesseract.image_to_string(img)
print(text)
getting this error
C:\python\lib\site-packages\PIL\Image.py:1048: UserWarning: Couldn't allocate palette entry for transparency
warnings.warn("Couldn't allocate palette entry for transparency")
Traceback (most recent call last):
File "D:/elifesciences/prox.py", line 8, in <module>
text = pytesseract.image_to_string(img)
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 345, in image_to_string
}[output_type]()
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 344, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 242, in run_and_get_output
temp_name, input_filename = save_image(image)
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 173, in save_image
image.save(input_file_name, format=extension, **image.info)
File "C:\python\lib\site-packages\PIL\Image.py", line 2088, in save
save_handler(self, fp, filename)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 507, in _save
_write_single_frame(im, fp, palette)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 414, in _write_single_frame
_write_local_header(fp, im, (0, 0), flags)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 532, in _write_local_header
transparency = int(transparency)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
Process finished with exit code 1
The idea is to convert each of the frames to RGB image before performing the OCR on them, as shown below -
for frame in range(0,img.n_frames):
img.seek(frame)
imgrgb = img.convert('RGBA')
imgrgb.show()
text = pytesseract.image_to_string(imgrgb)
print(text)
Working sample - https://colab.research.google.com/drive/1ctjk3hH0HUaWv0st6UpTY-oo9C9YCQdw
I am getting the error TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'> when I try to load a non-image dataset through the DataLoader. The versions of torch and torchvision are 1.0.1, and 0.2.2.post3, respectively. Python's version is 3.7.1 on a Windows 10 machine.
Here is the code:
class AndroDataset(Dataset):
def __init__(self, csv_path):
self.transform = transforms.Compose([transforms.ToTensor()])
csv_data = pd.read_csv(csv_path)
self.csv_path = csv_path
self.features = []
self.classes = []
self.features.append(csv_data.iloc[:, :-1].values)
self.classes.append(csv_data.iloc[:, -1].values)
def __getitem__(self, index):
# the error occurs here
return self.transform(self.features[index]), self.transform(self.classes[index])
def __len__(self):
return len(self.features)
And I set the loader:
training_data = AndroDataset('android.csv')
train_loader = DataLoader(dataset=training_data, batch_size=batch_size, shuffle=True)
Here is the full error stack trace:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1758, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 231, in <module>
main()
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 149, in main
for i, (images, labels) in enumerate(train_loader):
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torch\utils\data\dataloader.py", line 615, in __next__
batch = self.collate_fn([self.dataset[i] for i in indices])
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torch\utils\data\dataloader.py", line 615, in <listcomp>
batch = self.collate_fn([self.dataset[i] for i in indices])
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 102, in __getitem__
return self.transform(self.features[index]), self.transform(self.classes[index])
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\transforms.py", line 60, in __call__
img = t(img)
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\transforms.py", line 91, in __call__
return F.to_tensor(pic)
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\functional.py", line 50, in to_tensor
raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'>
This happens because of the transformation you use:
self.transform = transforms.Compose([transforms.ToTensor()])
As you can see in the documentation, torchvision.transforms.ToTensor converts a PIL Image or numpy.ndarray to tensor. So if you want to use this transformation, your data has to be of one of the above types.
Expanding on #MiriamFarber's answer, you cannot use transforms.ToTensor() on numpy.ndarray objects. You can convert numpy arrays to torch tensors using torch.from_numpy() and then cast your tensor to the required datatype.
Eg:
>>> import numpy as np
>>> import torch
>>> np_arr = np.ones((5289, 38))
>>> torch_tensor = torch.from_numpy(np_arr).long()
>>> type(np_arr)
<class 'numpy.ndarray'>
>>> type(torch_tensor)
<class 'torch.Tensor'>
If you want to use torchvision.transforms on a numpy array, first convert the numpy array to a PIL Image object using transforms.ToPILImage()
tf=transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((512,640)),
transforms.ToTensor()
])
it works for me.