OSError: image file is truncated - python

When I am processing a bunch of images, on one of them I get this error
File "/home/tensorflowpython/firstmodel/yololoss.py", line 153, in data_generator
image, box = get_random_data(annotation_lines[i], input_shape, random=True)
File "/home/tensorflowpython/firstmodel/yololoss.py", line 226, in get_random_data
image = image.resize((nw,nh), Image.BICUBIC)
File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/Image.py", line 1858, in resize
self.load()
File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/ImageFile.py", line 247, in load
"(%d bytes not processed)" % len(b)
OSError: image file is truncated (25 bytes not processed)
I have already tried the solution suggested here but it doesn't work
my code looks like this
from PIL import Image
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True):
Image.LOAD_TRUNCATED_IMAGES = True
line = annotation_line.split()
image = Image.open(line[0])
iw, ih = image.size
h, w = input_shape
box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
try:
image.load()
except IOError:
pass # You can always log it to logger
if not random:
# resize image
scale = min(w/iw, h/ih)
nw = int(iw*scale)
nh = int(ih*scale)
dx = (w-nw)//2
dy = (h-nh)//2
image_data=0
if proc_img:
image = image.resize((nw,nh), Image.BICUBIC)
new_image = Image.new('RGB', (w,h), (128,128,128))
new_image.paste(image, (dx, dy))
image_data = np.array(new_image)/255.
# correct boxes
box_data = np.zeros((max_boxes,5))
if len(box)>0:
np.random.shuffle(box)
if len(box)>max_boxes: box = box[:max_boxes]
box[:, [0,2]] = box[:, [0,2]]*scale + dx
box[:, [1,3]] = box[:, [1,3]]*scale + dy
box_data[:len(box)] = box
return image_data, box_data
# resize image
new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
scale = rand(.25, 2)
if new_ar < 1:
nh = int(scale*h)
nw = int(nh*new_ar)
else:
nw = int(scale*w)
nh = int(nw/new_ar)
image = image.resize((nw,nh), Image.BICUBIC) #error occurs here
The difference between my error and the previous solution is, mine says OS error and the solution is for IO error
EDIT: I have figured out the image that is causing this error, it can be downloaded from this link

I tried the solution that you linked with the truncated image and it worked. You made a slight mistake when trying to apply this solution: you have to set ImageFile.LOAD_TRUNCATED_IMAGES=True, not Image.LOAD_TRUNCATED_IMAGES.
LOAD_TRUNCATED_IMAGES does not originally exist in Image module, so when you do Image.LOAD_TRUNCATED_IMAGES=True you set a new variable which is not used by the library.
So I think you juste have to do that:
from PIL import ImageFile, Image
ImageFile.LOAD_TRUNCATED_IMAGES = True
image = Image.open("00090.jpg")
# resize now doesn't fail
image.resize((h, w), Image.BICUBIC)

Related

(-215:Assertion failed) corr.rows <= img.rows + templ.rows - 1 && corr.cols <= img.cols + templ.cols - 1 in function 'cv::crossCorr' (matchTemplate)

