I want to load my image and label data from a LMDB database I created. I assign a unique key to corresponding image-label pairs and add them to the LMDB (eg. image-000000001, label-000000001). While saving the images, I convert the numpy-array of the image to string using image.tostring(). Now while loading the LMDB, I see that I can get the labels very simply by passing the keys I generated, however the image-data is shown in an encoded fashion. Doing a numpy.fromstring(lmdb_cursor.get('image-000000001')) doesn't work.
I see here - the second answer, specifically, by #Ghilas BELHADJ that one has to use Caffe-datum objects to first load the data and then fetch the image using datum.data. But I don't have such a structure where the image and label are organised using the 'data' and 'label' tags. How does one read the data correctly back in the form of a numpy image from such an LMDB in python?
In Lua, this can be achieved as follows,
local imgBin -- this is the object returned from cursor:get(image-id)
local imageByteLen = string.len(imgBin)
local imageBytes = torch.ByteTensor(imageByteLen):fill(0)
imageBytes:storage():string(imgBin)
local img = Image.decompress(imageBytes, 3, 'byte')
img = Image.rgb2y(img)
img = Image.scale(img, imgW, imgH)
I don't know how to do this in Python.
import lmdb
import cv2
import numpy as np
with lmdb.open(lmdb_dir,readonly=True).begin(write=False) as txn:
for idx,(key,val) in enumerate(txn.cursor()):
img = cv2.imdecode(np.fromstring(val,dtype=np.uint8),1)
Related
My goal is to detect tables in images and transform such tables into CSV files.
I am using CascadeTabNet to detect the location of tables.
Their example works great (https://colab.research.google.com/drive/1lzjbBQsF4X2C2WZhxBJz0wFEQor7F-fv?usp=sharing), the problem is that it does not show at the end how to save the detected table as another image (nor how to actually make that table into a CSV file, but for that, I can use other code).
This is what they had:
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmcv
# Load model
config_file = '/content/CascadeTabNet/Config/cascade_mask_rcnn_hrnetv2p_w32_20e.py'
checkpoint_file = '/content/epoch_36.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# Test a single image
img = "/content/CascadeTabNet/Demo/demo.png"
# Run Inference
result = inference_detector(model, img)
# Visualization results
show_result_pyplot(img, result,('Bordered', 'cell', 'Borderless'), score_thr=0.85)
I tried to add the following to their code:
model.show_result(img, result, out_file='result.jpg')
And I get the following error:
TypeError: show_result() got an unexpected keyword argument 'out_file'
I also tried adding the "out_file" inside the "show_result_pyplot":
show_result_pyplot(img, result,('Bordered', 'cell', 'Borderless'), score_thr=0.85, out_file='result.jpg')
And I get the same error: show_result() got an unexpected keyword argument 'out_file'
Finally, I tried saving the image using pyplot from matplotlib, but it did not save the cropped area (the table area of the page) but all of it:
plt.savefig('foo.png')
I have been looking for solutions (How to crop an image in OpenCV using Python, How to detect symbols on a image and save it?, How to save cropped image using openCV?, How to save the detected object into a new image ...), and they all include cv2, but that did not work for me either.
In short, how to crop the area of the table and save it as a new image after using "inference_detector()"?
I want to load a GLB model in Python, replace the existing texture with another texture image and save it.
So far I can load, modify and export the model. Also, I found a way to append a local image to the model.
But I'm not sure how to find and replace the existing texture.
Example 3D model with one texture file: https://modelviewer.dev/shared-assets/models/Astronaut.glb
from pygltflib import GLTF2
from pygltflib.utils import ImageFormat, Image
filename = "Astronaut.glb"
gltf = GLTF2().load(filename)
image = Image()
image.uri = "new-texture.png"
gltf.images.append(image)
gltf.convert_images(ImageFormat.DATAURI)
gltf.images[0].uri
gltf.images[0].name
# How to find and replace the existing texture?
# ...
filename2 = "updated-3D-model.glb"
gltf.save(filename2)
you could do it with string replace in the gltf file. tried it like that and with python and string replace it works very smooth and fast.
torchvision.io.read_image uses as an input file stored in path argument. How can I achieve the same output if the image is stored as a variable? Of course, I can just save the image as a file and then read from it but it is additional time. Is there a way to get the same result as torchvision.io.read_image with input as a variable, not path?
If the images in memory are PIL images, you can use a transform function to convert it to a tensor in the right format (achieving the same effect as torchvision.io.read_image without the need of reading something from the disk).
import PIL
import torchvision.transforms.functional as transform
# Reads a file using pillow
PIL_image = PIL.Image.open(image_path)
# The image can be converted to tensor using
tensor_image = transform.to_tensor(PIL_image)
# The tensor can be converted back to PIL using
new_PIL_image = transform.to_pil_image(tensor_image)
I want to read some DICOM files, so I'm testing pydicom for my work, which I think is considerably useful.
Now I want to load existing DICOM files, replace the pixel data array with another pixel array (e.g. pre-processing or literally another DICOM pixel array) and most of all, I want to read it again with any DICOM viewer application.
For this test, I used the tutorial code below. This code loads a test data file. The size of image is 64*64. The code below does sub-sampling from the original data. After that, the size of image is 8*8, and the result is saved to after.dcm.
But when I read the file using a DICOM viewer app (I used 'Dicompass'), the size of DICOM image is still 64*64. What is it that I'm missing?
I referred to the pydicom documentation (http://pydicom.readthedocs.io/en/stable/getting_started.html, https://pydicom.github.io/pydicom/stable/index.html) to solve my problem.
# authors : Guillaume Lemaitre <g.lemaitre58#gmail.com>
# license : MIT
import pydicom
from pydicom.data import get_testdata_files
print(__doc__)
# FIXME: add a full-sized MR image in the testing data
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)
# get the pixel information into a numpy array
data = ds.pixel_array
print(data)
print('The image has {} x {} voxels'.format(data.shape[0],
data.shape[1]))
data_downsampling = data[::8, ::8]
print('The downsampled image has {} x {} voxels'.format(
data_downsampling.shape[0], data_downsampling.shape[1]))
# copy the data back to the original data set
ds.PixelData = data_downsampling.tostring()
# update the information regarding the shape of the data array
ds.Rows, ds.Columns = data_downsampling.shape
# print the image information given in the dataset
print('The information of the data set after downsampling: \n')
print(ds)
print(ds.pixel_array)
print(len(ds.PixelData))
ds.save_as("after.dcm")
The code looks OK. But, you are not overwriting original file.
You load the file with:
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)
where original file name is "MR_small.dcm".
Then you save the file with:
ds.save_as("after.dcm")
where destination file name is different. That means, original file is still unchanged.
You should either load "after.dcm" in your DICOM viewer to test
OR
You should overwrite the file (pydicom.filewriter.dcmwrite) while saving it.
Not a part of your problem, but if you are creating copy of original image with change in pixel data, it is recommended that you also modify instance specific information in dataset like some UIDs and InstanceNumber (0020,0013), SOPInstanceUID (0008,0018) etc.
I am trying to use SVMs to classify a set if images I have on my computer into 3 categories :
I am just facing a problem of how to load the data as in the following example , he uses a data set that is already saved.
http://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html
Me I have all the images in png format saved in a folder on my pc
You can load data as numpy arrays using Pillow, in this way:
from PIL import Image
import numpy as np
data = np.array(Image.open('yourimg.png')) # .astype(float) if necessary
couple it with os.listdir to read multiple files, e.g.
import os
for file in os.listdir('your_dir/'):
img = Image.open(os.path.join('your_dir/', file))
data = np.array(img)
your_model.train(data)