Each week I receive and respond to at least 2-3 emails and 3-4 blog post comments regarding
NoneTypeerrors in OpenCV and Python.
For beginners, these errors can be hard to diagnose — by definition they aren’t very informative.
Since this question is getting asked so often I decided to dedicate an entire blog post to the topic.
While
NoneTypeerrors can be caused for a nearly unlimited number of reasons, in my experience, both as a computer vision developer and chatting with other programmers here on PyImageSearch, in over 95% of the cases,
NoneTypeerrors in OpenCV are caused by either:
- An invalid image path passed to
cv2.imread
. - A problem reading a frame from a video stream/video file via
cv2.VideoCapture
and the associated.read
method.
To learn more about
NoneTypeerrors in OpenCV (and how to avoid them), just keep reading.
Looking for the source code to this post?
Jump right to the downloads section.
OpenCV: Resolving NoneType errors
In the first part of this blog post I’ll discuss exactly what
NoneTypeerrors are in the Python programming language.
I’ll then discuss the two primary reasons you’ll run into
NoneTypeerrors when using OpenCV and Python together.
Finally, I’ll put together an actual example that not only causes a
NoneTypeerror, but also resolves it as well.
What is a NoneType error?
When using the Python programming language you’ll inevitably run into an error that looks like this:
AttributeError: 'NoneType' object has no attribute ‘something’
Where
somethingcan be replaced by whatever the name of the actual attribute is.
We see these errors when we think we are working with an instance of a particular Class or Object, but in reality we have the Python built-in type
None.
As the name suggests,
Nonerepresents the absence of a value, such as when a function call returns an unexpected result or fails entirely.
Here is an example of generating a
NoneTypeerror from the Python shell:
>>> foo = None >>> foo.bar = True Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'bar' >>>
Here I create a variable named
fooand set it to
None.
I then try to set the
barattribute of
footo
True, but since
foois a
NoneTypeobject, Python will not allow this — hence the error message.
Two reasons for 95% of OpenCV NoneType errors
When using OpenCV and Python bindings, you’re bound to come across
NoneTypeerrors at some point.
In my experience, over 95% of the time these
NoneTypeerrors can be traced back to either an issue with
cv2.imreador
cv2.VideoCapture.
I have provided details for each of the cases below.
Case #1: cv2.imread
If you are receiving a
NoneTypeerror and your code is calling
cv2.imread, then the likely cause of the error is an invalid file path supplied to
cv2.imread.
The
cv2.imreadfunction does not explicitly throw an error message if you give it an invalid file path (i.e., a path to a nonexistent file). Instead,
cv2.imreadwill simply return
None.
Anytime you try to access an attribute of a
Noneimage loaded from disk via
cv2.imreadyou’ll get a
NoneTypeerror.
Here is an example of trying to load a nonexistent image from disk:
$ python >>> import cv2 >>> path = "path/to/image/that/does/not/exist.png" >>> image = cv2.imread(path) >>> print(image.shape) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'shape'
As you can see,
cv2.imreadgladly accepts the image path (even though it doesn’t exist), realizes the image path is invalid, and returns
None. This is especially confusing for Python programmers who are used to these types of functions throwing exceptions.
As an added bonus, I’ll also mention the
AssertionFailedexception.
If you try to pass an invalid image (i.e.,
NoneTypeimage) into another OpenCV function, Python + OpenCV will complain that the image doesn’t have any width, height, or depth information — and how could it, the “image” is a
Noneobject after all!
Here is an example of an error message you might see when loading a nonexistent image from disk and followed by immediately calling an OpenCV function on it:
>>> import cv2 >>> path = "path/to/image/that/does/not/exist.png" >>> image = cv2.imread(path) >>> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /tmp/opencv20150906-42178-3d0iam/opencv-2.4.12/modules/imgproc/src/color.cpp, line 3739 Traceback (most recent call last): File "<stdin>", line 1, in <module> cv2.error: /tmp/opencv20150906-42178-3d0iam/opencv-2.4.12/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor >>>
These types of errors can be harder to debug since there are many reasons why an
AssertionErrorcould be thrown. But in most cases, your first step should be be ensuring that your image was correctly loaded from disk.
A final, more rare problem you may encounter with
cv2.imreadis that your image does exist on disk, but you didn’t compile OpenCV with the given image I/O libraries installed.
For example, let’s say you have a .JPEG file on disk and you knew you had the correct path to it.
You then try to load the JPEG file via
cv2.imreadand notice a
NoneTypeor
AssertionError.
How can this be?
The file exists!
In this case, you likely forgot to compile OpenCV with JPEG file support enabled.
In Debian/Ubuntu systems, this is caused by a lack of
libjpegbeing installed.
For macOS systems, you likely forgot to install the
jpeglibrary via Homebrew.
To resolve this problem, regardless of operating system, you’ll need to re-compile and re-install OpenCV. Please see this page for more details on how to compile and install OpenCV on your particular system.
Case #2: cv2.VideoCapture and .read
Just like we see
NoneTypeerrors and
AssertionErrorexceptions when using
cv2.imread, you’ll also see these errors when working with video streams/video files as well.
To access a video stream, OpenCV uses the
cv2.VideoCapturewhich accepts a single argument, either:
- A string representing the path to a video file on disk.
- An integer representing the index of a webcam on your computer.
Working with video streams and video files with OpenCV is more complex than simply loading an image via
cv2.imread, but the same rules apply.
If you try to call the
.readmethod of an instantiated
cv2.VideoCapture(regardless if it’s a video file or webcam stream) and notice a
NoneTypeerror or
AssertionError, then you likely have a problem with either:
- The path to your input video file (it’s probably incorrect).
- Not having the proper video codecs installed, in which case you’ll need to install the codecs, followed by re-compiling and re-installing OpenCV (see this page for a complete list of tutorials).
- Your webcam not being accessible via OpenCV. This could be for any number of reasons, including missing drivers, an incorrect index passed to
cv2.VideoCapture
, or simply your webcam is not properly attached to your system.
Again, working with video files is more complex than working with simple image files, so make sure you’re systematic in resolving the issue.
First, try to access your webcam via a separate piece of software than OpenCV.
Or, try to load your video file in a movie player.
If both of those work, you likely have a problem with your OpenCV install.
Otherwise, it’s most likely a codec or driver issue.
An example of creating and resolving an OpenCV NoneType error
To demonstrate a
NoneTypeerror in action I decided to create a highly simplified Python + OpenCV script that represents what you might see elsewhere on the PyImageSearch blog.
Open up a new file, name it
display_image.py, and insert the following code:
# import the necessary packages import argparse import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to the image file") args = vars(ap.parse_args()) # load the image from disk and display the width, height, # and depth image = cv2.imread(args["image"]) (h, w, d) = image.shape print("w: {}, h: {}, d: {}".format(w, h, d)) # show the image cv2.imshow("Image", image) cv2.waitKey(0)
All this script does is:
- Parse command line arguments.
- (Attempts to) load an image from disk.
- Prints the width, height, and depth of the image to the terminal.
- Displays the image to our screen.
For most Python developers who are familiar with the command line, this script won’t give you any trouble.
But if you’re new to the command line and are unfamiliar/uncomfortable with command line arguments, you can easily run into a
NoneTypeerror if you’re not careful.
How, you might say?
The answer lies in not properly using/understanding command line arguments.
Over the past few years of running this blog, I’ve seen many emails and blog post comments from readers who are trying to modify the
.add_argumentfunction to supply the path to their image file.
DON’T DO THIS — you don’t have to change a single line of argument parsing code.
Instead, what you should do is spend the next 10 minutes reading through this excellent article that explains what command line arguments are and how to use them in Python:
This is required reading if you expect to follow tutorials here on the PyImageSearch blog.
Working with the command line, and therefore command line arguments, are a big part of what it means to be a computer scientist — a lack of command line skills is only going to harm you. You’ll thank me later.
Going back to the example, let’s check the contents of my local directory:
$ ls -l total 800 -rw-r--r-- 1 adrianrosebrock staff 541 Dec 21 08:45 display_image.py -rw-r--r-- 1 adrianrosebrock staff 403494 Dec 21 08:45 jemma.png
As we can see, I have two files:
-
display_image.py
: My Python script that I’ll be executing shortly. -
jemma.png
: The photo I’ll be loading from disk.
If I execute the following command I’ll see the
jemma.pngimage displayed to my screen, along with information on the dimensions of the image:
$ python display_image.py --image jemma.png w: 376, h: 500, d: 3
However, let’s try to load an image path that does not exist:
$ python display_image.py --image i_dont_exist.png Traceback (most recent call last): File "display_image.py", line 17, in <module> (h, w, d) = image.shape AttributeError: 'NoneType' object has no attribute 'shape'
Sure enough, there is our
NoneTypeerror.
In this case, it was caused because I did not supply a valid image path to
cv2.imread.
Summary
In this blog post I discussed
NoneTypeerrors and
AssertionErrorexceptions in OpenCV and Python.
In the vast majority of these situations, these errors can be attributed to either the
cv2.imreador
cv2.VideoCapturemethods.
Whenever you encounter one of these errors, make sure you can load your image/read your frame before continuing. In over 95% of circumstances, your image/frame was not properly read.
Otherwise, if you are using command line arguments and are unfamiliar with them, there is a chance that you aren’t using them properly. In that case, make sure you educate yourself by reading this tutorial on command line arguments — you’ll thank me later.
Anyway, I hope this tutorial has helped you in your journey to OpenCV mastery!
If you’re just getting started studying computer vision and OpenCV, I would highly encourage you to take a look at my book, Practical Python and OpenCV, which will help you grasp the fundamentals.
Otherwise, make sure you enter your email address in the form below to be notified when future blog posts and tutorials are published!
Downloads:
The post OpenCV: Resolving NoneType errors appeared first on PyImageSearch.
from PyImageSearch http://ift.tt/2hr35us
via IFTTT
No comments:
Post a Comment