質問編集履歴

1

コード追加

2019/11/05 02:09

投稿

ichigo_cookie
ichigo_cookie

スコア8

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,122 @@
19
19
 
20
20
 
21
21
  ### 該当のソースコード
22
+
23
+
24
+
25
+ ```python
26
+
27
+ from PIL import Image
28
+
29
+ import os, glob
30
+
31
+ import numpy as np
32
+
33
+ import random, math
34
+
35
+
36
+
37
+
38
+
39
+ root_dir = "C:/Users/yurus/Desktop/lab/Training2"
40
+
41
+
42
+
43
+ categories = ["Apple Braeburn","Apple Crimson Snow","Apple Golden1","Apple Golden2",
44
+
45
+ "Apple Golden3","Apple Granny Smith","Apple Pink Lady","Apple Red1",
46
+
47
+ "Apple Red2","Apple Red3"]
48
+
49
+
50
+
51
+
52
+
53
+ X = []
54
+
55
+
56
+
57
+ Y = []
58
+
59
+
60
+
61
+
62
+
63
+ def make_sample(files):
64
+
65
+ global X, Y
66
+
67
+ X = []
68
+
69
+ Y = []
70
+
71
+ for cat, fname in files:
72
+
73
+ add_sample(cat, fname)
74
+
75
+ return np.array(X), np.array(Y)
76
+
77
+
78
+
79
+ def add_sample(cat, fname):
80
+
81
+ img = Image.open(fname)
82
+
83
+ img = img.convert("RGB")
84
+
85
+ img = img.resize((150, 150))
86
+
87
+ data = np.asarray(img)
88
+
89
+ X.append(data)
90
+
91
+ Y.append(cat)
92
+
93
+
94
+
95
+
96
+
97
+ allfiles = []
98
+
99
+
100
+
101
+ for idx, cat in enumerate(categories):
102
+
103
+ image_dir = root_dir + "/" + cat
104
+
105
+ files = glob.glob(image_dir + "/*.jpg")
106
+
107
+ for f in files:
108
+
109
+ allfiles.append((idx, f))
110
+
111
+
112
+
113
+
114
+
115
+ random.shuffle(allfiles)
116
+
117
+ th = math.floor(len(allfiles) * 0.8)
118
+
119
+ train = allfiles[0:th]
120
+
121
+ test = allfiles[th:]
122
+
123
+ X_train, y_train = make_sample(train)
124
+
125
+ X_test, y_test = make_sample(test)
126
+
127
+ xy = (X_train, X_test, y_train, y_test)
128
+
129
+
130
+
131
+ np.save("C:/Users/yurus/Desktop/lab/fruit_test.npy", xy)
132
+
133
+
134
+
135
+ こちらはデータを保存したときのコードです↑
136
+
137
+ ```
22
138
 
23
139
 
24
140