0

I already verified my Python and openCV library definitely work. let just say a simple cv2.imshow() works normally when used with IDLE. but when I tried to use another IDE like Visual Studio and Pyscripter using the same code, the IDE cannot show the image and turns error. but when i tried to print(cv2.version), it works just ok. which means my IDE able to use the openCV library just fine. my pyscripter also gives the same output as VS. I already disabled any anti virus. why only IDLE works?

import cv2

print(cv2.__version__)

img = cv2.imread('cat.jpg')

cv2.imshow('img', img)`

IDLE vs VS running same code

python (64 bit): 3.7.5 opencv(pip): 4.1.1 Visual Studio: 2019 16.3.8

update: i found this error:

cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

so i added c++ module in my VS and i add this line to the code:

cv2.waitKey(0)
cv2.destroyAllWindows()

it works but a bit lag compared to IDLE, can anyone explain me why?

red
  • 1
  • 4
  • Can you add some more information to your question please. Can you tell us what code you are running in the other IDEs vs IDLE and what error you are getting. – csoham Nov 09 '19 at 08:59
  • code: `import cv2 print(cv2.__version__) img = cv2.imread('cat.jpg') cv2.imshow('img', img)` [comparison IDLE vs VS](https://imgur.com/P1EVf7S) [using anaconda](https://imgur.com/N7vGqZe) [using python](https://imgur.com/oOWIQAN) [how I setup my system](https://www.youtube.com/watch?v=d3AT9EGp4iw) I do not use vscode because cv2 has conflict with pylint, so I use VS – red Nov 09 '19 at 11:46

1 Answers1

0

What kind of error are you getting on the other IDE's? If you do print(img) do you get None on the other IDE's?

Also, if you provide a pip list, I can see if I can reproduce your error with the packages you have currently installed.

Have you tried using a virtual environment for python (virtualenv, Anaconda, or Miniconda)? It's a nice way to use different versions of python and your packages without conflicting with system python.

I set up a new environment with packages on Anaconda conda create -n test python=3.7 opencv jupyter and ran the commands in opencv like you mentioned. It worked in the following IDE's that I have installed:

  • Visual Studio 2019
  • Visual Studio Code
  • JetBrains PyCharm
  • GNU emacs
  • Jupyter Notebooks (IPython) (not really an IDE)

For each IDE, you will have to point them to the python interpreter that was set up using your virtual environment (I can provide more detailed instructions). I suspect what may be happening is python is set up differently than your other IDE's. import sys; print(sys.path) might tell you if this is the case. Or maybe the backend is different - point is, there are too many variables and it might be best to create a virtual environment to see if you get the same error.

EDIT: I made a Jupyter Notebook you can use for testing. If you'd like to see it run, you can click on Binder on the top right (or start in Colab). Point is, if you downloaded this notebook and ran it in your Python environment it should work (I've included conda setup instructions in the notebook). NBViewer, GitHub Repository

mikkeyboi
  • 26
  • 2
  • i install everything pretty much like this except using VS [how to install opencv python windows](https://www.youtube.com/watch?v=d3AT9EGp4iw) [my pip list](https://imgur.com/8gKqpdC) [using anaconda](https://imgur.com/N7vGqZe) [using python](https://imgur.com/oOWIQAN) @csoham – red Nov 09 '19 at 10:58
  • i found this error: `cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'`. weird because im using python – red Nov 09 '19 at 18:09
  • I don't think it's finding your picture (width and height are 0), try typing the full path to the picture and run it again. Are you running your script or using the integrated terminal? It also seems like you have different sources of opencv (check your PATH). Can you run everything in Anaconda Prompt (make sure you activate your environment before typing `python`) and see if you get the same error? In Visual Studio did you point your python interpreter to the one in conda? – mikkeyboi Nov 10 '19 at 01:25
  • hello, i did try your suggestion but the output still the same. but i found this [1](https://stackoverflow.com/questions/31586385/assertion-failure-size-width0-size-height0-in-function-imshow?noredirect=1). i think my problem is the same. however i still do not find any solution from the thread. – red Nov 10 '19 at 04:58
  • i added cv2.waitKey(0) and it works on VS but in IDLE i dont need these lines, why? – red Nov 10 '19 at 06:24
  • I edited the main post with a repository I made for you to test. You may need jupyter (`pip install jupyter` or `conda install jupyter`) to run the notebook. You can see I reproduced your error at the end of the notebook. The reason why `cv2.waitKey(0)` works is because it is displaying a frame indefinitely (None becomes replaced with an empty frame, error goes away). – mikkeyboi Nov 10 '19 at 10:28