質問編集履歴
1
前回が、あまりにもできてないものだったので、ちゃんとしたものまで変更。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
どこが足りていないのか、教えてほしい
|
test
CHANGED
@@ -6,49 +6,177 @@
|
|
6
6
|
### 実現したいこと
|
7
7
|
|
8
8
|
|
9
|
+
- ウェブカメラで撮影し、予測結果の出力しじゃんけんで勝つソフトを作る予定(google colaboratoryにて動作するものを作りたい)
|
9
|
-
-
|
10
|
+
- None値になっているが、そもそもウェブカメラがちゃんと動いてない恐れあり。
|
11
|
+
→できれば、google colaboratoryでウェブカメラをちゃんと動かす方法も知りたい。
|
10
12
|
|
11
13
|
|
12
14
|
### 発生している問題・エラーメッセージ
|
13
15
|
|
14
|
-
```
|
15
|
-
エラーメッセージ
|
16
|
-
```---------------------------------------------------------------------------
|
17
|
-
|
16
|
+
AttributeError Traceback (most recent call last)
|
18
|
-
<ipython-input-1
|
17
|
+
<ipython-input-1-302c9809989e> in <module>
|
19
|
-
|
18
|
+
32 #画像を認識し、補正する
|
20
|
-
|
19
|
+
33 #PILを使用して画像をリサイズする
|
21
|
-
---
|
20
|
+
---> 34 image = Image.fromarray(frame)
|
21
|
+
35 image = image.resize((input_width, input_height))
|
22
|
-
|
22
|
+
36 image = np.asarray(image)
|
23
23
|
|
24
|
+
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in fromarray(obj, mode)
|
25
|
+
2702 .. versionadded:: 1.1.6
|
24
|
-
|
26
|
+
2703 """
|
27
|
+
-> 2704 arr = obj.__array_interface__
|
28
|
+
2705 shape = arr["shape"]
|
29
|
+
2706 ndim = len(shape)
|
30
|
+
|
31
|
+
AttributeError: 'NoneType' object has no attribute '__array_interface__'
|
25
32
|
|
26
33
|
### 該当のソースコード
|
27
|
-
|
28
34
|
import json
|
29
35
|
import numpy as np
|
30
36
|
import tensorflow as tf
|
37
|
+
import cv2
|
31
38
|
from PIL import Image
|
32
39
|
|
40
|
+
#ファイルのパスを修正
|
33
|
-
with open("/content/drive/MyDrive/TensorFlow/signature.json", "r") as f:
|
41
|
+
with open("/content/drive/MyDrive/TensorFlow/signature.json", "r") as f:#signature.jsonの位置を確認し入力(必要に応じて変更必要)
|
34
42
|
signature = json.load(f)
|
43
|
+
|
35
44
|
inputs = signature.get('inputs')
|
36
45
|
outputs = signature.get('outputs')
|
37
46
|
|
38
|
-
|
47
|
+
#TensorFlowのセッションを作成
|
39
|
-
in
|
48
|
+
session = tf.compat.v1.Session(graph=tf.Graph())
|
40
49
|
|
50
|
+
#モデルをロード
|
51
|
+
tf.compat.v1.saved_model.loader.load(
|
52
|
+
sess=session,
|
53
|
+
tags=signature.get("tags"),
|
54
|
+
export_dir='/content/drive/MyDrive/TensorFlow/'#saved_modelの入っているファイルを探す(必要に応じて変更必要)
|
55
|
+
)
|
56
|
+
|
57
|
+
#入力画像のサイズを取得
|
41
58
|
input_width, input_height = inputs["Image"]["shape"][1:3]
|
59
|
+
|
60
|
+
#OpenCVを使ってウェブカメラから画像を取得する
|
61
|
+
cap = cv2.VideoCapture(0)
|
62
|
+
|
63
|
+
ret, frame = cap.read()
|
64
|
+
|
65
|
+
#画像を認識し、補正する
|
66
|
+
#PILを使用して画像をリサイズする
|
42
|
-
image = Image.o
|
67
|
+
image = Image.fromarray(frame)
|
43
68
|
image = image.resize((input_width, input_height))
|
44
|
-
image = np.asarray(image, dtype=np.float32) / 255.0
|
45
|
-
image = np.
|
69
|
+
image = np.asarray(image)
|
70
|
+
|
71
|
+
#画像を補正する
|
72
|
+
image = image.rotate(0) # 画像を45度回転させる(必要に応じて変更必要)
|
73
|
+
#また、必要に応じて、画像の平行移動やモノクロ化なども可能
|
74
|
+
|
75
|
+
#numpy配列に変換する
|
76
|
+
image = np.asarray(image)
|
77
|
+
|
78
|
+
#画像を入力としてTensorFlowを実行する
|
79
|
+
#入力と出力を定義
|
80
|
+
feed_dict = {inputs["Image"]["name"]: [image]}
|
81
|
+
fetches = [(key, output["name"]) for key, output in outputs.items()]
|
82
|
+
#実行
|
83
|
+
output = session.run(fetches=[name for _, name in fetches], feed_dict=feed_dict)
|
84
|
+
|
85
|
+
#出力から、認識された画像を取得する
|
86
|
+
recognized_image = output[0][0]
|
87
|
+
|
88
|
+
#出力を確認する
|
89
|
+
#print(output)
|
90
|
+
#今回は確認のため追加
|
91
|
+
|
92
|
+
#認識された画像をOpenCVで表示する
|
93
|
+
cv2.imshow("Recognized Image", recognized_image)
|
94
|
+
|
95
|
+
#出力から、じゃんけんの結果を取得する
|
96
|
+
result = output[0][0]
|
97
|
+
|
98
|
+
#最大値を取得
|
99
|
+
#出力から、じゃんけんの結果を取得する
|
100
|
+
#例えば、グーなら"rock"、チョキなら"scissors"など
|
101
|
+
result = ["unknown", "rock", "parer", "scissors"][np.argmax(output[0][0])]
|
102
|
+
|
103
|
+
#結果を表示する
|
104
|
+
print(f'じゃんけんの結果は{result}です。')
|
105
|
+
|
106
|
+
#キーを押すまで画面を表示する
|
107
|
+
cv2.waitKey(0)
|
108
|
+
|
109
|
+
#終了時の処理
|
110
|
+
cap.release()
|
111
|
+
cv2.destroyAllWindows()
|
112
|
+
|
113
|
+
#セッションをクローズする
|
114
|
+
session.close()
|
46
115
|
|
47
116
|
### 試したこと
|
48
117
|
|
49
|
-
TensorFlow
|
118
|
+
Lobe(TensorFlow)とpythonだけで動かし、画像を基に動かしたときは、特に問題なく動いた
|
119
|
+
|
120
|
+
以下:動かしたときのコード
|
121
|
+
import json
|
122
|
+
import numpy as np
|
123
|
+
import tensorflow as tf
|
124
|
+
import cv2
|
125
|
+
from PIL import Image
|
126
|
+
|
127
|
+
#ファイルのパスを修正
|
128
|
+
with open("/content/drive/MyDrive/TensorFlow/signature.json", "r") as f:#signature.jsonの位置を確認し入力
|
129
|
+
signature = json.load(f)
|
130
|
+
|
131
|
+
inputs = signature.get('inputs')
|
132
|
+
outputs = signature.get('outputs')
|
133
|
+
|
134
|
+
#TensorFlowのセッションを作成
|
135
|
+
session = tf.compat.v1.Session(graph=tf.Graph())
|
136
|
+
|
137
|
+
#モデルをロード
|
138
|
+
tf.compat.v1.saved_model.loader.load(
|
139
|
+
sess=session,
|
140
|
+
tags=signature.get("tags"),
|
141
|
+
export_dir='/content/drive/MyDrive/TensorFlow/'#saved_modelの入っているファイルを探す
|
142
|
+
)
|
143
|
+
|
144
|
+
#入力画像のサイズを取得
|
145
|
+
input_width, input_height = inputs["Image"]["shape"][1:3]
|
146
|
+
#入力画像のサイズを取得
|
147
|
+
input_width, input_height = inputs["Image"]["shape"][1:3]
|
148
|
+
|
149
|
+
|
150
|
+
#画像を開いてリサイズする
|
151
|
+
image = Image.open('/content/drive/MyDrive/output/hand/gu/IMG_5993.JPG')#検証につき画像ファイル選択(gu-)
|
152
|
+
image = image.resize((input_width, input_height))
|
153
|
+
image = np.asarray(image) / 255.0
|
154
|
+
|
155
|
+
#入力と出力を定義
|
156
|
+
feed_dict = {inputs["Image"]["name"]: [image]}
|
157
|
+
fetches = [(key, output["name"]) for key, output in outputs.items()]
|
158
|
+
|
159
|
+
#実行
|
160
|
+
output = session.run(fetches=[name for _, name in fetches], feed_dict=feed_dict)
|
161
|
+
|
162
|
+
#出力を確認する
|
163
|
+
print(output)#今回は確認のためですが、実用時は消せます。
|
164
|
+
|
165
|
+
|
166
|
+
#出力から、じゃんけんの結果を取得する
|
167
|
+
result = output[0][0]
|
168
|
+
|
169
|
+
#最大値を取得
|
170
|
+
#出力から、じゃんけんの結果を取得する
|
171
|
+
#例えば、グーなら"rock"、チョキなら"scissors"など
|
172
|
+
result = ["unknown", "rock", "parer", "scissors"][np.argmax(output[0][0])]
|
173
|
+
|
174
|
+
#結果を表示する
|
175
|
+
print(f'じゃんけんの結果は{result}です。')
|
176
|
+
|
177
|
+
|
50
178
|
|
51
179
|
### 補足情報(FW/ツールのバージョンなど)
|
52
180
|
|
53
|
-
|
181
|
+
環境:google colaboratory
|
54
|
-
|
182
|
+
TensorFlowは、じゃんけんの画像を基に作っており、精度は全体的に60%程度のものを使用中
|