I've run into issues with win32gui when trying to grab a real-time video stream of an application. I've seen I can use ImageGrab from PIL and based on this video Computer Screen Recording using Python & OpenCV I think I can use it instead of win32gui
I'm trying to learn python by writing a bot, the below code should grab images from a specified folder, load them into an array, converts them into a format OpenCV can use and then attempts to find any or all of them on my application window haystack
I can't find any details on google of the error I'm getting:
C:\Users\coyle\OneDrive\froggy-pirate-master\avoidShips>C:/Users/coyle/AppData/Local/Programs/Python/Python39/python.exe c:/Users/coyle/OneDrive/froggy-pirate-master/avoidShips/avoidships4.py
Traceback (most recent call last):
File "c:\Users\coyle\OneDrive\froggy-pirate-master\avoidShips\avoidships4.py", line 41, in <module>
loadImages()
File "c:\Users\coyle\OneDrive\froggy-pirate-master\avoidShips\avoidships4.py", line 22, in loadImages
return matchTemplate(image_list)
File "c:\Users\coyle\OneDrive\froggy-pirate-master\avoidShips\avoidships4.py", line 32, in matchTemplate
result = cv.matchTemplate(haystack, needle_img, cv.TM_CCOEFF_NORMED)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\templmatch.cpp:588: error: (-215:Assertion failed) corr.rows <= img.rows + templ.rows - 1 && corr.cols <= img.cols + templ.cols - 1 in function 'cv::crossCorr'
And my code:
def loadImages():
# Intialise empty array
image_list = []
# Get list of all images in directory
directory = glob.glob(r"C:\Users\*.png")
# Add images to image_list
for img in directory:
ship_img = cv.imread(img, 0)
image_list.append(ship_img)
return matchTemplate(image_list)
def matchTemplate(image_list):
# Video Loop
while True:
haystack_img = ImageGrab.grab()
haystack_img_np = np.array(haystack_img)
haystack = cv.cvtColor(haystack_img_np, cv.COLOR_BGR2GRAY)
# Object Detection
for ships in image_list:
needle_img = cv.imread(str(image_list), cv.IMREAD_UNCHANGED)
result = cv.matchTemplate(haystack, needle_img, cv.TM_CCOEFF_NORMED)
cv.imshow('Result', haystack)
if cv.waitKey(1) == 27:
break
cv.destroyAllWindows()
loadImages()
matchTemplate()
As a test, I've tried doing the same thing using static images and it works so I'm not sure where I'm going wrong.
import cv2 as cv
import glob
# load source images
directory = glob.glob(r'C:\Users\*.jpg')
# empty list to store the source images
image_list = []
for img in directory:
ships_img = cv.imread(img, 0)
image_list.append(ships_img)
haystack_img = cv.imread(r'C:\Users\both.jpg')
haystack_img = cv.cvtColor(haystack_img, cv.COLOR_BGR2GRAY)
#loop for matching
for ships in image_list:
#save the dimensions of the needle images
(H, W) = ships.shape[:2]
result = cv.matchTemplate(haystack_img, ships, cv.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
top_left = max_loc
bottom_right = (top_left[0] + W, top_left[1] + H)
cv.rectangle(haystack_img, top_left, bottom_right, 255, 2)
cv.imshow('Result', haystack_img)
cv.waitKey(0)
I can't test it but you simply try to load image which you already have in memory
You have
needle_img = cv.imread(str(image_list), cv.IMREAD_UNCHANGED)
but image_list has already loaded image, not filenames.
Besides imread() needs sinlge filename but you try use it with some list converted to string.
You should use directly
needle_img = ships
I think it should be
def loadImages():
# Intialise empty array
image_list = []
# Get list of all images in directory
directory = glob.glob(r"C:\Users\*.png")
# Add images to image_list
for img in directory:
ship_img = cv.imread(img, 0) # <-- here you load all images
image_list.append(ship_img)
return image_list # I preferr to send back data instead of running `matchTemplate`
def matchTemplate(image_list):
# Video Loop
while True:
haystack_img = ImageGrab.grab()
haystack_img_np = np.array(haystack_img)
haystack = cv.cvtColor(haystack_img_np, cv.COLOR_BGR2GRAY)
# Object Detection
for ships in image_list:
# you don't have to load images because you already have them in `image_list`
#needle_img = cv.imread(str(image_list), cv.IMREAD_UNCHANGED)
needle_img = ships
result = cv.matchTemplate(haystack, needle_img, cv.TM_CCOEFF_NORMED)
cv.imshow('Result', haystack)
if cv.waitKey(1) == 27:
break
cv.destroyAllWindows()
# --- main ---
image_list = loadImages()
matchTemplate(image_list)
BTW:
In normal open(), read() you get error if it has problem to open or read file but in OpenCV imread() does't raise error when it can't load image but it gives None - but you don't check if you get None - and you don't know that there was problem to load it - and later when you try to use this value in next command (matchTemplate) then it shows error. But real problem was with imread()

Python Open TIF Failure Multiple Libraries

I am having some problems opening some .tif files that I have. I've tried using pillow, gdal, cv2, and skimage. I would prefer to use pillow since I do use it in other scripts. I cannot use rasterio becuase when I set up a batch file rasterio throws an gdal._version error that I have yet to solve.
This opening image error is strange to me since I had another code that split raster images (had a missing data issue so I switched to this one) that uses gdal and has no problem opening it. I read a few posts about how pillow does not support certain data types, but I have not any work arounds yet. I've attached the properties of my image and will continue to trouble shoot.
Is there anything that pops out that I need to fix? Again I would prefer to use the first code block I posted (using pillow).
Pillow
import os
from PIL import Image
from itertools import product
def tile(filename, dir_in, dir_out, d):
Image.MAX_IMAGE_PIXELS = None
name, ext = os.path.splitext(filename)
img = Image.open(os.path.join(dir_in, filename))
w, h = img.size
grid = list(product(range(0, h - h % d, d), range(0, w - w % d, d)))
for i, j in grid:
box = (j, i, j + d, i + d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img.crop(box).save(out)
tile('name.tif',
r'D:\image',
r'D:\images_split',
1000)
Traceback (most recent call last):
File "C:/Users/delete_NA/split.py", line 22, in <module>
1000)
File "C:/Users/delete_NA/split.py", line 9, in tile
img = Image.open(os.path.join(dir_in, filename))
File "C:\Users\anaconda3\envs\split\lib\site-packages\PIL\Image.py", line 2959, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file 'D:\\image\\image_to_split.tif'
GDAL
import os
import gdal
from itertools import product
def tile(filename, dir_in, dir_out, d):
Image.MAX_IMAGE_PIXELS = None
name, ext = os.path.splitext(filename)
img = gdal.Open(os.path.join(dir_in, filename))
w, h = img.size
grid = list(product(range(0, h - h % d, d), range(0, w - w % d, d)))
for i, j in grid:
box = (j, i, j + d, i + d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img.crop(box).save(out)
tile('name.tif',
r'D:\image',
r'D:\images_split',
1000)
Traceback (most recent call last):
File "C:/Users/delete_NA/split.py", line 22, in <module>
1000)
File "C:/Users/delete_NA/split.py", line 10, in tile
w, h = img.size
File "C:\Users\anaconda3\envs\split\lib\site-packages\osgeo\gdal.py", line 2184, in <lambda>
__getattr__ = lambda self, name: _swig_getattr(self, Dataset, name)
File "C:\Users\anaconda3\envs\split\lib\site-packages\osgeo\gdal.py", line 80, in _swig_getattr
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
AttributeError: 'Dataset' object has no attribute 'size'
Other split code with gdal - this one works
import os
from osgeo import gdal
# import variables
# Setting the directory
os.chdir(r"D:\ortho")
# Loading in the image
rimg = gdal.Open("image.tif")
# Upper left corner of the minX and maxY
gt = rimg.GetGeoTransform()
xmin = gt[0]
ymax = gt[3]
res = gt[1]
xlen = res * rimg.RasterXSize # units is important UTM or WGS
ylen = res * rimg.RasterYSize
# how many tiles you want to have in each row and column
xdiv = 150
ydiv = 150
# Determining the size of each new image
xsize = xlen/xdiv
ysize = ylen/ydiv
print(xsize)
print(ysize)
xsteps = [xmin + xsize * i for i in range(xdiv+1)] # plut because we start in the left top corner where X is at its lowest
ysteps = [ymax - ysize * i for i in range(ydiv+1)] # minus because we start in the left top corner where Y is at its highest
for i in range(xdiv):
for j in range(ydiv):
xmin = xsteps[i]
xmax = xsteps[i+1]
ymax = ysteps[j]
ymin = ysteps[j+1]
# Splices the image up into the set divs and saves them.
gdal.Warp("D:/split_images/item" + str(i)+str(j) + ".tif",rimg,
outputBounds = (xmin,ymin,xmax,ymax),dstNodata = -9999)
Tif Properties Images
Edits
I ran another image through that is a fraction of the size, with the same properties. Same CRS, Units, Data type, ect. I don't think it would be a size issue because I pass Image.MAX_IMAGE_PIXELS = None. One thing to note though, each image that is split, the CRS is not being assigned, which does cause an issue for me later on.
After some digging I found that Image.open() from pillow does not support BigTiffs. It also only supports specific byte sizes. tifffile can be used to open BigTiffs. I ended up changing a couple things in my code. First, using tifffile instead of pillow. Second, changing img.size to img.shape. Third, tifffile opens the image into a numpy array, so use the clip method instead. The code does work, though it is not being clipped. This answers my initial question though.
import os
import tifffile
from itertools import product
def tile(filename, dir_in, dir_out, d):
name, ext = os.path.splitext(filename)
img = tifffile.imread(os.path.join(dir_in, filename))
w = img.shape[0]
h = img.shape[1]
grid = list(product(range(0, h - h % d, d), range(0, w - w % d, d)))
for i, j in grid:
box = (j, i, j + d, i + d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img = img.clip(box)
img = img.astype('uint8')
tifffile.imsave(out, img)
tile('name.tif',
r'D:\image',
r'D:\images_split',
1000)

Why am I getting multiple files when I save a Wand Image?

I have the following code:
def resizeImg(img, width, height):
outerImg = img(width=width, height=height, background=Color("WHITE"))
outerImg.units='pixelsperinch'
outerImg.resolution = (300, 300)
outerImg.format = img.format.lower()
outerImg.composite_channel('undefined', img, 'over', int((width - img.width) / 2), int((height - img.height) / 2))
return outerImg
for file in os.listdir():
if os.path.splitext(file)[1] in ('.tif', '.jpeg', '.jpg'):
with Image() as img:
fname = ntpath.basename(file).split('.')[0] # BC154360-ANG
img.read(filename=file)
img.units='pixelsperinch'
img.resolution = (300, 300)
img.compression_quality = 99
img.format = 'jpg'
dim = max(img.width, img.height)
img.resize(height=dim, width=dim) # make image square
img.resize(height=1500, width=1500) # set image size to 1500x1500
img.units='pixelsperinch'
img.resolution = (72, 72)
img.save(filename = jpg_location + fname + '.jpg') # .jpg
The issue is that I am getting multiple files when I run this, and they're all slightly different. It seems as if each image has been processed differently. The "-0" image appears to be correct, as does the result when I run display(img).
Can someone explain what is going on here? Perhaps it is the result of the channel and operator parameters in the composite_channel function?

Python Opencv images

def Read_img(path):
img = cv2.imread(path)
(h, w) = img.shape[:2]
WIDTH = 500
RATIO = WIDTH / float(w)
HEIGHT = int(h * RATIO) + 50
return cv2.resize(img, (WIDTH, HEIGHT))
for names in os.listdir(known_faces_dir):
print(names)
for file_name in os.listdir(f'{known_faces_dir}/{names}'):
print(file_name)
image = Read_img(f'{known_faces_dir}/{names}')
Here is the code my Read_img function is the problem it returns an error when i run the image variable
here is the error
File "Facial_Rec.py", line 23, in <module>
image = Read_img(f'{known_faces_dir}/{names}')
File "Facial_Rec.py", line 12, in Read_img`enter code here`
(h, w) = img.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
PLEASE HELP!
when you try to open an image but the path is not correct, OpenCV does not tell you that it could not find the image, it returns None. So when you want to check the shape you get an error
if the type is None just continue and pass to the next loop
if type(img) is None:
continue

Python error after loading .pkl file "ValueError: Did not recognise loaded array layout"

The code below is used for the training process of isolation forest in order to create a .pkl file (You can see the link here scikit-learn.org/stable/modules/generated/…). After generating the .pkl file, I am to load it from ubuntu to raspbian OS. However, I encountered this error "ValueError: Did not recognise loaded array layout". Can anyone help me with this?
Complete error:
Traceback (most recent call last):
File "oneclass_test.py", line 24, in
 clf_one,stdSlr,voc,k = joblib.load('oneclass.pkl')
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 575, in
 load obj = _unpickle(fobj, filename, mmap_mode)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 507, in
 _unpickle obj = unpickler.load()
File "/usr/lib/python2.7/pickle.py", line 858, in
 load dispatchkey
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 327, in
 load_build Unpickler.load_build(self)
File "/usr/lib/python2.7/pickle.py", line 1217, in
 load_build setstate(state)
File "sklearn/tree/_tree.pyx", line 650, in
 sklearn.tree._tree.Tree.setstate (sklearn/tree/_tree.c:8406)
ValueError: Did not recognise loaded array layout
oneclass_train.py:
#!/usr/local/bin/python2.7
import argparse as ap
# Importing library that supports user friendly commandline interfaces
import cv2
# Importing the opencv library
import imutils
# Importing the library that supports basic image processing functions
import numpy as np
# Importing the array operations library for python
import os
# Importing the library which supports standard systems commands
from scipy.cluster.vq import *
# Importing the library which classifies set of observations into clusters
from sklearn.externals import joblib
from sklearn.svm import OneClassSVM
from sklearn.neighbors import KNeighborsClassifier
clf_one,stdSlr, voc,k = joblib.load("oneclass.pkl")
# Get the path of the testing set
parser = ap.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-t", "--testingSet", help="Path to testing Set")
group.add_argument("-i", "--image", help="Path to image")
parser.add_argument('-v',"--visualize", action='store_true')
args = vars(parser.parse_args())
# Get the path of the testing image(s) and store them in a list
image_paths = []
if args["testingSet"]:
test_path = args["testingSet"]
try:
testing_names = os.listdir(test_path)
except OSError:
print "No such directory {}\nCheck if the file exists".format(test_path)
exit()
for testing_name in testing_names:
dir = os.path.join(test_path, testing_name)
class_path = imutils.imlist(dir)
image_paths+=class_path
else:
image_paths = [args["image"]]
# Create feature extraction and keypoint detector objects
fea_det = cv2.xfeatures2d.SIFT_create()
des_ext = cv2.xfeatures2d.SIFT_create()
# List where all the descriptors are stored
des_list = []
for image_path in image_paths:
im = cv2.imread(image_path)
r = 960.0 / im.shape[1]
dim = (960, int(im.shape[0]*r))
im = cv2.resize(im, dim, interpolation = cv2.INTER_AREA)
if im == None:
print "No such file {}\nCheck if the file exists".format(image_path)
exit()
img=im
img2=im
s = 75
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (s,s,im.shape[1]-(2*s),im.shape[0]-(2*s)) cv2.grabCut(img,mask,rect,bgdModel,fgdModel,1,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
im = img*mask2[:,:,np.newaxis]
cv2.imwrite(image_path + "_Segment.jpg" ,im)
print im.shape
cv2.namedWindow("segmentation", cv2.WINDOW_NORMAL)
pt = (0, 3 * im.shape[0] // 4)
cv2.putText(im, "segmentation", pt ,cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 3, [0, 255, 0], 5)
cv2.imshow("segmentation", im)
cv2.waitKey(2000)
kpts = fea_det.detect(im) # Computing the key points of test image
kpts, des = des_ext.compute(im, kpts) # Computing the descriptors of the test image
des_list.append((image_path, des)) # Appending the descriptors to a single list
# Stack all the descriptors vertically in a numpy array
descriptors = des_list[0][1]
for image_path, descriptor in des_list[0:]:
descriptors = np.vstack((descriptors, descriptor)) # Stacking the descriptors in to a numpy array
# Computing the histogram of features
test_features = np.zeros((len(image_paths), k), "float32")
for i in xrange(len(image_paths)):
words, distance = vq(des_list[i][1],voc)
for w in words:
test_features[i][w] += 1 # Calculating the histogram of features
# Perform Tf-Idf vectorization
nbr_occurences = np.sum( (test_features > 0) * 1, axis = 0) # Getting the number of occurrences of each word
idf = np.array(np.log((1.0*len(image_paths)+1) / (1.0*nbr_occurences + 1)), 'float32')
# Assigning weight to one that is occurring more frequently
test_features = stdSlr.transform(test_features)
predictions = []
confidences = []
predictions = []
pred = clf_one.predict(test_features)
print clf_one.predict(test_features)
for i in pred:
if i == 1:
predictions += ["PPB"]
if i == -1:
predictions += ["NOT PPB"]
a=0
# Visualize the results, if "visualize" flag set to true by the user
if args["visualize"]:
for image_path, prediction in zip(image_paths, predictions):
image = cv2.imread(image_path)
cv2.namedWindow(str(image_path), cv2.WINDOW_NORMAL)
pt = (0, 3 * image.shape[0] // 4)
cv2.putText(image, prediction , pt ,cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 5, [0, 255, 0], 5)
cv2.imshow(str(image_path), image)
cv2.imwrite(image_path + "_oneclass_Result.jpg" ,image)
cv2.waitKey(3000)
cv2.destroyAllWindows()
a= a + 1
try matching the versions of the libraries used in both raspbian and ubuntu

Categories

Resources