画像認識の結果をJSONに保存したいのですがJSONになりません。
コードは以下の通りです
python
1from keras.models import load_model 2from keras.preprocessing.image import ImageDataGenerator 3from keras.preprocessing import image 4import numpy as np 5import json 6 7rgb_image = './images/apple_image.jpg' 8 9model_path = './trained_model/tiny_CNN_apple.h5' 10model = load_model(model_path) 11apple_labels = ['青りんご','赤りんご'] 12 13load_img = image.load_img(rgb_image, target_size=(32, 32)) 14x = image.img_to_array(load_img) 15x = np.expand_dims(x, axis=0) / 255 16 17apple_predict = model.predict(x) 18if apple_predict < 0.5: 19 apple_text = apple_labels[0] 20elif apple_predict >= 0.5: 21 apple_text = apple_labels[1] 22print(apple_text, apple_predict[0][0]) 23dic = { "name": apple_text ,"probiability": apple_predict} 24file =open('sample.json','w') 25json.dump(dic,file)
これを実行すると以下のエラーとともに
bash
1Using TensorFlow backend. 2WARNING:tensorflow:From /Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. 3Instructions for updating: 4Colocations handled automatically by placer. 5WARNING:tensorflow:From /Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. 6Instructions for updating: 7Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`. 82019-07-14 23:11:39.863843: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 9WARNING:tensorflow:From /Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. 10Instructions for updating: 11Use tf.cast instead. 12/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/site-packages/keras/engine/saving.py:327: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer. 13 warnings.warn('Error in loading the saved optimizer ' 14青りんご 0.039337248 15Traceback (most recent call last): 16 File "apple_checker_cv.py", line 25, in <module> 17 json.dump(dic,file) 18 File "/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/json/__init__.py", line 179, in dump 19 for chunk in iterable: 20 File "/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/json/encoder.py", line 431, in _iterencode 21 yield from _iterencode_dict(o, _current_indent_level) 22 File "/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict 23 yield from chunks 24 File "/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/json/encoder.py", line 438, in _iterencode 25 o = _default(o) 26 File "/Users/takabatomoki/.pyenv/versions/3.7.1/lib/python3.7/json/encoder.py", line 179, in default 27 raise TypeError(f'Object of type {o.__class__.__name__} ' 28TypeError: Object of type ndarray is not JSON serializable
JSONファイルにこう書き込まれます。
JSON
1{"name": "\u9752\u308a\u3093\u3054", "probiability":
つまり、構文?どうりに書き込めない、文字化けするということです。
何卒、ご教授のほどよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/14 16:38
2019/07/14 16:47
2019/07/14 17:10
2019/07/14 17:15