Python macos;蟒蛇3;FileNotFoundError:[Errno 2]没有这样的文件或目录:'';。如何修复它?

Python macos;蟒蛇3;FileNotFoundError:[Errno 2]没有这样的文件或目录:'';。如何修复它?,python,terminal,python-3.6,Python,Terminal,Python 3.6,我刚从Python辞职,并尝试启动脚本 将_转换为_tfrecord.py (神经网络;它应该使用一些库来训练图像数据集…) 说明: 现在,您可以运行TFRecord脚本了。运行下面的命令 从tensorflow/models/research目录,并将 以下标志(运行两次:一次用于训练数据,一次用于测试 数据): 所以为了适应我的OS X,我用python3运行了这个脚本。。。更改脚本的名称。。。并设置一个目录。。。 我在我的脚本所在的目录下;我的文件夹在哪里;我的图书馆在哪里。 就我而言:

我刚从Python辞职,并尝试启动脚本

将_转换为_tfrecord.py

(神经网络;它应该使用一些库来训练图像数据集…)

说明:

现在,您可以运行TFRecord脚本了。运行下面的命令 从tensorflow/models/research目录,并将 以下标志(运行两次:一次用于训练数据,一次用于测试 数据):

所以为了适应我的OS X,我用python3运行了这个脚本。。。更改脚本的名称。。。并设置一个目录。。。 我在我的脚本所在的目录下;我的文件夹在哪里;我的图书馆在哪里。 就我而言:

python3 convert_to_tfrecord.py \
--output_path=train.record \ 
--images_dir=ENFj/ \
--labels_dir=ENFj/xml/
结果:

Oleksandrs-MacBook-Air:research jaskier$ python3 convert_to_tfrecord.py \
> --output_path=train.record \ 
Traceback (most recent call last):
  File "convert_to_tfrecord.py", line 89, in <module>
    tf.app.run()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 126, in run
    _sys.exit(main(argv))
  File "convert_to_tfrecord.py", line 81, in main
    for filename in os.listdir(FLAGS.images_dir):
FileNotFoundError: [Errno 2] No such file or directory: ''
Oleksandrs-MacBook-Air:research jaskier$ --images_dir=ENFj/ \
> --labels_dir=ENFj/xml/
尝试了不同的东西: 1.更新请求(一行+删除“\”,因为正如下面提到的,这是页面的PHP解释器的错误…它使用了\n并忘记隐藏输出文本):

2.“查找.-名称”.DS_商店”-删除 3.再次尝试:

文件 “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/PIL/Image.py”, 第2543行,在open fp=builtins.open(文件名“rb”)中 IsDirectoryError:[Errno 21]是一个目录: “/Users/jaskier/Downloads/models/research/ENFj/xml”


该代码用于将
--images\u dir
中的所有文件解释给某些图像处理机器。这意味着
--images\u dir
中的任何非图像资源都将导致脚本中断

一种解决方案是确保
--images\u dir
仅包含图像文件(即,确保目录不包含XML文件或以开头的文件,如
.git
.DS\u Store

另一个解决方案是修改源代码本身,使其仅对图像文件起作用。可以使用类似的方法:

import glob

# only match jpg files in the images_dir
for filename in glob.glob(FLAGS.images_dir + '/*.jpg'):
  tf_example = create_tf_example(filename)
  # copy the other lines here as needed

就我而言,这只是一个简单的愚蠢错误:当我手动更改文件夹名称时:

我在名字后面留了一个空格——当我用osglob.glob调用它时,我得到了:
FileNotFoundError:[Errno 2]没有这样的文件或目录:


在花费一些时间搜索问题(甚至更改一些文件名和删除一些子文件夹)后我回到文件夹,删除了多余的空间,一切正常。

我想你的运行脚本可以用不可见的字符
\n
从博客中复制出来。只要试试
python3 convert\u to\u tfrecord.py--output\u path=train.record--images\u dir=ENFj/--labels\u dir=ENFj/xml/
@Oleksandr谢谢你的跟进!我是glad我们开始了!几乎…但是在这段代码之后,我直接错误到第33行-这2行。文件“convert\u to\u tfrecord.py”,第33行image\u path=FLAGS.images\u dir+示例^IndentationError:应为缩进block@OleksandrPython是一种对空格敏感的语言。请尝试在每行之前插入4个空格…尝试了不同的东西,但我只有:File“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/PIL/Image.py”,第2543行,在open fp=builtins.open(filename,“rb”)中,isDirectoryError:[Errno 21]是一个目录:“/Users/jaskier/Downloads/models/research/ENFj/xml”我猜路径有问题……甚至可能是负责路径的模块(啊哈,把
xml/
移出
ENFj/
ENFj/
应该只包含图像,而
xml/
应该只包含xml;然后你可以运行:
python3 convert\u to\u tfrecord.py--output\u path=train.record--images\u dir=ENFj/--labels\u dir xml/
import os
import io
import xml.etree.ElementTree as ET
import tensorflow as tf

from object_detection.utils import dataset_util
from PIL import Image


flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('images_dir', '', 'Path to directory of images')
flags.DEFINE_string('labels_dir', '', 'Path to directory of labels')
FLAGS = flags.FLAGS


def create_tf_example(example):

    image_path = os.getcwd() + '/' +  FLAGS.images_dir + example
    labels_path = os.getcwd() + '/' +  FLAGS.labels_dir + os.path.splitext(example)[0] + '.xml'

    # Read the image
    img = Image.open(image_path)
    width, height = img.size
    img_bytes = io.BytesIO()
    img.save(img_bytes, format=img.format)

    height = height
    width = width
    encoded_image_data = img_bytes.getvalue()
    image_format = img.format.encode('utf-8')

    # Read the label XML
    tree = ET.parse(labels_path)
    root = tree.getroot()
    xmins = xmaxs = ymins = ymaxs = list()

    for coordinate in root.find('object').iter('bndbox'):
        xmins = [int(coordinate.find('xmin').text)]
        xmaxs = [int(coordinate.find('xmax').text)]
        ymins = [int(coordinate.find('ymin').text)]
        ymaxs = [int(coordinate.find('ymax').text)]

    classes_text = ['tswift'.encode('utf-8')]
    classes = [1]

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(encoded_image_data),
        'image/source_id': dataset_util.bytes_feature(encoded_image_data),
        'image/encoded': dataset_util.bytes_feature(encoded_image_data),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example


def main(_):
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    for filename in os.listdir(FLAGS.images_dir):
        tf_example = create_tf_example(filename)
        writer.write(tf_example.SerializeToString())

    writer.close()


if __name__ == '__main__':
    tf.app.run()
python3 convert_to_tfrecord.py 
--output_path=train.record 
--images_dir=ENFj
--labels_dir=ENFj/xml
import glob

# only match jpg files in the images_dir
for filename in glob.glob(FLAGS.images_dir + '/*.jpg'):
  tf_example = create_tf_example(filename)
  # copy the other lines here as needed