1

I'm using VS Code for Python (anaconda package and OpenCV).

I'm debugging the program (pressing F5) and I'm getting the error below.

it says File not found/ but the file is there when I run via terminal

code I'm getting the error with

import cv2 as cv
import os
import numpy as np

pathsyn = os.path.realpath("synset_words.txt")

print("path", pathsyn)

syn = open(pathsyn,"r").read().strip().split("\n")

syncls = [r[r.find(" ") +1:] for r in syn]

vid = cv.VideoCapture(0)

net = cv.dnn.readNetFromCaffe("Classifiers/model/bvlc_googlenet.prototxt","Classifiers/model/bvlc_googlenet.caffemodel")

though the both synset_words.txt,AI02.py files are in the same directory

both are in the same directory

if I modify the code to

pathsyn = os.path.realpath("OpenCV/synset_words.txt")

then it works.

import cv2 as cv
import os
import numpy as np

pathsyn = os.path.realpath("OpenCV/synset_words.txt")

print("path", pathsyn)

syn = open(pathsyn,"r").read().strip().split("\n")

syncls = [r[r.find(" ") +1:] for r in syn]

vid = cv.VideoCapture(0)

net = cv.dnn.readNetFromCaffe("OpenCV/Classifiers/model/bvlc_googlenet.prototxt","OpenCV/Classifiers/model/bvlc_googlenet.caffemodel")

Directories; .vscode is outside of the OpenCV folder

this is because .vscode the folder is in the previous directory and my AI02.py and synset_words.txt is in OpenCV folder.

Directory where VS Code is finding the file (where the .vscode folder also):

C:\Users\user\Documents\Python Scripts

but the file is in (where vs code should direct debugger like terminal):

C:\Users\user\Documents\Python Scripts\OpenCV
Andy K
  • 4,944
  • 10
  • 53
  • 82
Haseeb Ayub
  • 25
  • 3
  • 10

2 Answers2

7

In the launch.config change the CWD to the folder OR just use the command mentioned below:

{
    "version": "0.2.0",
    "configurations": [
        {
            ....,
            "cwd" : "${workspaceFolder}/${relativeFileDirname}"
        }
    ]
}
J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38
JASIM AHMED
  • 81
  • 1
  • 7
1

Try setting the current working directory to the one you need using os.chdir(path) before pathsyn = ... – Hamza Hathoute 4 hours ago

@HamzaHathoute that may work, but this would be a temporary solution. I want to set VS Code debugger to c:\Users\user\Documents\Python Scripts\OpenCV as my default directory instead of c:\Users\user\Documents\Python Scripts – Haseeb Ayub 3 hours ago

Did you try stackoverflow.com/q/38623138/9406615 ? – Hamza Hathoute 3 hours ago

Adding "cwd": "${workspaceFolder}/opencv" to my launch.json of VS Code solved the problem.

I would like say THANK YOU to @Hamza Hathoute for the solution. THANK YOU @Andy K and @jonrsharpe for suggesting edits.

Community
  • 1
  • 1
Haseeb Ayub
  • 25
  • 3
  • 10