OpenCV 2.3.1 Python with Eclipse shows synatx errors, but still works

I followed this tutorial to install OpenCV 2.3.1 in Python 2.7 using Eclipse.

I also copied the libraries to my python folder:

http://i.snag.gy/J9RrC.jpg

Here is my Hello World program that works correctly (creates a named window and displays an image), but Eclipse still shows syntax errors

Eclipse showing syntax errors

each error says "Undefined variable from import"

Here are my python settings for this project:

http://i.snag.gy/KBXiB.jpg http://i.snag.gy/KfTpF.jpg

Am I setting up my PythonPath incorrectly? How can I make Eclipse work correctly?

thanks

+6
source share
3 answers

I had the same problem, everything worked correctly, although there were import errors undefined all over the place. I eventually solved this by adding "cv" to the Forced Builtins list: Window> Preferences> Pydev> Interpreter - Python> Forced Builtins> New.

This is how I found the solution:

How to use code completion in Eclipse with OpenCV

I hope this helps too.

+4
source

EDIT: FYI, according to the best answer here , if you are just starting out (like me!), It is almost certainly better to use the older cv2 interface presented in cv2.cv. The author of this answer, Abid Rahman, has some lessons that look pretty good. (end of EDIT)

I used Debian tools to install the python-opencv package. There was no directory ... / dist -packages / opencv, and the cv.py file contained only:

from cv2.cv import * 

I am pretty inexperienced with Python and completely with Python accessing external libraries, so it looked like a kind of workaround related to this. Not so, apparently. I followed Casper's link above and found the solution that he used (which worked for me), but I was not happy with the use of “forced built-in functions” when I was not completely sure of the consequences.

However, a second, lower rating is my preferred solution. Instead

 import cv 

I use

 import cv2.cv as cv 

From what I can say, this just removes the cv.py broker from the import chain, if that makes sense. Saving / closing / reloading my script meant that Eclipse recognized cv.LoadImageM as specific and autoconfirmed other things from OpenCV.

I am reproducing this answer here because it seems to me cleaner, and I first found this question when I was looking for an answer to the same problem.

+2
source

It would be helpful to show the error you received and your code. However, I suspect that the problem is that the syntax errors that PyDev shows are based on his own code analysis, which is much more simplified than the actual python interpreter. If your code works, then undefined variables must be defined, but the PyDev parser simply cannot see them and tells them as "undefined".

The reason for this is that OpenCV does not explicitly define its variables in a way that PyDev can read. Unfortunately, I do not have a simple solution. I usually look at the problem using from ... import ... so that the error appears only once. If you want, you can write a wrapper module that explicitly imports the variables into the local namespace, and then imports the module.

0
source

Source: https://habr.com/ru/post/908671/


All Articles