机器学习day07

6.特征值(描述)矩阵
STAR特征点(位置,大小)        SIFT(方向)
                   \______________________/
                                      |
                         特征值(描述)矩阵
代码:desc.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
import matplotlib.pyplot as mp
original = cv.imread('../../data/table.jpg')
cv.imshow('Original', original)
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
star = cv.xfeatures2d.StarDetector_create()
keypoints = star.detect(gray)
sift = cv.xfeatures2d.SIFT_create()
_, desc = sift.compute(gray, keypoints)
print(desc.shape)
mp.matshow(desc, cmap='jet', fignum='Description')
mp.title('Description', fontsize=20)
mp.xlabel('Feature', fontsize=14)
mp.ylabel('Sample', fontsize=14)
mp.tick_params(which='both', top=False,
               labeltop=False, labelbottom=True,
               labelsize=10)
mp.show()


7.识别图片中的物体
代码:obj.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import warnings
import numpy as np
import cv2 as cv
import hmmlearn.hmm as hl
warnings.filterwarnings(
    'ignore', category=DeprecationWarning)
np.seterr(all='ignore')


def search_objects(directory):
    directory = os.path.normpath(directory)
    if not os.path.isdir(directory):
        raise IOError("The directory '" +
                      directory +
                      "' doesn't exist!")
    objects = {}
    for curdir, subdirs, files in os.walk(directory):
        for jpeg in (file for file in files
                     if file.endswith('.jpg')):
            path = os.path.join(curdir, jpeg)
            label = path.split(os.path.sep)[-2]
            if label not in objects:
                objects[label] = []
            objects[label].append(path)
    return objects


train_objects = search_objects(
    '../../data/objects/training')
train_x, train_y = [], []
for label, filenames in train_objects.items():
    descs = np.array([])
    for filename in filenames:
        image = cv.imread(filename)
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        h, w = gray.shape[:2]
        f = 200 / min(h, w)
        gray = cv.resize(gray, None, fx=f, fy=f)
        star = cv.xfeatures2d.StarDetector_create()
        keypoints = star.detect(gray)
        sift = cv.xfeatures2d.SIFT_create()
        _, desc = sift.compute(gray, keypoints)
        if len(descs) == 0:
            descs = desc
        else:
            descs = np.append(descs, desc, axis=0)
    train_x.append(descs)
    train_y.append(label)
models = {}
for descs, label in zip(train_x, train_y):
    model = hl.GaussianHMM(
        n_components=4, covariance_type='diag',
        n_iter=1000)
    models[label] = model.fit(descs)
test_objects = search_objects(
    '../../data/objects/testing')
test_x, test_y, test_z = [], [], []
for label, filenames in test_objects.items():
    test_z.append([])
    descs = np.array([])
    for filename in filenames:
        image = cv.imread(filename)
        test_z[-1].append(image)
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        h, w = gray.shape[:2]
        f = 200 / min(h, w)
        gray = cv.resize(gray, None, fx=f, fy=f)
        star = cv.xfeatures2d.StarDetector_create()
        keypoints = star.detect(gray)
        sift = cv.xfeatures2d.SIFT_create()
        _, desc = sift.compute(gray, keypoints)
        if len(descs) == 0:
            descs = desc
        else:
            descs = np.append(descs, desc, axis=0)
    test_x.append(descs)
    test_y.append(label)
pred_test_y = []
for descs in test_x:
    best_score, best_label = None, None
    for label, model in models.items():
        score = model.score(descs)
        if (best_score is None) or (
                best_score < score):
            best_score, best_label = score, label
    pred_test_y.append(best_label)
i = 0
for label, pred_label, images in zip(
        test_y, pred_test_y, test_z):
    for image in images:
        i += 1
        cv.imshow('{} - {} {} {}'.format(
            i, label,
            '==' if label == pred_label else '!=',
            pred_label), image)
cv.waitKey()


十八、人脸识别
1.视频捕捉
视频捕捉设备->图像帧
代码:vidcap.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
vc = cv.VideoCapture(0)
while True:
    frame = vc.read()[1]
    cv.imshow('VideoCapture', frame)
    if cv.waitKey(33) == 27:
        break
vc.release()
cv.destroyAllWindows()


