没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
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
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
资源评论
??2050
- 粉丝: 2
- 资源: 924
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功