python - 我正在做一些 HOG 特征提取并收到此错误 IndexError : too many indices for array

标签 python machine-learning

所以基本上我已经创建了返回 hog 的切片函数 我尝试自己解决,但失败了。如果有人熟悉此类错误并解决了此问题,请帮助我。

这段代码如下:

start_frame = cv2.imread("../train/abnormal_activity/act100014.jpg")
sourcer = FeatureHelp(feature_params, start_frame)

f = sourcer.features(start_frame)
print("feature shape:", f.shape)

rgb_img, a_img, b_img, c_img = sourcer.visualize()
show_images([rgb_img, a_img, b_img, c_img], per_row = 4, per_col = 1, W = 10, H = 2)

错误行是f = sourcer.features(start_frame)

该行引用了这段代码

from skimage.feature import hog
import numpy as np
from helper import convert, show_images

class FeatureHelp:
    # json object assign
    def __init__(self, p, start_frame):

        self.color_model = p['color_model']

        self.size = p['bounding_box_size']
        self.ori = p['number_of_orientations']

        self.ppc = (p['pixel_per_cell'], p['pixel_per_cell'])
        self.cpb = (p['cell_per_block'], p['cell_per_block'])

        self.do_sqrt = p['do_transform_sqrt']

        self.ABC_img = None
        self.dims = (None, None, None)
        self.hogA, self.hogB, self.HogC = None, None, None
        self.hogA_img, self.hogB_img, self.hogC_img = None, None, None

        self.RGB_img = start_frame
        self.new_frame(self.RGB_img)

    def hogg(self, channel):
        features, hog_img = hog(
            channel, orientations = self.ori, 
            pixels_per_cell = self.ppc,
            cells_per_block = self.cpb, 
            transform_sqrt = self.do_sqrt, 
            visualize = True
        )
        return features, hog_img

    def new_frame(self, frame):

        self.RGB_img = frame
        self.ABC_img = convert(frame, src_model = 'rgb', dest_model = self.color_model)

        self.hogA, self.hogA_img = self.hogg(self.ABC_img[:, :, 0])
        self.hogB, self.hogB_img = self.hogg(self.ABC_img[:, :, 1])
        self.hogC, self.hogC_img = self.hogg(self.ABC_img[:, :, 2])

    def slice(self, x_pix, y_pix, w_pix, h_pix):

        x_start, x_end, y_start, y_end = self.pix_to_hog(x_pix, y_pix, h_pix, w_pix)

        hogA = self.hogA[y_start: y_end, x_start: x_end].ravel()
        hogB = self.hogB[y_start: y_end, x_start: x_end].ravel()
        hogC = self.hogC[y_start: y_end, x_start: x_end].ravel()
        hog = np.hstack((hogA, hogB, hogC))

        return hog

    def features(self, frame):
        self.new_frame(frame)
        return self.slice(0, 0, frame.shape[1], frame.shape[0])

    def visualize(self):
        return self.RGB_img, self.hogA_img, self.hogB_img, self.hogC_img

    def pix_to_hog(self, x_pix, y_pix, h_pix, w_pix):

        if h_pix is None and w_pix is None: 
            h_pix, w_pix = self.size, self.size

        h = h_pix // self.ppc[0]
        w = w_pix // self.ppc[0]
        y_start = y_pix // self.ppc[0]
        x_start = x_pix // self.ppc[0]
        y_end = y_start + h - 1
        x_end = x_start + w - 1

        return x_start, x_end, y_start, y_end

问题出在这部分:

def new_frame(self, frame):

    self.RGB_img = frame
    self.ABC_img = convert(frame, src_model = 'rgb', dest_model = self.color_model)

    self.hogA, self.hogA_img = self.hogg(self.ABC_img[:, :, 0])
    self.hogB, self.hogB_img = self.hogg(self.ABC_img[:, :, 1])
    self.hogC, self.hogC_img = self.hogg(self.ABC_img[:, :, 2])

def slice(self, x_pix, y_pix, w_pix, h_pix):

    x_start, x_end, y_start, y_end = self.pix_to_hog(x_pix, y_pix, h_pix, w_pix)

    hogA = self.hogA[y_start: y_end, x_start: x_end].ravel()
    hogB = self.hogB[y_start: y_end, x_start: x_end].ravel()
    hogC = self.hogC[y_start: y_end, x_start: x_end].ravel()
    hog = np.hstack((hogA, hogB, hogC))

    return hog

def features(self, frame):
    self.new_frame(frame)
    return self.slice(0, 0, frame.shape[1], frame.shape[0])

错误是: here..

请帮我解决这个问题。

最佳答案

我认为这是一个错字。

在它显示的错误的traceback中。

hogA = self.hogA[y_start, y_end, x_start, x_end].ravel()

但是在你的帖子中,你有,这确实是正确的。

hogA = self.hogA[y_start: y_end, x_start: x_end].ravel()

关于python - 我正在做一些 HOG 特征提取并收到此错误 IndexError : too many indices for array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384865/

相关文章:

python - Keras:一维输入的卷积层

python - 我在随机森林分类器中收到“未拟合”错误?

Python - 如何使用 MNIST 预测不同形状的 np.darray

python - 了解 Python 中的 json 响应类型和 IDE 自动完成功能

python - 如何在 TextEdit 中使用文本并对其应用更改?就像我的代码看到它并告诉我我可以在 -btn_func- 函数中做什么?

python - 如何在箱形图2上绘制来自不同数据帧的数据 - Python

machine-learning - 可以使用哪些算法从时间序列的片段构建预测变量?

tensorflow - 如何从 ImageDataGenerator 获取 x_train 和 y_train?

python - Keras/Tensorflow 中的时间分布

python 泽普。强制不使用代理