2.人脸定位
哈尔级联人脸定位
代码:haar.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import cv2 as cv
# 哈尔级联人脸定位器
fd = cv.CascadeClassifier('../../data/haar/face.xml')
ed = cv.CascadeClassifier('../../data/haar/eye.xml')
nd = cv.CascadeClassifier('../../data/haar/nose.xml')
vc = cv.VideoCapture(0)
while True:
    frame = vc.read()[1]
    faces = fd.detectMultiScale(frame, 1.3, 5)
    for l, t, w, h in faces:
        a, b = int(w / 2), int(h / 2)
        # cv.ellipse(图像, 椭圆心, 半径, 旋转角,
        #     起始角, 终止角, 颜色, 线宽)
        cv.ellipse(frame, (l + a, t + b), (a, b),
                   0, 0, 360, (255, 0, 255), 2)
        face = frame[t:t + h, l:l + w]
        eyes = ed.detectMultiScale(face, 1.3, 5)
        for l, t, w, h in eyes:
            a, b = int(w / 2), int(h / 2)
            cv.ellipse(face, (l + a, t + b),
                       (a, b), 0, 0, 360,
                       (0, 255, 0), 2)
        noses = nd.detectMultiScale(face, 1.3, 5)
        for l, t, w, h in noses:
            a, b = int(w / 2), int(h / 2)
            cv.ellipse(face, (l + a, t + b),
                       (a, b), 0, 0, 360,
                       (0, 255, 255), 2)
    cv.imshow('VideoCapture', frame)
    if cv.waitKey(33) == 27:
        break
vc.release()
cv.destroyAllWindows()


3.简单人脸识别:OpenCV的LBPH(局部二值模式直方图)
代码:face.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import numpy as np
import cv2 as cv
import sklearn.preprocessing as sp
fd = cv.CascadeClassifier('../../data/haar/face.xml')


def search_faces(directory):
    directory = os.path.normpath(directory)
    if not os.path.isdir(directory):
        raise IOError("The directory '" +
                      directory +
                      "' doesn't exist!")
    faces = {}
    for curdir, subdirs, files in os.walk(directory):
        for jpeg in (file for file in files
                     if file.endswith('.jpg')):
            path = os.path.join(curdir, jpeg)
            label = path.split(os.path.sep)[-2]
            if label not in faces:
                faces[label] = []
            faces[label].append(path)
    return faces


train_faces = search_faces(
    '../../data/faces/training')
codec = sp.LabelEncoder()
codec.fit(list(train_faces.keys()))
train_x, train_y = [], []
for label, filenames in train_faces.items():
    for filename in filenames:
        image = cv.imread(filename)
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        faces = fd.detectMultiScale(gray, 1.1, 2,
                                    minSize=(100, 100))
        for l, t, w, h in faces:
            train_x.append(
                gray[t:t + h, l:l + w])
            train_y.append(
                codec.transform([label])[0])
train_y = np.array(train_y)
# 局部二值模式直方图人脸识别分类器
model = cv.face.LBPHFaceRecognizer_create()
model.train(train_x, train_y)
test_faces = search_faces(
    '../../data/faces/testing')
test_x, test_y, test_z = [], [], []
for label, filenames in test_faces.items():
    for filename in filenames:
        image = cv.imread(filename)
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        faces = fd.detectMultiScale(gray, 1.1, 2,
                                    minSize=(100, 100))
        for l, t, w, h in faces:
            test_x.append(
                gray[t:t + h, l:l + w])
            test_y.append(
                codec.transform([label])[0])
            a, b = int(w / 2), int(h / 2)
            cv.ellipse(image, (l + a, t + b),
                       (a, b), 0, 0, 360,
                       (255, 0, 255), 2)
            test_z.append(image)
test_y = np.array(test_y)
pred_test_y = []
for face in test_x:
    pred_code = model.predict(face)[0]
    pred_test_y.append(pred_code)
escape = False
while not escape:
    for code, pred_code, image in zip(
            test_y, pred_test_y, test_z):
        label, pred_label = \
            codec.inverse_transform([code, pred_code])
        text = '{} {} {}'.format(
            label,
            '==' if code == pred_code else '!=',
            pred_label)
        cv.putText(image, text, (10, 60),
                   cv.FONT_HERSHEY_SIMPLEX, 2,
                   (255, 255, 255), 6)
        cv.imshow('Recognizing...', image)
        if cv.waitKey(1000) == 27:
            escape = True
            break


