-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_img_classifier_for_fruits.py
70 lines (53 loc) · 2 KB
/
04_img_classifier_for_fruits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import numpy as np
import keras
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array, load_img
#===================================================
#モデル、重み、学習プロセスの読み込み
#===================================================
#モデルを読み込む
model = model_from_json(open('model_fruits.json').read())#★
#重みを読み込む
model.load_weights('weights_fruits.h5')#★
#損失関数、オプティマイザを指定
model.compile(loss='categorical_crossentropy', optimizer='adam')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('image', frame)
key = cv2.waitKey(1)
if key != -1:
break
#===================================================
#推論する画像を読み込み
#===================================================
x = frame
x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB) #RBGをRGBに変える
x = cv2.resize(x, (32,32))
#データ形式をCNNモデルに合わせる(3次元のPILから3次元のndarrayへ変更)
x = img_to_array(x)
#データを0.0~1.0へ正規化
x = x.astype('float32')/ 255.0
#次元を合わせる
x = np.expand_dims(x, axis=0)
#===================================================
#推論と結果表示
#===================================================
#推論
preds = model.predict(x)
print("predicts : " + str(preds))
#predsのインデックスでソート
preds_argsort = np.argsort(preds)
print("sort : " + str(preds_argsort))
#最大のインデックス
index = preds_argsort[0][-1]
print("index(max) : " + str(index))
#推論結果をリストから表示
label_list = ["無し", "チェリー", "桃", "いちご"]
print("予測 : " +str(label_list[index]))
#推論結果の確率を表示
probability = preds[0][index] * 100
print("確率 : " + str(probability) + " %")
cap.release()
cv2.destroyAllWindows()