FileNotFoundError:[Errno 2]没有这样的文件或目录:'Models\model_new.json'

问题描述 投票:0回答:1
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FileNotFoundError: [Errno 2] No such file or directory: 'Models\\model_new.json'[text]

这是我在下面的代码中遇到的错误。怎么解决呢。项目存储库的 Github 链接发布在底部。

class Application:

    def __init__(self):

        self.hs = Hunspell('en_US')
        self.vs = cv2.VideoCapture(0)
        self.current_image = None
        self.current_image2 = None
        self.json_file = open("Models\model_new.json", "r")
        self.model_json = self.json_file.read()
        self.json_file.close()

        self.loaded_model = model_from_json(self.model_json)
        self.loaded_model.load_weights("Models\model_new.h5")

        self.json_file_dru = open("Models\model-bw_dru.json" , "r")
        self.model_json_dru = self.json_file_dru.read()
        self.json_file_dru.close()

        self.loaded_model_dru = model_from_json(self.model_json_dru)
        self.loaded_model_dru.load_weights("Models\model-bw_dru.h5")
        self.json_file_tkdi = open("Models\model-bw_tkdi.json" , "r")
        self.model_json_tkdi = self.json_file_tkdi.read()
        self.json_file_tkdi.close()

        self.loaded_model_tkdi = model_from_json(self.model_json_tkdi)
        self.loaded_model_tkdi.load_weights("Models\model-bw_tkdi.h5")
        self.json_file_smn = open("Models\model-bw_smn.json" , "r")
        self.model_json_smn = self.json_file_smn.read()
        self.json_file_smn.close()

        self.loaded_model_smn = model_from_json(self.model_json_smn)
        self.loaded_model_smn.load_weights("Models\model-bw_smn.h5")

https://github.com/emnikhil/Sign-Language-To-Text-Conversion

这是我用于最后一年项目的项目,[如果您必须获取我已附加的我从中克隆项目的存储库的任何参考,则会出现此错误]

这个项目是关于使用 CNN 将美国手语转换为文本,并应用高斯模糊滤镜和灰度来减小图像的大小。我找不到问题的任何解决方案,因此我没有尝试或更改任何代码行并寻找问题的解决方案。

enter image description here

python machine-learning deep-learning computer-vision conv-neural-network
1个回答
0
投票

当您使用“\”而不是“/”时,您在路径中使用了转义字符

"Models\model-bw_dru.json" # This will throw an exception

应该是

"Models/model-bw_dru.json"

或者至少使用两个“\”

"Models\\model-bw_dru.json"

如果此文件位于“C:\somefolder\(Application.py 所在的文件夹)\Models\model-bw_dru.json”之类的位置,那么这可能适合您。我个人更喜欢这种路径方法,因为我发现它可靠并且可以与 pyinstaller 一起使用(以防万一):

import os
import sys
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)

my_json_file = application_path + "Models/model-bw_dru.json"
self.json_file_dru = open(json_file , "r")
© www.soinside.com 2019 - 2024. All rights reserved.