4.基于通用分类器的人脸识别
1)显示数据集
代码:face1.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import matplotlib.pyplot as mp
faces = sd.fetch_olivetti_faces('../../data/')
x = faces.data
y = faces.target
mp.figure('Olivette Faces', facecolor='black')
mp.subplots_adjust(
    left=0.04, bottom=0.04, right=0.96, top=0.96,
    wspace=0, hspace=0)
rows, cols = 10, 40
for row in range(rows):
    for col in range(cols):
        mp.subplot(rows, cols, row * cols + col + 1)
        if row == 0:
            mp.title(str(col), fontsize=8,
                     color='limegreen')
        if col == 0:
            mp.ylabel(str(row), fontsize=8,
                      color='limegreen')
        mp.xticks(())
        mp.yticks(())
        image = x[y == col][row].reshape(64, 64)
        mp.imshow(image, cmap='gray')
mp.show()


2)用SVM分类器实现人脸识别
代码:face2.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import sklearn.model_selection as ms
import sklearn.svm as svm
import sklearn.metrics as sm
import matplotlib.pyplot as mp
faces = sd.fetch_olivetti_faces('../../data/')
x = faces.data
y = faces.target
train_x, test_x, train_y, test_y = \
    ms.train_test_split(x, y, test_size=0.2,
                        random_state=7)
model = svm.SVC(class_weight='balanced')
model.fit(train_x, train_y)
pred_test_y = model.predict(test_x)
cm = sm.confusion_matrix(test_y, pred_test_y)
cr = sm.classification_report(test_y, pred_test_y)
print(cr)
mp.figure('Confusion Matrix', facecolor='lightgray')
mp.title('Confusion Matrix', fontsize=20)
mp.xlabel('Predicted Class', fontsize=14)
mp.ylabel('True Class', fontsize=14)
mp.tick_params(labelsize=10)
mp.imshow(cm, interpolation='nearest', cmap='gray')
mp.show()


3)通过主成分分析(PCA)降维
代码:pca1.py、pca2.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
# 原始样本
A = np.mat('3 2000; 2 3000; 4 5000; 5 8000; 1 2000',
           dtype=float)
print('A =', A, sep='\n')
# 归一缩放:各列均值为0,极差为1
mu = A.mean(axis=0)
s = A.max(axis=0) - A.min(axis=0)
X = (A - mu) / s
print('X =', X, sep='\n')
# 协方差矩阵
SIGMA = X.T * X
print('SIGMA =', SIGMA, sep='\n')
# 对协方差矩阵做奇异值分解
U, S, V = np.linalg.svd(SIGMA)
print('U =', U, sep='\n')
# 主成分特征矩阵
U_reduce = U[:, 0]
print('U_reduce =', U_reduce, sep='\n')
# 降维样本
Z = X * U_reduce
print('Z =', Z, sep='\n')
# 近似恢复归一缩放样本
X_approx = Z * U_reduce.T
print('X_approx =', X_approx, sep='\n')
# 近似恢复原始样本
A_approx = np.multiply(X_approx, s) + mu
print('A_approx =', A_approx, sep='\n')
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import sklearn.pipeline as pl
import sklearn.preprocessing as sp
import sklearn.decomposition as dc
# 原始样本
A = np.mat('3 2000; 2 3000; 4 5000; 5 8000; 1 2000',
           dtype=float)
print('A =', A, sep='\n')
# PCA模型
model = pl.Pipeline([
    ('scaler', sp.MinMaxScaler()),
    ('pca', dc.PCA(n_components=1))])
# 降维样本
Z = model.fit_transform(A)
print('Z =', Z, sep='\n')
# 近似恢复原始样本
A_approx = model.inverse_transform(Z)
print('A_approx =', A_approx, sep='\n')


