Home > Software engineering >  Can't open/read file error / 'NoneType' object has no attribute 'shape'
Can't open/read file error / 'NoneType' object has no attribute 'shape'

Time:06-15

I'm trying to run my Python code to open file in JPG extension (csv file consists only file name, without extension). Here's a part of it:

//config file
BASE_PATH = "dataset"
IMAGES_PATH = os.path.sep.join([BASE_PATH, "images"])
ANNOTS_PATH = os.path.sep.join([BASE_PATH, "bboxes.csv"])

//main file
rows = open(config.ANNOTS_PATH).read().strip().split("\n")
data = []
targets = []
filenames = []

for row in rows: 
    row = row.split(' ')
    (filename, startX, startY, endX, endY) = row
    suffix = ".jpg"
    imagePath = os.path.sep.join([config.IMAGES_PATH, filename suffix])
    image = cv2.imread(imagePath)
    (h, w) = image.shape[:2]

But get the next error:

> [ WARN:[email protected]] global
> /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp
> (239) findDecoder imread_('dataset/images/"2007_000027.jpg'): can't
> open/read file: check file path/integrity Traceback (most recent call
> last):   File "/Users/username/Downloads/lr2_a/train.py", line 30, in
> <module>
>     (h, w) = image.shape[:2] AttributeError: 'NoneType' object has no attribute 'shape'

Please help me solve this problem

CodePudding user response:

I suspect that you may have a leading double quote at the beginning of the row with 2007_000027, as it is found here: ('dataset/images/ --> " <-- 2007_000027.jpg').

Strip that quote, with:

rows = open(config.ANNOTS_PATH).read().strip('\"').split("\n")

Btw, check if your csv file data is formatted correctly.

  • Related