- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python构建图像分类识别器的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
机器学习用在图像识别是非常有趣的话题.
我们可以利用OpenCV强大的功能结合机器学习算法实现图像识别系统.
首先,输入若干图像,加入分类标记。利用向量量化方法将特征点进行聚类,并得出中心点,这些中心点就是视觉码本的元素.
其次,利用图像分类器将图像分到已知的类别中,ERF(极端随机森林)算法非常流行,因为ERF具有较快的速度和比较精确的准确度。我们利用决策树进行正确决策.
最后,利用训练好的ERF模型后,创建目标识别器,可以识别未知图像的内容.
当然,这只是雏形,存在很多问题:
界面不友好.
准确率如何保证,如何调整超参数,只有认真研究算法机理,才能真正清除内部实现机制后给予改进.
下面,上代码:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
import
os
import
sys
import
argparse
import
json
import
cv2
import
numpy as np
from
sklearn.cluster
import
KMeans
# from star_detector import StarFeatureDetector
from
sklearn.ensemble
import
ExtraTreesClassifier
from
sklearn
import
preprocessing
try
:
import
cPickle as pickle
#python 2
except
ImportError as e:
import
pickle
#python 3
def
load_training_data(input_folder):
training_data
=
[]
if
not
os.path.isdir(input_folder):
raise
IOError(
"The folder "
+
input_folder
+
" doesn't exist"
)
for
root, dirs, files
in
os.walk(input_folder):
for
filename
in
(x
for
x
in
files
if
x.endswith(
'.jpg'
)):
filepath
=
os.path.join(root, filename)
print
(filepath)
object_class
=
filepath.split(
'\\'
)[
-
2
]
print
(
"object_class"
,object_class)
training_data.append({
'object_class'
: object_class,
'image_path'
: filepath})
return
training_data
class
StarFeatureDetector(
object
):
def
__init__(
self
):
self
.detector
=
cv2.xfeatures2d.StarDetector_create()
def
detect(
self
, img):
return
self
.detector.detect(img)
class
FeatureBuilder(
object
):
def
extract_features(
self
, img):
keypoints
=
StarFeatureDetector().detect(img)
keypoints, feature_vectors
=
compute_sift_features(img, keypoints)
return
feature_vectors
def
get_codewords(
self
, input_map, scaling_size, max_samples
=
12
):
keypoints_all
=
[]
count
=
0
cur_class
=
''
for
item
in
input_map:
if
count >
=
max_samples:
if
cur_class !
=
item[
'object_class'
]:
count
=
0
else
:
continue
count
+
=
1
if
count
=
=
max_samples:
print
(
"Built centroids for"
, item[
'object_class'
])
cur_class
=
item[
'object_class'
]
img
=
cv2.imread(item[
'image_path'
])
img
=
resize_image(img, scaling_size)
num_dims
=
128
feature_vectors
=
self
.extract_features(img)
keypoints_all.extend(feature_vectors)
kmeans, centroids
=
BagOfWords().cluster(keypoints_all)
return
kmeans, centroids
class
BagOfWords(
object
):
def
__init__(
self
, num_clusters
=
32
):
self
.num_dims
=
128
self
.num_clusters
=
num_clusters
self
.num_retries
=
10
def
cluster(
self
, datapoints):
kmeans
=
KMeans(
self
.num_clusters,
n_init
=
max
(
self
.num_retries,
1
),
max_iter
=
10
, tol
=
1.0
)
res
=
kmeans.fit(datapoints)
centroids
=
res.cluster_centers_
return
kmeans, centroids
def
normalize(
self
, input_data):
sum_input
=
np.
sum
(input_data)
if
sum_input >
0
:
return
input_data
/
sum_input
else
:
return
input_data
def
construct_feature(
self
, img, kmeans, centroids):
keypoints
=
StarFeatureDetector().detect(img)
keypoints, feature_vectors
=
compute_sift_features(img, keypoints)
labels
=
kmeans.predict(feature_vectors)
feature_vector
=
np.zeros(
self
.num_clusters)
for
i, item
in
enumerate
(feature_vectors):
feature_vector[labels[i]]
+
=
1
feature_vector_img
=
np.reshape(feature_vector, ((
1
, feature_vector.shape[
0
])))
return
self
.normalize(feature_vector_img)
# Extract features from the input images and
# map them to the corresponding object classes
def
get_feature_map(input_map, kmeans, centroids, scaling_size):
feature_map
=
[]
for
item
in
input_map:
temp_dict
=
{}
temp_dict[
'object_class'
]
=
item[
'object_class'
]
print
(
"Extracting features for"
, item[
'image_path'
])
img
=
cv2.imread(item[
'image_path'
])
img
=
resize_image(img, scaling_size)
temp_dict[
'feature_vector'
]
=
BagOfWords().construct_feature(img, kmeans, centroids)
if
temp_dict[
'feature_vector'
]
is
not
None
:
feature_map.append(temp_dict)
return
feature_map
# Extract SIFT features
def
compute_sift_features(img, keypoints):
if
img
is
None
:
raise
TypeError(
'Invalid input image'
)
img_gray
=
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
keypoints, descriptors
=
cv2.xfeatures2d.SIFT_create().compute(img_gray, keypoints)
return
keypoints, descriptors
# Resize the shorter dimension to 'new_size'
# while maintaining the aspect ratio
def
resize_image(input_img, new_size):
h, w
=
input_img.shape[:
2
]
scaling_factor
=
new_size
/
float
(h)
if
w < h:
scaling_factor
=
new_size
/
float
(w)
new_shape
=
(
int
(w
*
scaling_factor),
int
(h
*
scaling_factor))
return
cv2.resize(input_img, new_shape)
def
build_features_main():
data_folder
=
'training_images\\'
scaling_size
=
200
codebook_file
=
'codebook.pkl'
feature_map_file
=
'feature_map.pkl'
# Load the training data
training_data
=
load_training_data(data_folder)
# Build the visual codebook
print
(
"====== Building visual codebook ======"
)
kmeans, centroids
=
FeatureBuilder().get_codewords(training_data, scaling_size)
if
codebook_file:
with
open
(codebook_file,
'wb'
) as f:
pickle.dump((kmeans, centroids), f)
# Extract features from input images
print
(
"\n====== Building the feature map ======"
)
feature_map
=
get_feature_map(training_data, kmeans, centroids, scaling_size)
if
feature_map_file:
with
open
(feature_map_file,
'wb'
) as f:
pickle.dump(feature_map, f)
# --feature-map-file feature_map.pkl --model- file erf.pkl
#----------------------------------------------------------------------------------------------------------
class
ERFTrainer(
object
):
def
__init__(
self
, X, label_words):
self
.le
=
preprocessing.LabelEncoder()
self
.clf
=
ExtraTreesClassifier(n_estimators
=
100
,
max_depth
=
16
, random_state
=
0
)
y
=
self
.encode_labels(label_words)
self
.clf.fit(np.asarray(X), y)
def
encode_labels(
self
, label_words):
self
.le.fit(label_words)
return
np.array(
self
.le.transform(label_words), dtype
=
np.float32)
def
classify(
self
, X):
label_nums
=
self
.clf.predict(np.asarray(X))
label_words
=
self
.le.inverse_transform([
int
(x)
for
x
in
label_nums])
return
label_words
#------------------------------------------------------------------------------------------
class
ImageTagExtractor(
object
):
def
__init__(
self
, model_file, codebook_file):
with
open
(model_file,
'rb'
) as f:
self
.erf
=
pickle.load(f)
with
open
(codebook_file,
'rb'
) as f:
self
.kmeans,
self
.centroids
=
pickle.load(f)
def
predict(
self
, img, scaling_size):
img
=
resize_image(img, scaling_size)
feature_vector
=
BagOfWords().construct_feature(
img,
self
.kmeans,
self
.centroids)
image_tag
=
self
.erf.classify(feature_vector)[
0
]
return
image_tag
def
train_Recognizer_main():
feature_map_file
=
'feature_map.pkl'
model_file
=
'erf.pkl'
# Load the feature map
with
open
(feature_map_file,
'rb'
) as f:
feature_map
=
pickle.load(f)
# Extract feature vectors and the labels
label_words
=
[x[
'object_class'
]
for
x
in
feature_map]
dim_size
=
feature_map[
0
][
'feature_vector'
].shape[
1
]
X
=
[np.reshape(x[
'feature_vector'
], (dim_size,))
for
x
in
feature_map]
# Train the Extremely Random Forests classifier
erf
=
ERFTrainer(X, label_words)
if
model_file:
with
open
(model_file,
'wb'
) as f:
pickle.dump(erf, f)
#--------------------------------------------------------------------
# args = build_arg_parser().parse_args()
model_file
=
'erf.pkl'
codebook_file
=
'codebook.pkl'
import
os
rootdir
=
r
"F:\airplanes"
list
=
os.listdir(rootdir)
for
i
in
range
(
0
,
len
(
list
)):
path
=
os.path.join(rootdir,
list
[i])
if
os.path.isfile(path):
try
:
print
(path)
input_image
=
cv2.imread(path)
scaling_size
=
200
print
(
"\nOutput:"
, ImageTagExtractor(model_file,codebook_file)\
.predict(input_image, scaling_size))
except
:
continue
#-----------------------------------------------------------------------
build_features_main()
train_Recognizer_main()
|
以上这篇Python构建图像分类识别器的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/weixin_42039090/article/details/80673711 。
最后此篇关于Python构建图像分类识别器的方法的文章就讲到这里了,如果你想了解更多关于Python构建图像分类识别器的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!