4)还原率
经过PCA降维后样本,再进过逆向还原,与原始样本相比的误差。
代码:face3.py、face4.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import sklearn.decomposition as dc
import matplotlib.pyplot as mp
faces = sd.fetch_olivetti_faces('../../data/')
x = faces.data
y = faces.target
ncps = range(10, 410, 10)
evrs = []
for ncp in ncps:
    model = dc.PCA(n_components=ncp)
    model.fit_transform(x)
    # 还原率误差
    evr = model.explained_variance_ratio_.sum()
    evrs.append(evr)
mp.figure('Explained Variance Ratio',
          facecolor='lightgray')
mp.title('Explained Variance Ratio', fontsize=20)
mp.xlabel('n_components', fontsize=14)
mp.ylabel('Explained Variance Ratio', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.plot(ncps, evrs, c='dodgerblue',
        label='Explained Variance Ratio')
mp.legend()
mp.show()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import sklearn.decomposition as dc
import matplotlib.pyplot as mp
faces = sd.fetch_olivetti_faces('../../data/')
x = faces.data
y = faces.target
mp.figure('Explained Variance Ratio',
          facecolor='black')
mp.subplots_adjust(
    left=0.04, bottom=0.04, right=0.96, top=0.96,
    wspace=0, hspace=0)
rows, cols = 11, 40
for row in range(rows):
    if row > 0:
        ncp = 140 - (row - 1) * 15
        model = dc.PCA(n_components=ncp)
        model.fit(x)
    for col in range(cols):
        mp.subplot(rows, cols, row * cols + col + 1)
        if row == 0:
            mp.title(str(col), fontsize=8,
                     color='limegreen')
        if col == 0:
            mp.ylabel(str(ncp) if row > 0
                      else 'orig', fontsize=8,
                      color='limegreen')
        mp.xticks(())
        mp.yticks(())
        if row > 0:
            pca_x = model.transform([x[y == col][0]])
            ipca_x = model.inverse_transform(pca_x)
            image = ipca_x.reshape(64, 64)
        else:
            image = x[y == col][0].reshape(64, 64)
        mp.imshow(image, cmap='gray')
mp.show()


5)用PCA降维改善基于SVM分类器的人脸识别效果
代码:face5.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import sklearn.decomposition as dc
import sklearn.model_selection as ms
import sklearn.svm as svm
import sklearn.metrics as sm
import matplotlib.pyplot as mp
faces = sd.fetch_olivetti_faces('../../data/')
x = faces.data
y = faces.target
model = dc.PCA(n_components=140)
pca_x = model.fit_transform(x)
train_x, test_x, train_y, test_y = \
    ms.train_test_split(pca_x, y, test_size=0.2,
                        random_state=7)
model = svm.SVC(class_weight='balanced')
model.fit(train_x, train_y)
pred_test_y = model.predict(test_x)
cm = sm.confusion_matrix(test_y, pred_test_y)
cr = sm.classification_report(test_y, pred_test_y)
print(cr)
mp.figure('Confusion Matrix', facecolor='lightgray')
mp.title('Confusion Matrix', fontsize=20)
mp.xlabel('Predicted Class', fontsize=14)
mp.ylabel('True Class', fontsize=14)
mp.tick_params(labelsize=10)
mp.imshow(cm, interpolation='nearest', cmap='gray')
mp.show()


6)核主成分分析(KPCA)
对于在原始维度空间无法线性分割的样本,利用核函数升高其维度,力图在更高维度空间线性可分,而后在选择适当的与原始维度相同的空间做反向投影,得到低维度空间线性可分的样本集。
代码:kpca.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sklearn.datasets as sd
import sklearn.decomposition as dc
import matplotlib.pyplot as mp
x, y = sd.make_circles(n_samples=500, factor=0.2,
                       noise=0.04)
model = dc.KernelPCA(kernel='rbf', gamma=1)
kpca_x = model.fit_transform(x)[:, :x.shape[1]]
mp.figure('Original', facecolor='lightgray')
mp.title('Original', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.scatter(x[:, 0], x[:, 1], c=y, cmap='brg',
           alpha=0.5, s=60)
mp.figure('KPCA', facecolor='lightgray')
mp.title('KPCA', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.scatter(kpca_x[:, 0], kpca_x[:, 1], c=y,
           cmap='brg', alpha=0.5, s=60)
mp.show()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值