PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.
MIT License
4.51k stars 1.03k forks source link

Example of using range-detector #50

Open iamtodor opened 6 years ago

iamtodor commented 6 years ago

Hello Adrian,

Thanks for your library. Could you please provide the example how to use range detector? Suppose I have the image with the specific object I want to be detected. Should I make the screenshot with the only that object present in the image? How exactly can I run it? I have tried to import it and find function range_detector() but it doesn't exist. I have tried to run it with the command ./range-detector -f HSV -i /path/to/image/image.jpg but then I got error

computer-vision $ ./imutils/bin/range-detector -h
Traceback (most recent call last):
  File "./imutils/bin/range-detector", line 10, in <module>
    import cv2
ImportError: No module named cv2

I have cv2 libraby and can use it via jupyter notebook as well. Could you please help me please with this issue?

jrosebr1 commented 6 years ago

It looks like OpenCV is not installed on your system or you used a Python virtual environment when installing it. Can you double-check that OpenCV is installed on your system?

Additionally, i'll be rewriting the entire range_detector script (it's a bit hard to use now) and then writing a dedicated blog post on it soon.

iamtodor commented 6 years ago

Thank you @jrosebr1 for the fast reply. Yes, I'm pretty sure I have installed this module since I have passed a lot of your articles and https://www.udemy.com/master-computer-vision-with-opencv-in-python/ I can manipulate with images via opencv as well. I have 3.4.0 version (probably latest)

jrosebr1 commented 6 years ago

Can you confirm that you can access OpenCV from your Python shell? As it stands now, you shouldn't be able to. The cv2 import error isn't specific to the range_detector script. Whenever cv2 doesn't import it's because the library isn't installed properly or you are using a Python virtual environment or Anaconda. Again, double-check your install of OpenCV on your system.

iamtodor commented 6 years ago

Here is the logs from the console:

eigenfish $ python3
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.0'

I have no anaconda istalled

jrosebr1 commented 6 years ago

You have OpenCV installed for python3, but it looks like range_detector is being executed for python2 (aliased as just python) which does not have OpenCV installed.

Try explicitly supplying your python3 binary:

$ python3 imutils/bin/range-detector -h

Update your path as needed.

iamtodor commented 6 years ago

I got another errors:

computer-vision $ python3 imutils/bin/range-detector -h
usage: range-detector [-h] -f FILTER [-i IMAGE] [-w] [-p]

optional arguments:
  -h, --help            show this help message and exit
  -f FILTER, --filter FILTER
                        Range filter. RGB or HSV
  -i IMAGE, --image IMAGE
                        Path to the image
  -w, --webcam          Use webcam
  -p, --preview         Show a preview of the image after applying the mask

I was trying to pass image as well with the allias --image path/to/image.jpg but it didnt work for me

jrosebr1 commented 6 years ago

You aren't getting an error. The -h flag indicates that the command line arguments help is being printed to your console. If you're new to command line arguments, that's okay, but take the time to read up on how they work first. I recommend this tutorial to help you get started.

As I mentioned, I will be entirely rewriting this script and making it easier to use. I'll also include a detailed blog post on how to use it. But until then I can only provide limited support for it. I hope you understand.

iamtodor commented 6 years ago

I run python3 imutils/bin/range-detector -f HSV -i orange.jpg and got some interesting message:

On Mac OS X, you might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
QObject::moveToThread: Current thread (0x101e52cf0) is not the object's thread (0x11bc700e0).
Cannot move to target thread (0x101e52cf0)
jrosebr1 commented 6 years ago

My guess is that your compiled and installed OpenCV using the Qt GUI library rather than GTK but I'm not sure. It really depends on how you installed OpenCV.

iamtodor commented 6 years ago

uhhh I'm pretty sure I've installed OpenCV on my mac via brew :( I will check my success on Monday, on Ubuntu 16.04, OpenCV was compiled from the source with the support GTK and maybe Qt. I've compiled OpenCV with following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=OFF \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules \
      -D BUILD_EXAMPLES=ON \
      -D WITH_CUDA=OFF \
      -D BUILD_opencv_xfeatures2d=OFF \
      -D WITH_QT=ON \
      -D WITH_GTK=ON \
      -D WITH_OPENGL=ON \
      -D WITH_TBB=ON ..
jrosebr1 commented 6 years ago

It sounds like for whatever reason Qt is causing an issue. You may want to consider installing OpenCV without Qt and see if that resolves the problem.