-1

I am doing a vehicle license plate detection and recognition project. My code was working perfectly fine and then all of a sudden it stopped working. It seems as if opencv stopped working completely.

This is the error I am getting.

Traceback (most recent call last):
  File "c:\Users\user-pc\Desktop\Python Project VSCode\Code\main.py", line 5, in <module>
    cv2.imshow("real image", image)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1006: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I am just running a smaller code to get get opencv working. This is the code i am running.

import cv2

image = cv2.imread('T2.jpg')
cv2.imshow("real image", image)
cv2.waitKey(0)

I have tried uninstalling and reinstalling opencv. I have even tried in a different IDE but I get the same error. I dont know what to do.

Please help

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Your image is not being read, check if the path is right, you can do `if image is not None` before any tratment – Ivan Sep 08 '22 at 08:38
  • To be more precise, OpenCV is telling you that the image size is 0 and it cannot do anything with it. So likely the problem comes from the way you read your image. Where is it placed? can you use it's full path? – Ivan Sep 08 '22 at 08:39
  • Please print out the shape of `image` and share with us. – Markus Sep 08 '22 at 08:40
  • learn about **current working directory** and how relative paths are resolved – Christoph Rackwitz Sep 08 '22 at 11:42
  • the image is in the same file as the code. I have also tried using the path for the image but that gives the same error. @ivan – Storm Slater Sep 08 '22 at 15:39
  • 1
    `print(os.path.exists("T2.jpg"), repr(os.getcwd()), os.listdir(os.getcwd()))` and report back. and review [mre]. learn to debug your program and follow an issue to its root. – Christoph Rackwitz Sep 08 '22 at 16:15

1 Answers1

1

A workaround: Put images and code in the same directory.

For example, I create a new folder called test, I put a picture named T2.jpg in the folder, and then create a new test.py file.

enter image description here

Open this folder with vscode. The code runs without errors.

enter image description here



To answer your comment:

What folder are you opening in vscode? Is it the folder that contains main.py (Like the test folder in my answer), in your project this should be the Code folder.

If you open the Python Project VSCode folder in vscode, and then run the main.py (in Code folder), there will definitely be an error.

enter image description here

At this time, you need to specify the path in the code, like the following:

enter image description here

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • This is what I have done. the image is in the same folder as the python file. I have even tried entering the path manually but that gives the same error @PeterSmith – Storm Slater Sep 08 '22 at 15:28
  • https://imgur.com/rtm4M4R here is a link to a picture of the file – Storm Slater Sep 08 '22 at 15:37
  • I updated the answer, is it useful? – JialeDu Sep 09 '22 at 01:44
  • okay, I have done that and it is giving me a different error. File "c:\Users\user-pc\Desktop\Python Project VSCode\Code\main.py", line 5, in cv2.imshow("real image", image) cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1274: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage' – Storm Slater Sep 09 '22 at 06:51
  • Can you show your error interface? Or the entire interface of vscode. // Or try absolute paths. – JialeDu Sep 09 '22 at 06:59
  • do you want me to screen shot the entire window? – Storm Slater Sep 09 '22 at 07:01
  • https://imgur.com/a/vIdjrGT – Storm Slater Sep 09 '22 at 07:02
  • It is recommended that you reinstall the package to see if that resolves the error. `pip uninstall opencv-python ` and `pip install opencv-python`. This [link](https://stackoverflow.com/questions/67120450/error-2unspecified-error-the-function-is-not-implemented-rebuild-the-libra) should help you. – JialeDu Sep 09 '22 at 07:34
  • I tried doing that and it gives the same error. And i just want to say, i really appreciate your help and patience. Have you got any other sollutions to this problem – Storm Slater Sep 09 '22 at 10:21
  • I'm sorry for not being able to help. Finally, if you can successfully run the code in an external terminal or other IDE, I recommend filing an [issue](https://github.com/microsoft/vscode-python/issues/new/choose) on GitHub. – JialeDu Sep 13 '22 at 02:23