file can't be found when using relative path

Viewed 67

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

1 Answers

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

Related