I created an npz(compressed file) using following code:
import pandas as pd
from tqdm import tqdm
from keras.preprocessing import image
import numpy as np # for mathematical operations
from keras.preprocessing import image # for preprocessing the images
anomoly_data = pd.read_csv('/code_data/anomoly_data.csv')
f = open('/code_data/anomoly_data.npz', 'wb')
for i in tqdm(range(anomoly_data.shape[0])):
img = image.load_img(anomoly_data['image'][i], target_size=(224,224,3))
# converting it to array
img = image.img_to_array(img)
# normalizing the pixel value
img = img/255
np.savez_compressed(f, img)
f.close()
the compressed file has size of 52GBs which indicates that it contains data of all the images.
in anomoly_data.csv i have the path for each image and entries in .csv are
188604
and now i'm trying to read the data of all the images from compressed using following code:
f=open("/code_data/anomoly_data.npz","rb")
for i in tqdm(range(anomoly_data.shape[0])):
img=np.load(f)
print(img)
and then I get this error telling me that there is only one ndarray in the compressed file, Error:
<numpy.lib.npyio.NpzFile object at 0x7fdfd1d437c0>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-29b2878e9fe9> in <module>
1 f=open("/code_data/anomoly_data.npz","rb")
2 for i in range(anomoly_data.shape[0]):
----> 3 img=np.load(f)
4 print(img)
5
~/Programs/Anaconda/lib/python3.8/site-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
442 # Try a pickle
443 if not allow_pickle:
--> 444 raise ValueError("Cannot load file containing pickled data "
445 "when allow_pickle=False")
446 try:
ValueError: Cannot load file containing pickled data when allow_pickle=False
but i get data of only one array which suggests that there is either data of only one image or it is deformed. implying that compressed file is not genrated properly.
is there any better way to do this?
Related
I created a program that would decompile a video, run a roboflow yolo v5 model on each frame and then recompile the video. I am getting an error when saving the last frame of the model. Also I want to integrate the loop which opens the file and then runs the model instead of saving the video frame by frame and then running the model on the saved frames... This version of the code provided is where I tried to integrate the loops and I get an error during the second loop:
import cv2
from roboflow import Roboflow
import numpy as np
import cv2
import skvideo.io
import numpy as np
rf = Roboflow(api_key="API_KEY")
project = rf.workspace("WORKSPACE_NAME").project("PROJECT_NAME")
model = project.version(4).model
vidcap = cv2.VideoCapture('video2.mp4') #runs video file
success,image = vidcap.read() #creates two variables success and image, success is a boolean that returns true
count = 0 # as long as videocapture returns another image
while success:
#cv2.imwrite("/content/frames2/frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
#print('Read a new frame: ', success)
count += 1
height,width,layers=cv2.image.shape
array=np.zeros(shape=(count, height, width, layers))
print(count)
for i in range(count):
name = "/content/frames2/frame"+str(i)+".jpg" #run model on each frame of video
success,image=vidcap.read()
prediction=model.predict((image), confidence=40, overlap=30)
prediction.save(name)
print(i)
height,width,layers=cv2.imread(name).shape
array=np.zeros(shape=(count, height, width, layers))
for j in range(0,count):
foto=cv2.imread("/content/frames2/frame"+str(j)+".jpg")
#print(foto.shape)
array[j]=foto
array = array.astype(np.uint8)
skvideo.io.vwrite("codevideo.mp4", array)
This is the error I'm getting:
ValueError Traceback (most recent call last)
<ipython-input-2-c47bf522ab91> in <module>
23 name = "/content/frames2/frame"+str(i)+".jpg" #run model on each frame of video
24 success,image=vidcap.read()
---> 25 prediction=model.predict((image), confidence=40, overlap=30)
26 prediction.save(name)
27 print(i)
/usr/local/lib/python3.8/dist-packages/roboflow/models/object_detection.py in predict(self, image_path, hosted, format, classes, overlap, confidence, stroke, labels)
176 image_dims = {"width": "0", "height": "0"}
177 else:
--> 178 raise ValueError("image_path must be a string or a numpy array.")
179 else:
180 # Create API URL for hosted image (slightly different)
ValueError: image_path must be a string or a numpy array.
I created a program that would decompile a video, run a roboflow yolo v5 model on each frame and then recompile the video. I am getting an error when saving the last frame of the model. I thought I would be able to run the model on each frame saved locally in the numpy array but it throws the error detailed above...
Here is my code. I used cv2 to load the jpeg image because for some weird reason pil couldn't load the image directly.
import pytesseract
import shutil
import os
import random
import cv2
from PIL import Image
img = cv2.imread('/content/believe in yourself.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
extractedInformation = pytesseract.image_to_string(img)
print(extractedInformation)
here was the error which i was facing.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-38-e446955037e6> in <module>
----> 1 extractedInformation = pytesseract.image_to_string(img)
2 print(extractedInformation)
5 frames
/usr/local/lib/python3.8/dist-packages/PIL/Image.py in save(self, fp, format, **params)
2121 the resulting size will be rounded up.
2122
-> 2123 :param factor: A greater than 0 integer or tuple of two integers
2124 for width and height separately.
2125 :param box: An optional 4-tuple of ints providing
KeyError: 'PNG'
Th h5 file does not have a group or subgroup, and when I try to extract images it shows me this error. These are depth images.
This code works for h5 file with group i.e. images then I just write image_ds = hf['images'] and it works, but for h5 file without group doesn't work.
Maybe some error in imwrite function, because when I print(IMAGE_arr) and print(imagename) it prints fine. The number of dimensions are 3 and type is float32
Here is my code:
import h5py
import numpy as np
import cv2
save_dir = 'C:/Users.../depth_imgs'
with h5py.File('depth.h5', 'r') as hf:
image_ds = hf
for imagename in image_ds.keys():
IMAGE_arr = image_ds[imagename][()]
cv2.imwrite(f"{save_dir}/{imagename}", IMAGE_arr)
cv2.waitKey(1000)
cv2.destroyAllWindows()
Loaded data:
enter image description here
enter image description here
I am working on an Image Classification problem using Keras/Tensorflow. The problem is that since I am using an IDE like Pycharm (I also use Jupyter Notebook), I am curious to know if there is any way where I can load the dataset from the directory only once and then when I re-run the whole .py file, I just use the images from already loaded data?
labels = ['rugby', 'soccer']
img_size = 224
def get_data(data_dir):
data = []
for label in labels:
path = os.path.join(data_dir, label)
class_num = labels.index(label)
for img in os.listdir(path):
try:
img_arr = cv2.imread(os.path.join(path, img))[...,::-1] #convert BGR to RGB format
resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size
data.append([resized_arr, class_num])
except Exception as e:
print(e)
return np.array(data)
Now we can easily fetch our train and validation data.
train = get_data('../input/traintestsports/Main/train')
val = get_data('../input/traintestsports/Main/test')
Every time get_data is called, it would require additional time to load entire datset
You can read in each image using the cv2.imread() method, and use the np.save() method to save all the images (put into a single array) to save the data into a binary file in .npy format:
import cv2
import numpy as np
imgs = ['image1.png', 'image2.png', 'image3.png', 'image4.png']
# Map each str to cv2.imread, convert map object to list, and convert list to array
arr = np.array(list(map(cv2.imread, imgs)))
np.save('data.npy', arr)
When you want to access the data, you can use the np.load() method:
import numpy as np
arr = np.load('data.npy')
You can install cv2 (OpenCV) via the command prompt command:
pip install opencv-python
and numpy with
pip install numpy
If you have a more complex data type, you can use the pickle.dump() method to save your data sterilized into a file:
import pickle
data = {"data": ['test', 1, 2, 3]} # Replace this with your dataset
with open("data.pickle", "wb") as f:
pickle.dump(data, f)
When you want to access the data, you can use the pickle.load() method:
import pickle
with open("data.pickle", "rb") as f:
data = pickle.load(f)
print(data)
Output:
{'data': ['test', 1, 2, 3]}
The pickle module is built into python.
I try open a tif image with 16-bit per pixel and multi-band to convert it in a raw file. I'm using PIL with the next commands i = Image.open('image.tif') and after I use rawData = i.tostring(). It doesn't work with multi-band tif image.
The error is:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
The directory contains the file.
How I must do it ?
GDAL is pretty good at opening multiband rasters, and supports 11 different band types, including int16.
from osgeo import gdal
import numpy as np
ds = gdal.Open('image.tif')
# loop through each band
for bi in range(ds.RasterCount):
band = ds.GetRasterBand(bi + 1)
# Read this band into a 2D NumPy array
ar = band.ReadAsArray()
print('Band %d has type %s'%(bi + 1, ar.dtype))
raw = ar.tostring()