djangoで予測コード(main.py)からworker(.init.py)にタスクキューを投げて非同期処理をしようとしました。
workerにはceleryを使ってます。
しかし、json形式になってないとのエラーが出てしまします。
作業の流れ的には以下のようになります。
手順1. $ python manage.py runserverでdjangoを起動
手順2. http://127.0.0.1:8000/myappにアクセス
手順3. フロントエンドで画像を入力
手順4. main.pyの関数predに入力した画像が入り、worker(init.py)の関数add_numbersを呼び出し(非同期処理をして)、予測コードを返す
この手順4でmain.pyのadd_numbers.delay(image)
でworkerにimage(画像)を投げるとエラーが出てる状況です。
おそらくnumpy形式の画像がjson形式でないからだと思うのですが、、
何かいい解決策はないでしょうか?
ご教授お願いします
# エラー Exception Value: Object of type 'ndarray' is not JSON serializable Exception Location: /anaconda3/envs/django/lib/python3.6/json/encoder.py in default, line 180 Python Executable: /anaconda3/envs/django/bin/python
python
1#main.py 2def pred(img_path): 3 x=np.asarray(Image.open(img_path)) 4 x=np.resize(x,(150,150,3)) 5 x = np.expand_dims(x, axis=0) 6 image = preprocess_input(x) 7 id = add_numbers.delay(image) 8 time.sleep(10) 9 id=id.result 10 return id
python
1#__init__.py 2@app.task() 3def add_numbers(image): 4 sess = tf.Session() 5 K.set_session(sess) 6 cnt=pd.read_csv(CNT_PATH) 7 cnt=cnt.drop('Unnamed: 0', axis=1) 8 cnt=cnt.drop('cnt', axis=1) 9 10 11 dic = {} 12 for i, v in cnt.iterrows(): 13 dic.setdefault(v['index'], []).append([v['brand_id'], v['model_id'], v['cate_id']]) 14 15 16 #x=np.asarray(Image.open(img_path)) 17 #x=np.resize(x,(150,150,3)) 18 #x = np.expand_dims(x, axis=0) 19 #image = preprocess_input(x) 20 21 test_model = InceptionResNetV2(include_top=True) 22 test_model.load_weights(WHEIGHT_PATH) 23 test_model.compile(optimizer=SGD(lr=0.01, momentum=0.9, decay=0.001, nesterov=True), 24 loss='categorical_crossentropy', 25 metrics=['accuracy']) 26 27 y_pred = test_model.predict(image) 28 top_k=sess.run(tf.nn.top_k(y_pred,k=3,sorted=True)) 29 idxs=list(np.reshape(top_k[1],(3,))) 30 id=[] 31 for idx, v in dic.items(): 32 if idx in idxs: 33 id.append(['brand_id:{} model_id:{}, cate_id:{}'.format(v[0][0], v[0][1], v[0][2])]) 34 return id
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/23 03:49