質問編集履歴

1

コードの追記

2019/01/30 02:41

投稿

yellow3
yellow3

スコア13

test CHANGED
File without changes
test CHANGED
@@ -15,3 +15,71 @@
15
15
  2,9
16
16
 
17
17
  となるようなテキストデータにしてほしいです。
18
+
19
+ 現在のコードも載せておきます。
20
+
21
+ ```python
22
+
23
+ import train
24
+
25
+ import sys
26
+
27
+ from PIL import Image
28
+
29
+ import numpy as np
30
+
31
+ import os
32
+
33
+
34
+
35
+ input_dir = 'images'
36
+
37
+ categories = [name for name in os.listdir(input_dir)]
38
+
39
+
40
+
41
+
42
+
43
+ target_dir = sys.argv[1] if len(sys.argv) >= 2 else '.'
44
+
45
+
46
+
47
+ for filename in os.listdir(target_dir):
48
+
49
+ path_in = os.path.join(target_dir, filename)
50
+
51
+ if not (os.path.isfile(path_in) and path_in.endswith('.png')):
52
+
53
+ continue
54
+
55
+ path_out = path_in.replace('.png', '.txt')
56
+
57
+ print('{} -> {} ...'.format(path_in, path_out))
58
+
59
+ with open(path_in) as fin, open(path_out, 'w') as fout:
60
+
61
+ img = Image.open(filename).convert('RGB') ## Gray->L, RGB->RGB
62
+
63
+ img = img.resize((50, 50))
64
+
65
+ x = np.array(img, dtype=np.float32)
66
+
67
+ x = x / 255.
68
+
69
+ x = x[None, ...]
70
+
71
+ model = train.TrainModel().train(x.shape[1:])
72
+
73
+ model.load_weights('namimodel.h5')
74
+
75
+ predict = model.predict(x)
76
+
77
+ for pre in predict:
78
+
79
+ y = pre.argmax()
80
+
81
+ print("hantei : ", categories[y])
82
+
83
+ fout.write(str(pre))
84
+
85
+ ```