开发者社区 问答 正文

我做了一些HOG特征提取,得到了这个错误的IndexError:数组的索引太多了

基本上我创建了返回hog的slice函数 我试着自己解决,但我失败了。如果有人熟悉这种类型的错误,并解决了这个请帮助我。 代码如下:

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 = source .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])

错误: 在这里. . 请帮我解决这个问题。 问题来源StackOverflow 地址:/questions/59384865/i-was-doing-some-hog-feature-extraction-and-got-this-error-indexerror-too-many

展开
收起
kun坤 2019-12-26 14:28:17 447 分享 版权
1 条回答
写回答
取消 提交回答
  • 我想是打错了。 在错误的回溯中显示。

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

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

    hogA = self.hogA[y_start: y_end, x_start: x_end].ravel()
    
    2019-12-26 14:28:25
    赞同 展开评论
问答分类:
问答地址: