前提
google colaboratory(python)でLobeを使った画像解析のシステムを作っています。
お試しでじゃんけんの手を認識しようとした際にエラーメッセージが発生しました。
実現したいこと
- ウェブカメラで撮影し、予測結果の出力しじゃんけんで勝つソフトを作る予定(google colaboratoryにて動作するものを作りたい)
- None値になっているが、そもそもウェブカメラがちゃんと動いてない恐れあり。
→できれば、google colaboratoryでウェブカメラをちゃんと動かす方法も知りたい。
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last)
<ipython-input-1-302c9809989e> in <module>
32 #画像を認識し、補正する
33 #PILを使用して画像をリサイズする
---> 34 image = Image.fromarray(frame)
35 image = image.resize((input_width, input_height))
36 image = np.asarray(image)
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in fromarray(obj, mode)
2702 .. versionadded:: 1.1.6
2703 """
-> 2704 arr = obj.array_interface
2705 shape = arr["shape"]
2706 ndim = len(shape)
AttributeError: 'NoneType' object has no attribute 'array_interface'
該当のソースコード
import json
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image
#ファイルのパスを修正
with open("/content/drive/MyDrive/TensorFlow/signature.json", "r") as f:#signature.jsonの位置を確認し入力(必要に応じて変更必要)
signature = json.load(f)
inputs = signature.get('inputs')
outputs = signature.get('outputs')
#TensorFlowのセッションを作成
session = tf.compat.v1.Session(graph=tf.Graph())
#モデルをロード
tf.compat.v1.saved_model.loader.load(
sess=session,
tags=signature.get("tags"),
export_dir='/content/drive/MyDrive/TensorFlow/'#saved_modelの入っているファイルを探す(必要に応じて変更必要)
)
#入力画像のサイズを取得
input_width, input_height = inputs["Image"]["shape"][1:3]
#OpenCVを使ってウェブカメラから画像を取得する
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
#画像を認識し、補正する
#PILを使用して画像をリサイズする
image = Image.fromarray(frame)
image = image.resize((input_width, input_height))
image = np.asarray(image)
#画像を補正する
image = image.rotate(0) # 画像を45度回転させる(必要に応じて変更必要)
#また、必要に応じて、画像の平行移動やモノクロ化なども可能
#numpy配列に変換する
image = np.asarray(image)
#画像を入力としてTensorFlowを実行する
#入力と出力を定義
feed_dict = {inputs["Image"]["name"]: [image]}
fetches = [(key, output["name"]) for key, output in outputs.items()]
#実行
output = session.run(fetches=[name for _, name in fetches], feed_dict=feed_dict)
#出力から、認識された画像を取得する
recognized_image = output[0][0]
#出力を確認する
#print(output)
#今回は確認のため追加
#認識された画像をOpenCVで表示する
cv2.imshow("Recognized Image", recognized_image)
#出力から、じゃんけんの結果を取得する
result = output[0][0]
#最大値を取得
#出力から、じゃんけんの結果を取得する
#例えば、グーなら"rock"、チョキなら"scissors"など
result = ["unknown", "rock", "parer", "scissors"][np.argmax(output[0][0])]
#結果を表示する
print(f'じゃんけんの結果は{result}です。')
#キーを押すまで画面を表示する
cv2.waitKey(0)
#終了時の処理
cap.release()
cv2.destroyAllWindows()
#セッションをクローズする
session.close()
試したこと
Lobe(TensorFlow)とpythonだけで動かし、画像を基に動かしたときは、特に問題なく動いた
以下:動かしたときのコード
import json
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image
#ファイルのパスを修正
with open("/content/drive/MyDrive/TensorFlow/signature.json", "r") as f:#signature.jsonの位置を確認し入力
signature = json.load(f)
inputs = signature.get('inputs')
outputs = signature.get('outputs')
#TensorFlowのセッションを作成
session = tf.compat.v1.Session(graph=tf.Graph())
#モデルをロード
tf.compat.v1.saved_model.loader.load(
sess=session,
tags=signature.get("tags"),
export_dir='/content/drive/MyDrive/TensorFlow/'#saved_modelの入っているファイルを探す
)
#入力画像のサイズを取得
input_width, input_height = inputs["Image"]["shape"][1:3]
#入力画像のサイズを取得
input_width, input_height = inputs["Image"]["shape"][1:3]
#画像を開いてリサイズする
image = Image.open('/content/drive/MyDrive/output/hand/gu/IMG_5993.JPG')#検証につき画像ファイル選択(gu-)
image = image.resize((input_width, input_height))
image = np.asarray(image) / 255.0
#入力と出力を定義
feed_dict = {inputs["Image"]["name"]: [image]}
fetches = [(key, output["name"]) for key, output in outputs.items()]
#実行
output = session.run(fetches=[name for _, name in fetches], feed_dict=feed_dict)
#出力を確認する
print(output)#今回は確認のためですが、実用時は消せます。
#出力から、じゃんけんの結果を取得する
result = output[0][0]
#最大値を取得
#出力から、じゃんけんの結果を取得する
#例えば、グーなら"rock"、チョキなら"scissors"など
result = ["unknown", "rock", "parer", "scissors"][np.argmax(output[0][0])]
#結果を表示する
print(f'じゃんけんの結果は{result}です。')
補足情報(FW/ツールのバージョンなど)
環境:google colaboratory
TensorFlowは、じゃんけんの画像を基に作っており、精度は全体的に60%程度のものを使用中
回答1件
あなたの回答
tips
プレビュー