在jupyter里用python处理图像并提取图像特征做人脸识别实验

华工小软 华工小软 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
本人python小白一枚,学院突然要求用python做机器学习基于Adaboost算法的人脸识别实验。。猝不及防呀。。

写了一会儿代码之后发现,自己的程序调用老师提供feature.py的类方法时会报错index error,看了半天不知道哪里出错,希望能有高手指点。

顺便问一下,如何用pickle库中的dump()函数将特征数据存在缓存中,稍后再用load()读取?


报错信息:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-16-7de3db667e50> in <module>()
52 for k in range(data_size - 1):
53 npd = NPDFeature(img_set[k])
---> 54 npd.extract()

<ipython-input-16-7de3db667e50> in extract(self)
32 for i in range(self.n_pixels - 1):
33 for j in range(i + 1, self.n_pixels, 1):
---> 34 self.features[count] = NPDFeature.__NPD_table__[self.image[i]][self.image[j]]
35 count += 1
36 return self.features

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

代码:
from skimage import data_dir,transform,color
import numpy
import cv2
import skimage.io as io
import matplotlib.pyplot as plt


def convert_gray(f):
rgb=io.imread(f) # read RGB image
gray=color.rgb2gray(rgb) # convert RGB images to Gray images
dst=transform.resize(gray,(24,24)) # resize
return dst

path='/Users/jzh/Desktop/ML2018-lab-03-master/datasets/original/face/*.jpg:/Users/jzh/Desktop/ML2018-lab-03-master/datasets/original/nonface/*.jpg' # this is my images' path
img_set = io.ImageCollection(path,load_func=convert_gray) # load images and convert them

#io.imshow(img_set[20])


# %load feature.py
import numpy

data_size = 1000 # there are totally 1000 images

class NPDFeature():
"""It is a tool class to extract the NPD features.

Attributes:
image: A two-dimension ndarray indicating grayscale image.
n_pixels: An integer indicating the number of image total pixels.
features: A one-dimension ndarray to store the extracted NPD features.
"""
__NPD_table__ = None

def __init__(self, image):
'''Initialize NPDFeature class with an image.'''
if NPDFeature.__NPD_table__ is None:
NPDFeature.__NPD_table__ = NPDFeature.__calculate_NPD_table()
assert isinstance(image, numpy.ndarray)
self.image = image.ravel()
self.n_pixels = image.size
self.features = numpy.empty(shape=self.n_pixels * (self.n_pixels - 1) // 2, dtype=float)

def extract(self):
'''Extract features from given image.

Returns:
A one-dimension ndarray to store the extracted NPD features.
'''
count = 0
for i in range(self.n_pixels - 1):
for j in range(i + 1, self.n_pixels, 1):
self.features[count] = NPDFeature.__NPD_table__[self.image[i]][self.image[j]] #报错
count += 1
return self.features

@staticmethod
def __calculate_NPD_table():
'''Calculate all situations table to accelerate feature extracting.'''
print("Calculating the NPD table...")
table = numpy.empty(shape=(1 << 8, 1 << 8), dtype=float)
for i in range(1 << 8):
for j in range(1 << 8):
if i == 0 and j == 0:
table[i][j] = 0
else:
table[i][j] = (i - j) / (i + j)
return table

# extract features
for k in range(data_size - 1):
npd = NPDFeature(img_set[k])
npd.extract() #extract处报错
...全文

相关推荐

python实现LBP方法提取图像纹理特征实现分类的步骤

主要介绍了python实现LBP方法提取图像纹理特征实现分类的步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38650150

基于opencv和python的人脸识别

基于opencv和python的人脸识别基于python和opencv的人脸识别门禁系统,用了opencv的lbph算法,达到百分之七十以上的相似度才可认为是识别成功

GZM888888

基于jupyter notebook的python编程—–通过python编程,利用opencv-python对图像进行处理

基于jupyter notebook的python编程—–通过python编程,利用opencv对图像进行处理目录一、在python命令行下载opencv图像处理库1、打开python终端命令行2、在终端输入以下命令,下载opencv-python图像处理库二、准备图片视频数据1、将自己准备的图片放在相应的位置,记住不要有中文路径、图片的命名也不要是中文的2、准备一段不太长的视频,10秒之内最好,用于帧差法处理图像三、运行jupyter notebook,搭建python环境1、打开Windows终端命令行,输入jupyter notebook,打开我们的jupyter工具,如下所示:2、在j

weixin_38598613

rbm-feature-extraction:使用受限Boltzmann机进行图像特征提取的简单介绍

rbm-feature-extraction:使用受限Boltzmann机进行图像特征提取的简单介绍

chsqi

图像字幕生成器:LSTM模型从预先训练的VGG-16模型中提取特征后,会为输入图像生成字幕。 (计算机视觉,自然语言处理,深度学习,Python)

图像字幕生成器:LSTM模型从预先训练的VGG-16模型中提取特征后,会为输入图像生成字幕。 (计算机视觉,自然语言处理,深度学习,Python)

Luna Knight
评论 1
收藏 已收藏
分享
返回顶部
华工小软
脚本语言 37,738 成员 34,208 内容