json.decoder.JSONDecodeError:期望值:从Google QuickDraw数据集中读取json文件时,第1行第1列(字符0)

时间:2019-02-24 03:43:27

标签: python json deep-learning dataset

我正在使用Google QuickDraw数据集ndjson文件制作一个应用程序。我正在文件的每一行上运行此功能:

def parse_line(ndjson_line):
    """Parse an ndjson line and return ink (as np array) and classname."""
    sample = json.loads(ndjson_line)
    class_name = sample["word"]
    if not class_name:
        print("Empty classname")
        return None, None
    inkarray = sample["drawing"]
    stroke_lengths = [len(stroke[0]) for stroke in inkarray]
    total_points = sum(stroke_lengths)
    np_ink = np.zeros((total_points, 3), dtype=np.float32)
    current_t = 0
    if not inkarray:
        print("Empty inkarray")
        return None, None
    for stroke in inkarray:
        if len(stroke[0]) != len(stroke[1]):
            print("Inconsistent number of x and y coordinates.")
            return None, None
    for i in [0, 1]:
        np_ink[current_t:(current_t + len(stroke[0])), i] = stroke[i]
    current_t += len(stroke[0])
    np_ink[current_t - 1, 2] = 1  # stroke_end
    # Preprocessing.
    # 1. Size normalization.
    lower = np.min(np_ink[:, 0:2], axis=0)
    upper = np.max(np_ink[:, 0:2], axis=0)
    scale = upper - lower
    scale[scale == 0] = 1
    np_ink[:, 0:2] = (np_ink[:, 0:2] - lower) / scale
    # 2. Compute deltas.
    np_ink[1:, 0:2] -= np_ink[0:-1, 0:2]
    np_ink = np_ink[1:, :]
    return np_ink, class_name

对于大多数行它都可以正常工作,但是对于少数几行,例如:

{"word":"wristwatch","countrycode":"FR","timestamp":"2017-01-19 09:30:18.19194 UTC","recognized":true,"key_id":"6721203257475072","drawing":[[[0,143],[66,67]],[[1,170],[35,39]],[[169,169,179,186,187,193,216,228,228,225,249,254,255,249,244,246,251,254,242,226,232,238,237,224,235,234,211,201,197,192,170,160,144,141,142],[39,26,7,9,25,15,2,2,12,19,7,23,36,39,34,32,37,56,54,44,47,58,67,65,74,80,84,82,75,92,73,97,85,71,67]],[[94,96,110],[26,88,89]]]}

我遇到以下错误:

Traceback (most recent call last):
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 168, in <module>
    tf.app.run(main=main,argv=[sys.argv[0]]+unparsed)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 124, in main
    classes = convert_to_tfrecord(FLAGS.source_path,FLAGS.destination_path,FLAGS.train_examples_per_class,FLAGS.eval_examples_per_class,FLAGS.classes_path,FLAGS.output_shards)
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 98, in convert_to_tfrecord
    drawing, class_name  = parse_line(ndjson_line)
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 13, in parse_line
    sample = json.loads(ndjson_line)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

该错误的原因可能是什么?它说的是期望值,但是我已经通过了,所以它需要什么?而且,这与我通过的其他行没有什么不同,这些行没有任何错误,那么那行是什么呢?我需要在JSON文件或代码中进行哪些更改?我已经从Google GitHub存储库中获取了代码。另外,在数据集中,我正在使用数据集中的简化JSON文件。整个数据集是:

QuickDraw dataset

1 个答案:

答案 0 :(得分:1)

也许您的数据集有空行,您可以尝试添加此行以检查错误字符串

exec()

已更新#2

异常处理

try:
    sample = json.loads(ndjson_line)
except json.decoder.JSONDecodeError as e:
    print("Error Line: {}\n".format(ndjson_line)) # Print the Decode Error string
    raise e # To Stop the Program