3

cv2.imread("some.jpg") fails to read many different jpgs. I have checked a million different things:

  1. Run the same exact code in a terminal - reads and opens images just fine
  2. I checked version numbers of both python and cv2 - they are exactly the same:3.4.3 and 3.1.0.
  3. Used complete paths to files. No difference
  4. Images exist: I've manually opened dozens to see if there's something thing
  5. Timing: added pauses just to make sure this wasn't a timing issue.
  6. Checked to make sure the img/filename exists with print(os.path.exists(filename)) # prints True
  7. Hardcoded a file path into `img = imread("gif_pos_0pW-p-wb8U4_0.jpg") # print(img)... None
filename = random.choice(filename_list)
print("reading:", filename) # prints correct/verified jpg paths
sleep(.5)
img = cv2.imread(filename)
sleep(.3)
print(img) # none
read_image = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT), 3)

img is none and the resize line fails with: OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /home/user/opencv/modules/imgproc/src/imgwarp.cpp, line 3229

This is Ubuntu 15.1 if it matters. Any thoughts on what could be causing this?

Yes, I know this question exists elsewhere. The existing answers have not helped me. I have quadruple checked everything. There seems to be something else going on.

The weirdest part is that cv2 reads the image just fine from the command line, with the same exact python and cv2 versions.

EDIT: this is a script, so I'm just doing python3 train.py.

JohnAllen
  • 7,317
  • 9
  • 41
  • 65
  • Did you check `sys.executable` is the same for both? – user1767754 May 31 '16 at 18:27
  • no but i'll try it now. EDIT: it's the same... – JohnAllen May 31 '16 at 18:31
  • Are you saying that one image gets opened and the other does not? Or does no image get opened at all? – tfv May 31 '16 at 19:31
  • 1
    Have you checked with filenames without path in the same directory? What happens? Have you checked without random.choice, entering one single path directly? – tfv May 31 '16 at 19:33
  • No image gets opened at all. Yes I have placed a subset of the images in the scripts directory and changed the directory that gets read. That had no effect. I have hardcoded a single checked filepath string into `imread`, yes. – JohnAllen May 31 '16 at 19:35

1 Answers1

3

The script might be executed as a different user, with different privileges or at a different location than executing the code on the command line.

  • Check existence of file in the code with os.path.isfile
  • provide details on the script (language) and how you start it. Is this a cron job, or an executable file? Start the script manually from the command line and only after that works create batch jobs or call it from other scripts.
  • Can you give examples of paths?
  • Does it work trying to load one single image located in the same directory, without using random.choice?)
  • Does the code below work with the image attached? It works for me.

enter image description here

import cv2

img=cv2.imread("image.jpg")

cv2.imshow('Test',img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
tfv
  • 6,016
  • 4
  • 36
  • 67
  • Forgot to add that I did check that the images existed with something similar to y our `os.path.isfile`. Will add that and script details – JohnAllen May 31 '16 at 19:30
  • Have run the script from the same directory on the same images as I ran the interpreter from. Shouldn't that be the same? Pretty sure I made all of the images readable last night but will double check that. Maybe I didn't recursively make the images readable? – JohnAllen May 31 '16 at 19:41
  • In unix systems and with scripts I would *only* work with full paths. – tfv May 31 '16 at 19:45
  • Yes, your code works, as does basically a copy of it with one of my images. So the problem lies in the code before this code is executing which is totally perplexing. Thanks for the help. Will see if I can post some more code. – JohnAllen May 31 '16 at 19:47
  • 1
    Have a look here for finding where your code gets executed: http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory – tfv May 31 '16 at 19:50