質問するログイン新規登録

質問編集履歴

1

コードの追記

2019/01/30 02:41

投稿

yellow3
yellow3

スコア13

title CHANGED
File without changes
body CHANGED
@@ -6,4 +6,38 @@
6
6
  出力では
7
7
  1,1
8
8
  2,9
9
- となるようなテキストデータにしてほしいです。
9
+ となるようなテキストデータにしてほしいです。
10
+ 現在のコードも載せておきます。
11
+ ```python
12
+ import train
13
+ import sys
14
+ from PIL import Image
15
+ import numpy as np
16
+ import os
17
+
18
+ input_dir = 'images'
19
+ categories = [name for name in os.listdir(input_dir)]
20
+
21
+
22
+ target_dir = sys.argv[1] if len(sys.argv) >= 2 else '.'
23
+
24
+ for filename in os.listdir(target_dir):
25
+ path_in = os.path.join(target_dir, filename)
26
+ if not (os.path.isfile(path_in) and path_in.endswith('.png')):
27
+ continue
28
+ path_out = path_in.replace('.png', '.txt')
29
+ print('{} -> {} ...'.format(path_in, path_out))
30
+ with open(path_in) as fin, open(path_out, 'w') as fout:
31
+ img = Image.open(filename).convert('RGB') ## Gray->L, RGB->RGB
32
+ img = img.resize((50, 50))
33
+ x = np.array(img, dtype=np.float32)
34
+ x = x / 255.
35
+ x = x[None, ...]
36
+ model = train.TrainModel().train(x.shape[1:])
37
+ model.load_weights('namimodel.h5')
38
+ predict = model.predict(x)
39
+ for pre in predict:
40
+ y = pre.argmax()
41
+ print("hantei : ", categories[y])
42
+ fout.write(str(pre))
43
+ ```