python进⾏图像识别与分类_Python构建图像分类识别器的⽅
法
机器学习⽤在图像识别是⾮常有趣的话题。
我们可以利⽤OpenCV强⼤的功能结合机器学习算法实现图像识别系统。
⾸先,输⼊若⼲图像,加⼊分类标记。利⽤向量量化⽅法将特征点进⾏聚类,并得出中⼼点,这些中⼼点就是视觉码本的元素。
其次,利⽤图像分类器将图像分到已知的类别中,ERF(极端随机森林)算法⾮常流⾏,因为ERF具有较快的速度和⽐较精确的准确度。我们利⽤决策树进⾏正确决策。
最后,利⽤训练好的ERF模型后,创建⽬标识别器,可以识别未知图像的内容。
当然,这只是雏形,存在很多问题:
界⾯不友好。
准确率如何保证,如何调整超参数,只有认真研究算法机理,才能真正清除内部实现机制后给予改进。
下⾯,上代码:
import os
import sys
import argparse
import json
import cv2
import numpy as np
from sklearn.cluster import KMeans
# from star_detector import StarFeatureDetector
semble 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 dswith('.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 = act_features(img)
d(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]))) alize(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()pute(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))
size(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'
import pickle# 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 = de_labels(label_words)
self.clf.fit(np.asarray(X), y)
def encode_labels(self, label_words):
self.le.fit(label_words)
return np.array(ansform(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:
with open(codebook_file, 'rb') as f:
self.kmeans, ids = pickle.load(f)
def predict(self, img, scaling_size):
img = resize_image(img, scaling_size)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论